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
1 changed files with 25 additions and 14 deletions

View File

@ -1,8 +1,9 @@
"""Integrates Johnny Decimal Notation into Xonsh"""
from pathlib import Path
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):
"""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
def jds_preproc(cmd: str, **kw):
"""processes command strings starting with "`" to cd to matching folder."""
if cmd.startswith("`") and "." in cmd:
jdn = cmd.replace("`", "")
bucket = find_category(int(jdn.split(".")[0]), HOME, r"(^\d0)-(\d9)")
category = find_category(int(jdn.split(".")[0]), bucket, r"^(\d\d)")
folder = find_category(int(jdn.split(".")[1]), category, r"^(\d\d)")
cmd = f"cd '{folder.resolve()}'"
elif cmd.startswith("`"):
jdn = cmd.replace("`", "")
if jdn == "\n":
return f"cd '{HOME.resolve()}'"
bucket = find_category(int(jdn.split(".")[0]), HOME, r"(^\d0)-(\d9)")
category = find_category(int(jdn.split(".")[0]), bucket, r"^(\d\d)")
cmd = f"cd '{category.resolve()}'"
if cmd.startswith("`") and "." in cmd and not cmd.strip().endswith("`"):
try:
johnny_decimal_notation = cmd.replace("`", "")
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)")
folder = find_category(int(johnny_decimal_notation.split(".")[1]), category, r"^(\d\d)")
cmd = f"cd '{folder.resolve()}'"
except FileNotFoundError:
cmd = f'print("\'{HOME}\' not found.")'
except ValueError:
cmd = f'print("\'{cmd.strip().replace("`", "")}\' is not a valid JDN.")'
elif cmd.startswith("`") and not cmd.strip().endswith("`"):
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