From 978da38001024a7890d090651a5a788582127fff Mon Sep 17 00:00:00 2001 From: Marty Oehme Date: Mon, 1 Nov 2021 12:01:10 +0100 Subject: [PATCH] Fix file writing and replacement process --- jrnlwarrior.py | 28 +++++++++++++++------------- 1 file changed, 15 insertions(+), 13 deletions(-) diff --git a/jrnlwarrior.py b/jrnlwarrior.py index 59a7574..b73d1a9 100755 --- a/jrnlwarrior.py +++ b/jrnlwarrior.py @@ -35,6 +35,7 @@ def process_file(options): fname = options.jrnl_fname lines_to_delete = [] today_exists = False + curdate = "" with open(fname) as file: line_number = 0 for line in file: @@ -62,11 +63,11 @@ def process_file(options): if is_today(curdate): today_exists = True - if lines_to_delete: - delete_lines_from_file(fname, lines_to_delete, options.dryrun) + if lines_to_delete: + delete_lines_from_file(fname, lines_to_delete, options.dryrun) - if not today_exists and options.taskwarrior_fill_today: - add_today(fname, options) + if not today_exists and options.taskwarrior_fill_today: + add_today(fname, options) def handle_completed_tasks(line, date, options): @@ -190,22 +191,23 @@ def is_today(cur_date): def add_today(fname, options): - cmd = ["task", "+TODAY or +OVERDUE", "export"] + cmd = ["task", *options.taskwarrior_overrides, "+TODAY or +OVERDUE", "export"] + due_json = json.loads(subprocess.run(cmd,capture_output=True).stdout) + tasks = f"[{TODAY} 09:00] {options.todo_block_title}\n" - for task in json.loads(subprocess.check_output(cmd)): + for task in due_json: tasks += f"{task['description']}\n" + tasks += "\n" if options.dryrun: print(f"\nWRITING TODAY:\n{tasks}") return - repl_file = fname + ".bak" - with open(fname) as read_file, open(repl_file, "w") as write_file: - write_file.write(tasks + "\n") - for line in read_file: - write_file.write(line) - os.rename(repl_file, fname) - + repl_fname = fname + ".bak" + with open(fname, "r") as read_file, open(repl_fname, "w") as write_file: + write_file.write(tasks) + write_file.write(read_file.read()) + os.rename(repl_fname, fname) if __name__ == "__main__": process_file(opts.init())