Fix file writing and replacement process
This commit is contained in:
parent
ec5ff47449
commit
978da38001
1 changed files with 15 additions and 13 deletions
|
@ -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())
|
||||
|
|
Loading…
Reference in a new issue