Enhanced directory-creating, CSV-header initializing still-seems-to-work

This commit is contained in:
mlncn 2021-05-02 17:27:50 -04:00
parent fede7fabe2
commit 810712166d

View file

@ -1,5 +1,6 @@
import sys import sys
import csv import csv
import os
# This is a local script are we supposed to indicate that? # This is a local script are we supposed to indicate that?
import prompt_window import prompt_window
import time import time
@ -53,21 +54,26 @@ def quit_prompt():
def log_step(text, utc_start, end_section=False): def log_step(text, utc_start, end_section=False):
""" Write detailed log of all events """ Write detailed log of all events
""" """
timelog_file = Path(timelog_path(ext='log', daily=True)) timelog_file = Path(prepare_file(utc_start, ext='log', daily=True))
timelog_file.touch(exist_ok=True) timelog_file.touch(exist_ok=True)
with timelog_file.open('a') as timelog: with timelog_file.open('a') as timelog:
timelog.write(text + '\n') timelog.write(text + '\n')
if end_section: if end_section:
timelog.write('\n\n') timelog.write('\n\n')
def record_task(whatnext, whatdid, start, end=None): def record_task(whatnext, whatdid, start, end=None):
""" Record completed pomodoro to CSV """ Record completed pomodoro to CSV
""" """
if end is None: if end is None:
end = datetime.now(pytz.utc) end = datetime.now(pytz.utc)
with open(prepare_file(), 'a', newline='') as csvfile:
# TODO make first line started, recorded, description, intention filepath = prepare_file(start)
if not os.path.isfile(filepath):
with open(filepath, 'w', newline='') as csvfile:
log = csv.writer(csvfile)
log.writewrow(['started', 'recorded', 'description', 'intention'])
with open(filepath, 'a', newline='') as csvfile:
timewriter = csv.writer(csvfile) timewriter = csv.writer(csvfile)
timewriter.writerow([start, end, whatdid, whatnext]) timewriter.writerow([start, end, whatdid, whatnext])
@ -84,7 +90,7 @@ def countdown_to(until):
print('\r', str_minutes(to_go), sep='', end='') print('\r', str_minutes(to_go), sep='', end='')
time.sleep(1) time.sleep(1)
def prepare_file(ext='csv', daily=False, utc_start): def prepare_file(utc_start, ext='csv', daily=False):
# Ensure log directory exists. # Ensure log directory exists.
if not os.path.exists('log'): if not os.path.exists('log'):
os.makedirs('log') os.makedirs('log')
@ -148,6 +154,5 @@ def main():
record_task(whatnext, 'Incomplete, interrupted task:' + whatnext, start) record_task(whatnext, 'Incomplete, interrupted task:' + whatnext, start)
log_step('Incomplete, interrupted task:' + whatnext, start, True) log_step('Incomplete, interrupted task:' + whatnext, start, True)
if __name__ == '__main__': if __name__ == '__main__':
main() main()