Skip to content

Commit

Permalink
Use click for subcommands
Browse files Browse the repository at this point in the history
  • Loading branch information
mr-pennyworth committed Jun 23, 2024
1 parent 8c265ae commit a550d70
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 18 deletions.
4 changes: 2 additions & 2 deletions info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -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</string>
<key>scriptargtype</key>
<integer>1</integer>
Expand Down Expand Up @@ -552,7 +552,7 @@ mkdir -p "$alfred_workflow_data"
<string>loading...</string>
<key>script</key>
<string>mkdir -p "$alfred_workflow_data"
./python pyapp/BetterDict.py listUnimported "$alfred_workflow_data"</string>
./python pyapp/BetterDict.py list-unimported "$alfred_workflow_data"</string>
<key>scriptargtype</key>
<integer>1</integer>
<key>scriptfile</key>
Expand Down
61 changes: 45 additions & 16 deletions pyapp/BetterDict.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from struct import unpack
from subprocess import *

import click
import meilisearch
from bs4 import BeautifulSoup

Expand All @@ -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():
Expand Down Expand Up @@ -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()
6 changes: 6 additions & 0 deletions pyapp/alfred.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}"
)
1 change: 1 addition & 0 deletions pyapp/requirements.txt
Original file line number Diff line number Diff line change
@@ -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
Expand Down

0 comments on commit a550d70

Please sign in to comment.