diff --git a/info.plist b/info.plist index c1452b3..617da1f 100644 --- a/info.plist +++ b/info.plist @@ -460,7 +460,7 @@ if [ "$(confirm)" = "1" ]; then killall alfred-dict-server - ./python pyapp/BetterDict.py factoryReset "$alfred_workflow_data" + ./python pyapp/BetterDict.py factory-reset "$alfred_workflow_data" fi scriptargtype 1 @@ -552,7 +552,7 @@ mkdir -p "$alfred_workflow_data" loading... script mkdir -p "$alfred_workflow_data" -./python pyapp/BetterDict.py listUnimported "$alfred_workflow_data" +./python pyapp/BetterDict.py list-unimported "$alfred_workflow_data" scriptargtype 1 scriptfile diff --git a/pyapp/BetterDict.py b/pyapp/BetterDict.py index 795eb1d..fd80bad 100644 --- a/pyapp/BetterDict.py +++ b/pyapp/BetterDict.py @@ -13,6 +13,7 @@ from struct import unpack from subprocess import * +import click import meilisearch from bs4 import BeautifulSoup @@ -27,6 +28,8 @@ SEARCH_IP = os.environ.get("SEARCH_IP", "127.0.0.1") SEARCH_PORT = os.environ.get("SEARCH_PORT", "6789") WORKFLOW_DIR = alfred.get_workflow_dir() +WORKFLOW_ID = plist.read(f"{WORKFLOW_DIR}/info.plist")["bundleid"] +DEFAULT_WORKFLOW_DATA_DIR = alfred.default_workflow_data_dir(WORKFLOW_ID) def noop(): @@ -427,24 +430,50 @@ def list_unimported_dicts(import_base_dir): print(json.dumps(alfreditems, indent=2)) -def factory_reset(base_dir): - shutil.rmtree(base_dir) - shutil.copy(f"{WORKFLOW_DIR}/info.plist", f"{WORKFLOW_DIR}/info.plist.bkp") - shutil.copy(f"{WORKFLOW_DIR}/info.plist.orig", f"{WORKFLOW_DIR}/info.plist") +@click.group() +def main(): + pass -def fullpath(p): - return os.path.abspath(os.path.expanduser(p)) +@main.command() +@click.argument( + "workflow_data_dir", + type=click.Path(exists=True, file_okay=False, resolve_path=True), + envvar="alfred_workflow_data", + default=DEFAULT_WORKFLOW_DATA_DIR, +) +def list_unimported(workflow_data_dir: str): + list_unimported_dicts(workflow_data_dir) + + +@main.command(name="import") +@click.argument( + "dict_path", + type=click.Path(exists=True, file_okay=False, resolve_path=True), +) +@click.argument( + "workflow_data_dir", + type=click.Path(exists=True, file_okay=False, resolve_path=True), + envvar="alfred_workflow_data", + default=DEFAULT_WORKFLOW_DATA_DIR, +) +def import_(dict_path: str, workflow_data_dir: str): + import_dict(dict_path, workflow_data_dir) + + +@main.command() +@click.argument( + "workflow_data_dir", + type=click.Path(exists=True, file_okay=False, resolve_path=True), + envvar="alfred_workflow_data", + default=DEFAULT_WORKFLOW_DATA_DIR, +) +def factory_reset(workflow_data_dir: str): + shutil.rmtree(workflow_data_dir) + shutil.copy(f"{WORKFLOW_DIR}/info.plist", f"{WORKFLOW_DIR}/info.plist.bkp") + shutil.copy(f"{WORKFLOW_DIR}/info.plist.orig", f"{WORKFLOW_DIR}/info.plist") + os.mkdir(workflow_data_dir) if __name__ == "__main__": - command = sys.argv[1] - if command == "listUnimported": - base_dir = fullpath(sys.argv[2]) - list_unimported_dicts(base_dir) - elif command == "import": - dict_path, base_dir = map(fullpath, sys.argv[2:4]) - import_dict(dict_path, base_dir) - elif command == "factoryReset": - base_dir = fullpath(sys.argv[2]) - factory_reset(base_dir) + main() diff --git a/pyapp/alfred.py b/pyapp/alfred.py index 927f2b6..86cad6a 100644 --- a/pyapp/alfred.py +++ b/pyapp/alfred.py @@ -22,3 +22,9 @@ def get_workflow_dir(): return infer_workflow_dir() return f"{prefs_dir}/workflows/{workflow_uid}" + + +def default_workflow_data_dir(workflow_id): + return os.path.expanduser( + f"~/Library/Application Support/Alfred/Workflow Data/{workflow_id}" + ) diff --git a/pyapp/requirements.txt b/pyapp/requirements.txt index c54b62b..cde8d25 100644 --- a/pyapp/requirements.txt +++ b/pyapp/requirements.txt @@ -1,4 +1,5 @@ beautifulsoup4==4.8.2 +click==8.1.7 lxml==5.1.0 meilisearch==0.30.0 multiprocess==0.70.11.1