-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreadme_parser.py
More file actions
104 lines (83 loc) · 2.91 KB
/
readme_parser.py
File metadata and controls
104 lines (83 loc) · 2.91 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
"""Parse README files for task lists."""
import re
from pathlib import Path
from typing import List, Tuple
def parse_readme_tasks(project_path: str) -> List[dict]:
"""
Parse README.md for task items.
Supports formats:
- [ ] Uncompleted task
- [x] Completed task
- [X] Completed task
* [ ] Also works with asterisks
Returns list of dicts with 'title', 'completed', 'priority'
"""
if not project_path:
return []
path = Path(project_path)
# Try common README filenames
readme_names = ['README.md', 'readme.md', 'README.MD', 'Readme.md',
'README', 'readme', 'README.txt', 'readme.txt']
readme_path = None
for name in readme_names:
candidate = path / name
if candidate.exists():
readme_path = candidate
break
if not readme_path:
return []
try:
content = readme_path.read_text(encoding='utf-8')
except Exception:
return []
tasks = []
priority = 0
# Match markdown task lists: - [ ] task or - [x] task
# Also matches * [ ] task format
pattern = r'^[\s]*[-*]\s*\[([ xX])\]\s*(.+)$'
for line in content.split('\n'):
match = re.match(pattern, line)
if match:
checkbox, title = match.groups()
completed = checkbox.lower() == 'x'
tasks.append({
'title': title.strip(),
'completed': completed,
'priority': priority,
'from_readme': True
})
priority += 1
return tasks
def sync_readme_tasks(existing_tasks: List[dict], readme_tasks: List[dict]) -> Tuple[List[dict], List[dict], List[dict]]:
"""
Sync tasks from README with existing database tasks.
Returns:
(tasks_to_add, tasks_to_update, all_merged_tasks)
"""
# Create lookup by title for existing tasks
existing_by_title = {t['title']: t for t in existing_tasks}
readme_by_title = {t['title']: t for t in readme_tasks}
tasks_to_add = []
tasks_to_update = []
merged = []
# Process README tasks
for task in readme_tasks:
title = task['title']
if title in existing_by_title:
# Task exists, check if completion status changed
existing = existing_by_title[title]
if existing.get('completed') != task['completed']:
tasks_to_update.append({
'id': existing['id'],
'completed': task['completed']
})
merged.append({**existing, 'from_readme': True})
else:
# New task from README
tasks_to_add.append(task)
merged.append(task)
# Add existing tasks not in README (manual tasks)
for task in existing_tasks:
if task['title'] not in readme_by_title:
merged.append({**task, 'from_readme': False})
return tasks_to_add, tasks_to_update, merged