Only include report style blocks and structure the output into nested bullets grouped by project and headers

This commit is contained in:
Campbell Alden 2022-06-28 10:52:03 +09:00
parent 71e022be93
commit 051d5e82ce

72
dump.py
View file

@ -1,4 +1,5 @@
import things import things
import re
import random import random
import sys import sys
@ -11,7 +12,6 @@ def mine_tags(tags, place):
tags.extend(place['tags']) tags.extend(place['tags'])
def has_tag(task, tag): def has_tag(task, tag):
area = things.areas(uuid=task['area']) if 'area' in task else None area = things.areas(uuid=task['area']) if 'area' in task else None
project = things.projects(uuid=task['project']) if 'project' in task else None project = things.projects(uuid=task['project']) if 'project' in task else None
@ -25,28 +25,56 @@ def has_tag(task, tag):
mine_tags(tags, project_area) mine_tags(tags, project_area)
return tag in tags return tag in tags
def format_project(project): def tasks_to_heirarchy(top_level_comment, tasks):
return project['title'] heir = {}
for task in tasks:
task_leaf = (task, {})
if 'project' not in task and 'heading' not in task:
heir[task['uuid']] = task_leaf
elif 'heading' in task:
h_uuid = task['heading']
head = things.get(h_uuid)
def format_task(task): p_uuid = head['project'] # This must exist
task_str = f"{task['title']}. {task['notes']}" proj = things.get(p_uuid)
if 'heading' in task: proj_title, proj_sublevel = heir[p_uuid] if p_uuid in heir else (proj, {})
heading = things.get(task['heading']) head_title, head_sublevel = proj_sublevel[h_uuid] if h_uuid in proj_sublevel else (head, {})
task_str = f"{heading['project_title']} > {heading['title']} > {task_str}" head_sublevel[task['uuid']] = task_leaf
elif 'project_title' in task: proj_sublevel[head['uuid']] = (head_title, head_sublevel)
task_str = f"{task['project_title']} > {task_str}" heir[p_uuid] = (proj_title, proj_sublevel)
else: # The project must not be None here
p_uuid = task['project']
proj = things.get(p_uuid)
proj_title, proj_sublevel = heir[p_uuid] if p_uuid in heir else (proj, {})
proj_sublevel[task['uuid']] = task_leaf
heir[p_uuid] = (proj_title, proj_sublevel)
return task_str.strip() return (top_level_comment, heir)
reportable = list(filter(lambda t: has_tag(t, target_tag), things.today()))
top_level_comment = ' '.join(map(lambda e: f':{e}:', random.choices(EMOJIS, k=3)))
structured_tasks = tasks_to_heirarchy({ 'title': top_level_comment, 'notes': '' }, reportable)
def parse_note(note):
note_block_pattern = r"```report\n([\s\S]*?)\n?```"
return ' '.join(re.findall(note_block_pattern, note))
def recursive_format(structure, depth):
(node, branches) = structure
notes = parse_note(node['notes'])
node_str = f"{node['title']}{'' if notes == '' else f'. {notes}'}"
if len(branches) == 1:
item = list(branches.values())[0]
node_str += f" > {recursive_format(item, depth)}"
elif len(branches) > 1:
new_depth = depth + 2
space = ''.join([' '] * new_depth)
bullets = [f"{space}- {recursive_format(item, new_depth)}" for item in branches.values()]
node_str += ''.join(map(lambda b: f'\n{b}', bullets))
return node_str
reportable = filter(lambda t: has_tag(t, target_tag), things.today()) print(recursive_format(structured_tasks, 0))
#print('\n'.join(output))
output = [' '.join(map(lambda e: f':{e}:', random.choices(EMOJIS, k=3))), ''] # TODO: Three random emojis?
for reported in reportable:
if reported['type'] == 'project':
output.append(f'- {format_project(reported)}')
else:
output.append(f'- {format_task(reported)}')
print('\n'.join(output))