Skip to content

Instantly share code, notes, and snippets.

@cactail
Last active February 1, 2018 21:25
Show Gist options
  • Save cactail/0781696ca149e7585d1184a27bc6fb19 to your computer and use it in GitHub Desktop.
Save cactail/0781696ca149e7585d1184a27bc6fb19 to your computer and use it in GitHub Desktop.
Helper to manage my daily task lists
import re
import sys
import os
from os.path import join, isfile
from datetime import date
TODOS_LOCATION = '/home/cactus/Org/todos'
ARCHIVE_LOCATION = '/home/cactus/Org/todos/archive'
WEEK_DAYS = {0: 'lun', 1: 'mar', 2: 'mié',
3: 'jue', 4: 'vie', 5: 'sáb', 6: 'dom'}
HEADER = "#+TODO: TODO(t) POSTPONED(p) RELOCATED(r) | DONE(d) CANCELLED(c)"
FILE_NAME_FORMAT = "todo-{}-{}-{}.org"
def get_postponed_by_date():
onlyfiles = (f for f in os.listdir(TODOS_LOCATION)
if isfile(join(TODOS_LOCATION, f)))
postponed_todos = dict()
for file_ in onlyfiles:
with open(join(TODOS_LOCATION, file_)) as f:
lines = f.readlines()
todo_date = lines[1].strip()
postponed_in_file = [line.strip()[13:]
for line in lines[3:]
if re.match(r'\** POSTPONED', line)]
if postponed_in_file:
postponed_todos[todo_date] = postponed_in_file
return postponed_todos
def pretty_print_postponed_by_date(postponed):
todo_number = 1
print("\x1b[1mTareas pospuestas\x1b[0m\n")
for k, v in postponed.items():
print(k)
for i, value in enumerate(v, todo_number):
print(" ({}) {}".format(i, value))
todo_number += len(v)
print("-" * 50)
def _get_enumerate_todos(postponed):
postponed_list = list()
for value in postponed.values():
postponed_list += value
return enumerate(postponed_list, 1)
def create_todo_file(chosen, enumerate_postponed):
today = date.today()
file_name = FILE_NAME_FORMAT.format(*today.isoformat().split('-')[::-1])
date_format = "<{}-{}-{} {}>".format(
*today.isoformat().split('-'), WEEK_DAYS[today.weekday()])
todos = "\n** TODO ".join((todo[1] for todo in enumerate_postponed
if todo[0] in chosen))
to_save = "{}\n{}\n\n** TODO {}".format(HEADER, date_format, todos)
with open(join(TODOS_LOCATION, file_name), 'w') as f:
f.write(to_save)
def update_previous_todo_files(chosen, postponed):
def _get_postponed_todos_to_relocate():
enumerate_postponed = _get_enumerate_todos(postponed)
postponed_to_relocate = {todo[1] for todo in enumerate_postponed
if todo[0] in chosen}
postponed_as_set = {k: set(v) for k, v in postponed.items()}
return {k: v.intersection(postponed_to_relocate)
for k, v in postponed_as_set.items()}
for k, v in _get_postponed_todos_to_relocate().items():
file_name = FILE_NAME_FORMAT.format(*k[:-5][1:].split('-')[::-1])
with open(join(TODOS_LOCATION, file_name), 'r+') as f:
readlines = f.readlines()
for todo in tuple(v):
string = "** POSTPONED {}\n".format(todo)
idx = readlines.index(string)
readlines[idx] = readlines[idx].replace(
"** POSTPONED", "** RELOCATED")
f.seek(0)
f.writelines(readlines)
def main():
try:
postponed = get_postponed_by_date()
pretty_print_postponed_by_date(postponed)
chosen = tuple(map(
int, input("Tareas para reubicar (por ej. 1,2,5): ").split(',')))
create_todo_file(chosen, _get_enumerate_todos(postponed))
print(":: Lista de TODOS creada")
update_previous_todo_files(chosen, postponed)
print(":: Todos previos actualizados")
except KeyboardInterrupt:
print()
sys.exit()
if __name__ == "__main__":
# Posibles mejoras
# 1-que el todo-list del dia se llame today.org
# 2-que avise cuando ya existe un todo-list en este dia
# Cambio algo?
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment