add error handling to jds

This commit is contained in:
Riley Housden 2022-06-30 00:24:59 -04:00
parent 67fd1b3cf4
commit cc6bbe937f
Signed by: InValidFire
GPG key ID: 0D6208F6DF56B4D8

View file

@ -1,8 +1,9 @@
"""Integrates Johnny Decimal Notation into Xonsh""" """Integrates Johnny Decimal Notation into Xonsh"""
from pathlib import Path
from xonsh.built_ins import XSH from xonsh.built_ins import XSH
HOME = p"/run/media/riley/drive" HOME = Path("/run/media/riley/drive")
def find_category(number: int, path, regex: str): def find_category(number: int, path, regex: str):
"""Find matching JDN folder when given notation""" """Find matching JDN folder when given notation"""
@ -25,17 +26,27 @@ def find_category(number: int, path, regex: str):
@XSH.builtins.events.on_transform_command @XSH.builtins.events.on_transform_command
def jds_preproc(cmd: str, **kw): def jds_preproc(cmd: str, **kw):
"""processes command strings starting with "`" to cd to matching folder.""" """processes command strings starting with "`" to cd to matching folder."""
if cmd.startswith("`") and "." in cmd: if cmd.startswith("`") and "." in cmd and not cmd.strip().endswith("`"):
jdn = cmd.replace("`", "") try:
bucket = find_category(int(jdn.split(".")[0]), HOME, r"(^\d0)-(\d9)") johnny_decimal_notation = cmd.replace("`", "")
category = find_category(int(jdn.split(".")[0]), bucket, r"^(\d\d)") bucket = find_category(int(johnny_decimal_notation.split(".")[0]), HOME, r"(^\d0)-(\d9)")
folder = find_category(int(jdn.split(".")[1]), category, r"^(\d\d)") category = find_category(int(johnny_decimal_notation.split(".")[0]), bucket, r"^(\d\d)")
cmd = f"cd '{folder.resolve()}'" folder = find_category(int(johnny_decimal_notation.split(".")[1]), category, r"^(\d\d)")
elif cmd.startswith("`"): cmd = f"cd '{folder.resolve()}'"
jdn = cmd.replace("`", "") except FileNotFoundError:
if jdn == "\n": cmd = f'print("\'{HOME}\' not found.")'
return f"cd '{HOME.resolve()}'" except ValueError:
bucket = find_category(int(jdn.split(".")[0]), HOME, r"(^\d0)-(\d9)") cmd = f'print("\'{cmd.strip().replace("`", "")}\' is not a valid JDN.")'
category = find_category(int(jdn.split(".")[0]), bucket, r"^(\d\d)") elif cmd.startswith("`") and not cmd.strip().endswith("`"):
cmd = f"cd '{category.resolve()}'" try:
johnny_decimal_notation = cmd.replace("`", "")
if johnny_decimal_notation == "\n":
return f"cd '{HOME.resolve()}'"
bucket = find_category(int(johnny_decimal_notation.split(".")[0]), HOME, r"(^\d0)-(\d9)")
category = find_category(int(johnny_decimal_notation.split(".")[0]), bucket, r"^(\d\d)")
cmd = f"cd '{category.resolve()}'"
except FileNotFoundError:
cmd = f'print("\'{HOME}\' not found.")'
except ValueError:
cmd = f'print("\'{cmd.strip().replace("`", "")}\' is not a valid JDN.")'
return cmd return cmd