Created
September 14, 2020 00:17
-
-
Save cscorley/fd340c014f8ba59164919e43cf00f60f to your computer and use it in GitHub Desktop.
personal worklog generator for leo
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@language python | |
import datetime | |
import os.path | |
worklog_name = 'Worklogs' | |
numdays = 5 * 365 | |
def generate_node(primary_node, node_name, child=True, body_text=''): | |
node = g.findNodeInTree(c, primary_node, node_name) | |
if node is None and not child: | |
# maybe it's elsewhere? | |
node = g.findNodeAnywhere(c, node_name) | |
is_new = False | |
if node is None: | |
g.es(f'Creating new worklog node named {node_name}') | |
if child: | |
node = primary_node.insertAsLastChild() | |
else: | |
node = primary_node.insertAfter() | |
node.h = node_name | |
is_new = True | |
if body_text: | |
g.es(f'Inserting body text into {node_name}') | |
node.b = body_text | |
else: | |
g.es(f'No body to set for {node_name}') | |
return node, is_new | |
# no idea how else to do this? | |
root = c.lastTopLevel() | |
g.es('generating work node') | |
worklog, new_worklog = generate_node(root, worklog_name, child=False) | |
base = datetime.datetime.today() | |
date_list = [base - datetime.timedelta(days=x) for x in range(numdays)] | |
previous_text = dict() | |
if new_worklog: | |
years = sorted(list(set(date.strftime("%Y") for date in date_list))) | |
for year in years: | |
fn = g.os_path_finalize_join(g.app.homeDir, 'worklogs', f'worklog-{year}.md') | |
if os.path.exists(fn): | |
with open(fn, 'r') as f: | |
current_date = None | |
current_log = [] | |
for line in f.readlines(): | |
if line.startswith('#'): | |
ld = line[1:].strip() | |
try: | |
date = datetime.datetime.strptime(ld, "%Y-%m-%d") | |
if current_date: | |
g.es(f'Adding {len(current_log)} log lines for {current_date}') | |
previous_text[current_date] = ''.join(current_log) | |
current_date = ld | |
current_log = [] | |
except: | |
g.es(f"Found start of section, was not date though: {line} '{ld}'") | |
else: | |
current_log.append(line) | |
g.es(f'Got {len(previous_text)} previous entries') | |
year_cache = dict() | |
month_cache = dict() | |
new_day_count = 0 | |
for date in date_list: | |
year_name = date.strftime("%Y") | |
if year_name in year_cache: | |
year_node = year_cache[year_name] | |
else: | |
year_node, new_year = generate_node(worklog, date.strftime("%Y")) | |
year_cache[year_name] = year_node | |
month_cache = dict() | |
month_name = date.strftime("%B") | |
if month_name in month_cache: | |
month_node = month_cache[month_name] | |
else: | |
month_node, new_month = generate_node(year_node, date.strftime("%B")) | |
month_cache[month_name] = month_node | |
day_name = date.strftime("%Y-%m-%d") | |
body = previous_text[day_name] if day_name in previous_text else '' | |
# is this a weekend? if we have no imported notes, do not create it | |
if date.weekday() < 5 or body: | |
day_node, new_day = generate_node(month_node, day_name, body_text=body) | |
if not new_day: | |
new_day_count += 1 | |
if new_day_count > 7: | |
g.es(f'Date already existed for entire week, breaking') | |
break | |
c.redraw() # redraw the nodes |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment