-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path__init__.py
More file actions
273 lines (218 loc) · 7.35 KB
/
Copy path__init__.py
File metadata and controls
273 lines (218 loc) · 7.35 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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
"""Parse taskmark-spec markdown files."""
from __future__ import annotations
from pathlib import Path
from typing import IO
from taskmark._loader import build_task_store
from taskmark._mutator import DEFAULT_STATE_MACHINE
from taskmark._mutator import update_task as _update_task
from taskmark._parser import parse_file, parse_string
from taskmark._types import (
REMOVE,
BatchUpdateResult,
FileInfo,
Frontmatter,
ParseError,
RejectedChange,
StateMachine,
StateTransition,
Task,
TaskChanges,
TaskState,
TaskStore,
TaskUpdateSpec,
UpdateResult,
UpdateStatus,
Warning,
WriteResult,
)
from taskmark._writer import write_updates as _write_updates
__version__ = "0.1.0"
__all__ = [
"DEFAULT_STATE_MACHINE",
"REMOVE",
"BatchUpdateResult",
"FileInfo",
"Frontmatter",
"ParseError",
"RejectedChange",
"StateMachine",
"StateTransition",
"Task",
"TaskChanges",
"TaskState",
"TaskStore",
"TaskUpdateSpec",
"UpdateResult",
"UpdateStatus",
"Warning",
"WriteResult",
"add_assignees",
"add_tags",
"batch_update",
"load",
"loads",
"remove_assignees",
"remove_tags",
"update",
"write_updates",
]
def loads(
s: str,
/,
*,
minutes_per_day: int = 480,
) -> TaskStore:
"""Parse taskmark markdown string.
Args:
s: Markdown string containing tasks
minutes_per_day: Minutes in a work day for ~1d duration (default: 480)
Returns:
TaskStore with tasks and any warnings
Example:
store = taskmark.loads("- [ ] My task")
for task in store:
print(task["title"])
"""
return parse_string(s, minutes_per_day=minutes_per_day)
def load(
fp: str | Path | IO[str],
/,
*,
minutes_per_day: int = 480,
) -> TaskStore:
"""Parse taskmark file, following any [text](file.md) links.
Args:
fp: File path (str/Path) or file object
minutes_per_day: Minutes in a work day for ~1d duration (default: 480)
Returns:
TaskStore with tasks, file metadata, and any warnings
Raises:
FileNotFoundError: If file doesn't exist
Example:
store = taskmark.load("tasks.md")
store.check() # raises ParseError if warnings
for task in store:
print(task["title"])
"""
if isinstance(fp, (str, Path)):
return build_task_store(fp, minutes_per_day=minutes_per_day)
return parse_file(fp, minutes_per_day=minutes_per_day)
# ─────────────────────────────────────────────────────────────
# Mutation API
# ─────────────────────────────────────────────────────────────
def update(
store: TaskStore,
title: str,
project: str | None,
changes: TaskChanges,
*,
today: str | None = None,
) -> UpdateResult:
"""Update a task in the store.
Finds the task by (title, project), validates changes, applies them in-memory.
Call write_updates() to persist changes to disk.
Args:
store: TaskStore from load() or loads()
title: Exact task title
project: Full project path (e.g., "acme/product/core/v2"), or None
changes: Changes to apply
today: Override today's date (for testing)
Returns:
UpdateResult with status and task
- SUCCESS: All changes applied, task is updated
- PARTIAL: Some changes rejected (check result.rejected)
- FAIL: Task not found or ambiguous (check result.error)
Note:
Does NOT raise exceptions. Check result.status instead.
"""
return _update_task(store, title, project, changes, today)
def batch_update(
store: TaskStore,
updates: list[TaskUpdateSpec],
*,
today: str | None = None,
) -> BatchUpdateResult:
"""Update multiple tasks in the store.
Best-effort: each update is independent. If one fails,
others can still succeed.
Args:
store: TaskStore from load() or loads()
updates: List of (title, project, changes) specs
today: Override today's date (for testing)
Returns:
BatchUpdateResult with results for each update
- Each result has status: SUCCESS, PARTIAL, or FAIL
- FAIL results include error message (e.g., "task not found")
Note:
Does NOT raise exceptions. Check result.status for each update.
"""
results: list[UpdateResult] = []
succeeded = 0
partial = 0
failed = 0
for spec in updates:
result = _update_task(store, spec.title, spec.project, spec.changes, today)
results.append(result)
if result.status == UpdateStatus.SUCCESS:
succeeded += 1
elif result.status == UpdateStatus.PARTIAL:
partial += 1
else:
failed += 1
return BatchUpdateResult(
total=len(updates),
succeeded=succeeded,
partial=partial,
failed=failed,
results=results,
)
def write_updates(store: TaskStore) -> WriteResult:
"""Write all pending changes to files.
Processes files independently. Within each file, processes
tasks bottom-up by line number to avoid index drift.
Each file is written atomically (temp file + rename).
Args:
store: TaskStore with pending changes from update()/batch_update()
Returns:
WriteResult with list of modified files and stats
"""
return _write_updates(store)
# ─────────────────────────────────────────────────────────────
# Helper Functions
# ─────────────────────────────────────────────────────────────
def add_tags(task: Task, *tags: str) -> list[str]:
"""Return explicit_tags with additions (preserves order, no duplicates).
Args:
task: Task to get current tags from
*tags: Tags to add
Returns:
New tag list suitable for TaskChanges.tags
"""
return list(dict.fromkeys(task.explicit_tags + list(tags)))
def remove_tags(task: Task, *tags: str) -> list[str]:
"""Return explicit_tags with removals.
Args:
task: Task to get current tags from
*tags: Tags to remove
Returns:
New tag list suitable for TaskChanges.tags
"""
return [t for t in task.explicit_tags if t not in tags]
def add_assignees(task: Task, *assignees: str) -> list[str]:
"""Return explicit_assignees with additions (preserves order, no duplicates).
Args:
task: Task to get current assignees from
*assignees: Assignees to add
Returns:
New assignee list suitable for TaskChanges.assignees
"""
return list(dict.fromkeys(task.explicit_assignees + list(assignees)))
def remove_assignees(task: Task, *assignees: str) -> list[str]:
"""Return explicit_assignees with removals.
Args:
task: Task to get current assignees from
*assignees: Assignees to remove
Returns:
New assignee list suitable for TaskChanges.assignees
"""
return [a for a in task.explicit_assignees if a not in assignees]