-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtask_parser.py
More file actions
executable file
·144 lines (123 loc) · 4.26 KB
/
task_parser.py
File metadata and controls
executable file
·144 lines (123 loc) · 4.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#!/usr/bin/env python3
"""
Task parser utility for Antigravity CLI.
Extracts and analyzes task.md files to provide structured access to task status
and metadata. Designed to be imported and used by antigravity-cli.py.
"""
import os
import json
from pathlib import Path
from typing import Dict, List, Any
def parse_task_md(task_path: Path) -> Dict[str, List[Dict[str, Any]]]:
"""
Read task.md and extract all checklist items.
Parses each line to identify status:
- [x] = completed
- [/] = in_progress
- [ ] = todo
Extracts the ID from each line (e.g., <!-- id: 0 -->)
Returns structured data with lists for completed, in_progress, and todo items.
Each item is a dict: { 'text': str, 'id': int }
Args:
task_path: Path to task.md file
Returns:
Dict with keys: 'completed', 'in_progress', 'todo'
"""
result = {
'completed': [],
'in_progress': [],
'todo': []
}
task_md_path = task_path / "task.md"
if not task_md_path.exists():
print(f"Error: task.md not found at {task_md_path}", file=sys.stderr)
return result
try:
with open(task_md_path, 'r') as f:
lines = f.readlines()
except Exception as e:
print(f"Error reading task.md: {e}", file=sys.stderr)
return result
for line in lines:
# Skip empty lines and headers
line = line.strip()
if not line or line.startswith('#'):
continue
# Check for checklist item pattern
if line.startswith('[x]') or line.startswith('[/]') or line.startswith('[ ]'):
# Extract status
if line.startswith('[x]'):
status = 'completed'
text = line[3:].strip()
elif line.startswith('[/]'):
status = 'in_progress'
text = line[3:].strip()
elif line.startswith('[ ]'):
status = 'todo'
text = line[3:].strip()
else:
continue
# Extract ID from HTML comment
item_id = None
if '<!-- id:' in line:
try:
# Extract between <!-- id: and -->
start = line.find('<!-- id:') + len('<!-- id:')
end = line.find('-->', start)
if end != -1:
item_id_str = line[start:end].strip()
item_id = int(item_id_str)
except ValueError:
item_id = None
# Add to result
result[status].append({
'text': text,
'id': item_id
})
return result
def get_task_summary(task_path: Path) -> Dict[str, Any]:
"""
Return task summary including:
- task name (first line after # in task.md)
- version and updatedAt from task.md.metadata.json
- GUID from the directory name
Args:
task_path: Path to task directory
Returns:
Dict with keys: 'name', 'version', 'updatedAt', 'guid'
"""
# Extract GUID from directory name
guid = task_path.name
# Extract task name from task.md
task_name = "Unknown Task"
task_md_path = task_path / "task.md"
if task_md_path.exists():
try:
with open(task_md_path, 'r') as f:
first_line = f.readline().strip()
if first_line.startswith('#'):
task_name = first_line[1:].strip()
else:
task_name = first_line
except Exception:
pass
# Extract version and updatedAt from metadata
version = "unknown"
updated_at = "unknown"
meta_path = task_path / "task.md.metadata.json"
if meta_path.exists():
try:
with open(meta_path, 'r') as f:
metadata = json.load(f)
version = metadata.get('version', 'unknown')
updated_at = metadata.get('updatedAt', 'unknown')
except json.JSONDecodeError:
pass
except Exception:
pass
return {
'name': task_name,
'version': version,
'updatedAt': updated_at,
'guid': guid
}