add error handling to jds
This commit is contained in:
parent
67fd1b3cf4
commit
cc6bbe937f
1 changed files with 25 additions and 14 deletions
|
@ -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)")
|
||||||
|
folder = find_category(int(johnny_decimal_notation.split(".")[1]), category, r"^(\d\d)")
|
||||||
cmd = f"cd '{folder.resolve()}'"
|
cmd = f"cd '{folder.resolve()}'"
|
||||||
elif cmd.startswith("`"):
|
except FileNotFoundError:
|
||||||
jdn = cmd.replace("`", "")
|
cmd = f'print("\'{HOME}\' not found.")'
|
||||||
if jdn == "\n":
|
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()}'"
|
return f"cd '{HOME.resolve()}'"
|
||||||
bucket = find_category(int(jdn.split(".")[0]), HOME, r"(^\d0)-(\d9)")
|
bucket = find_category(int(johnny_decimal_notation.split(".")[0]), HOME, r"(^\d0)-(\d9)")
|
||||||
category = find_category(int(jdn.split(".")[0]), bucket, r"^(\d\d)")
|
category = find_category(int(johnny_decimal_notation.split(".")[0]), bucket, r"^(\d\d)")
|
||||||
cmd = f"cd '{category.resolve()}'"
|
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
|
||||||
|
|
Loading…
Reference in a new issue