Skip to content

Instantly share code, notes, and snippets.

@juzim
Last active May 8, 2019 12:12
Show Gist options
  • Save juzim/1c2a9dcce56ecd830fbd8c8988bc6341 to your computer and use it in GitHub Desktop.
Save juzim/1c2a9dcce56ecd830fbd8c8988bc6341 to your computer and use it in GitHub Desktop.
Script to shift tasklists in zim to the next workday. Completed or excluded tasks are ignored, all others are set to `[>]` on the last day and reset on the next.
#!/usr/bin/python
import sys
import datetime
import re
from pathlib import Path
file = Path(sys.argv[1])
date_parts = list(map(lambda x: x.replace('.txt', ''), file.parts[-3:]))
with Path('out').open('w+') as nf:
nf.write(sys.argv[1])
date = datetime.date(int(date_parts[0]), int(date_parts[1]), int(date_parts[2]))
base_path = file.parent.parent.parent
if date.isoweekday() in set((5, 6)):
next_day = date + datetime.timedelta(days=8 - date.isoweekday())
else:
next_day = date + datetime.timedelta(days=1)
print(f'input day: {date}')
print(f'output day: {next_day}')
next_file = (base_path / next_day.strftime('%Y/%m/%d')).with_suffix(file.suffix)
print(f'input file: {file}')
print(f'output file: {next_file}')
with file.open('r+') as f:
content = f.read()
if content.strip() == "":
raise Exception('File empty')
def filters(x):
return not re.match(r'\s*?[\w=].*?', x) and not re.match(r'\s*\[[x*]\].*', x)
next_content = """Content-Type: text/x-zim-wiki
Wiki-Format: zim 0.4
"""
next_content += f"====== {next_day.strftime('%A %d %B %Y')} ======"
next_content += "\n".join(filter(filters, content.split("\n"))).replace('[>]', '[ ]')
print("_______________________")
print(content)
print("\n ====> \n")
print(next_content)
print("_______________________")
print(f'writing to {next_file}')
with next_file.open('w+') as nf:
nf.write(next_content)
print('Saving updated day')
shift_content = content.replace('[ ]', '[>]')
f.seek(0)
f.write(shift_content)
f.truncate()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment