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
|
fname = options.jrnl_fname
|
||||||
lines_to_delete = []
|
lines_to_delete = []
|
||||||
today_exists = False
|
today_exists = False
|
||||||
|
curdate = ""
|
||||||
with open(fname) as file:
|
with open(fname) as file:
|
||||||
line_number = 0
|
line_number = 0
|
||||||
for line in file:
|
for line in file:
|
||||||
|
@ -62,11 +63,11 @@ def process_file(options):
|
||||||
if is_today(curdate):
|
if is_today(curdate):
|
||||||
today_exists = True
|
today_exists = True
|
||||||
|
|
||||||
if lines_to_delete:
|
if lines_to_delete:
|
||||||
delete_lines_from_file(fname, lines_to_delete, options.dryrun)
|
delete_lines_from_file(fname, lines_to_delete, options.dryrun)
|
||||||
|
|
||||||
if not today_exists and options.taskwarrior_fill_today:
|
if not today_exists and options.taskwarrior_fill_today:
|
||||||
add_today(fname, options)
|
add_today(fname, options)
|
||||||
|
|
||||||
|
|
||||||
def handle_completed_tasks(line, date, options):
|
def handle_completed_tasks(line, date, options):
|
||||||
|
@ -190,22 +191,23 @@ def is_today(cur_date):
|
||||||
|
|
||||||
|
|
||||||
def add_today(fname, options):
|
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"
|
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 += f"{task['description']}\n"
|
||||||
|
tasks += "\n"
|
||||||
|
|
||||||
if options.dryrun:
|
if options.dryrun:
|
||||||
print(f"\nWRITING TODAY:\n{tasks}")
|
print(f"\nWRITING TODAY:\n{tasks}")
|
||||||
return
|
return
|
||||||
|
|
||||||
repl_file = fname + ".bak"
|
repl_fname = fname + ".bak"
|
||||||
with open(fname) as read_file, open(repl_file, "w") as write_file:
|
with open(fname, "r") as read_file, open(repl_fname, "w") as write_file:
|
||||||
write_file.write(tasks + "\n")
|
write_file.write(tasks)
|
||||||
for line in read_file:
|
write_file.write(read_file.read())
|
||||||
write_file.write(line)
|
os.rename(repl_fname, fname)
|
||||||
os.rename(repl_file, fname)
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
process_file(opts.init())
|
process_file(opts.init())
|
||||||
|
|
Loading…
Reference in a new issue