ollie/tools/task_complete.py

53 lines
1.5 KiB
Python
Executable File

#!/usr/bin/env python3
# description: Mark a plan step complete; returns remaining steps
# Args: <plan-file> <step-substring>
import sys, os, re
if len(sys.argv) < 3:
print("usage: task_complete <plan-file> <step-substring>", file=sys.stderr)
sys.exit(1)
ollie = os.environ.get('OLLIE_9MOUNT', os.path.expanduser('~/mnt/ollie'))
plan_dir = os.path.join(ollie, 'pl')
# Accept either a bare filename or a path; always resolve against plan_dir
filepath = os.path.join(plan_dir, os.path.basename(sys.argv[1]))
substring = sys.argv[2].lower()
with open(filepath) as f:
lines = f.readlines()
matched = False
for i, line in enumerate(lines):
if re.match(r'^- \[ \] ', line) and substring in line.lower():
lines[i] = line.replace('- [ ] ', '- [x] ', 1)
matched = True
break
if not matched:
print(f"no unchecked step matching {sys.argv[2]!r}", file=sys.stderr)
sys.exit(1)
remaining = [line[6:].strip() for line in lines if re.match(r'^- \[ \] ', line)]
def restat(path, status):
return re.sub(r'__(todo|wip|complete)\.md$', f'__{status}.md', path)
if '__todo' in filepath:
newpath = restat(filepath, 'wip')
elif not remaining and '__wip' in filepath:
newpath = restat(filepath, 'complete')
else:
newpath = filepath
with open(filepath, 'w') as f:
f.writelines(lines)
if newpath != filepath:
os.rename(filepath, newpath)
if remaining:
print('\n'.join(f'- {s}' for s in remaining))
else:
print('all steps complete')