53 lines
1.6 KiB
Python
53 lines
1.6 KiB
Python
import sys
|
|
import re
|
|
import os
|
|
|
|
def promote_task(file_path, project_id):
|
|
if not os.path.exists(file_path):
|
|
print(f"Error: {file_path} not found")
|
|
return
|
|
|
|
with open(file_path, 'r') as f:
|
|
lines = f.readlines()
|
|
|
|
in_project = False
|
|
project_level = 0
|
|
updated = False
|
|
|
|
for i, line in enumerate(lines):
|
|
# 1. Identify project
|
|
if f":ID: {project_id}" in line:
|
|
in_project = True
|
|
# Find the nearest parent headline to get the level
|
|
for j in range(i, -1, -1):
|
|
m = re.match(r'^(\*+) ', lines[j])
|
|
if m:
|
|
project_level = len(m.group(1))
|
|
break
|
|
continue
|
|
|
|
if in_project:
|
|
# Check if we exited project by hitting a headline of same or higher level
|
|
headline_match = re.match(r'^(\*+) ', line)
|
|
if headline_match and len(headline_match.group(1)) <= project_level:
|
|
in_project = False
|
|
break
|
|
|
|
# 2. Find first available TODO to promote
|
|
if re.match(r'^\*+ TODO ', line) and not updated:
|
|
lines[i] = line.replace("TODO ", "NEXT ", 1)
|
|
updated = True
|
|
print(f"Promoted: {lines[i].strip()}")
|
|
|
|
if updated:
|
|
with open(file_path, 'w') as f:
|
|
f.writelines(lines)
|
|
else:
|
|
print(f"No TODO found to promote in project {project_id}")
|
|
|
|
if __name__ == "__main__":
|
|
if len(sys.argv) < 3:
|
|
print("Usage: promote_task.py <file_path> <project_id>")
|
|
else:
|
|
promote_task(sys.argv[1], sys.argv[2])
|