fix: handle empty or unreadable task files

This commit is contained in:
Johann Dreo 2024-06-13 10:48:28 +02:00
commit ec2d50cd39

View file

@ -7,6 +7,7 @@ import json
import pytz import pytz
import queue import queue
import pathlib import pathlib
import logging
import textwrap import textwrap
import datetime import datetime
import humanize import humanize
@ -20,6 +21,7 @@ from rich.columns import Columns
error_codes = { error_codes = {
"NO_DATA_FILE": 100, "NO_DATA_FILE": 100,
"CANNOT_INIT": 200, "CANNOT_INIT": 200,
"NO_DATA": 300,
} }
@ -517,7 +519,7 @@ def get_data(taskfile, filter = None):
try: try:
jdata = json.loads(out) jdata = json.loads(out)
except json.decoder.JSONDecodeError as exc: except json.decoder.JSONDecodeError as exc:
print("ERROR:", exc.returncode, exc.output) print("ERROR:", exc)
else: else:
return jdata return jdata
@ -652,7 +654,7 @@ def upsearch(filename, at = pathlib.Path.cwd()):
while current != root: while current != root:
found = current / filename found = current / filename
if found.exists(): if os.access(found, os.R_OK) and found.exists():
return found return found
current = current.parent current = current.parent
@ -771,6 +773,9 @@ if __name__ == "__main__":
jdata = get_data(taskfile, filter) jdata = get_data(taskfile, filter)
else: else:
jdata = get_data(taskfile, filter = None) jdata = get_data(taskfile, filter = None)
if not jdata:
error("NO_DATA", f"Failed to get data from taskfile {taskfile}")
# If no explicit touch from an editing command, # If no explicit touch from an editing command,
# then just point out tasks matching the filter. # then just point out tasks matching the filter.
if not touched: if not touched:
@ -891,8 +896,11 @@ if __name__ == "__main__":
uprelp = pathlib.Path(os.path.relpath(task_dir.parent, cwd)) uprelp = pathlib.Path(os.path.relpath(task_dir.parent, cwd))
upreli = re.sub("\.\./*", "", str(uprelp)) upreli = re.sub("\.\./*", "", str(uprelp))
upjdata = get_data(uptaskfile) upjdata = get_data(uptaskfile)
console.print(w.rtext(f"{upreli} {uptaskfile.parent.name}/: ", swatch="parentdir"), end="") try:
console.print(w.rtext(f"{len(upjdata)} tasks", swatch="parentdir.tasks")) console.print(w.rtext(f"{upreli} {uptaskfile.parent.name}/: ", swatch="parentdir"), end="")
console.print(w.rtext(f"{len(upjdata)} tasks", swatch="parentdir.tasks"))
except:
pass
# Relative path to the directory holding the task files. # Relative path to the directory holding the task files.
rela = re.sub("\.\./*", "", str(relp)) rela = re.sub("\.\./*", "", str(relp))