Skip to content

Commit

Permalink
Add dataclasses for alfred item
Browse files Browse the repository at this point in the history
  • Loading branch information
mr-pennyworth committed Jun 23, 2024
1 parent a550d70 commit 0d8b2b0
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 5 deletions.
16 changes: 11 additions & 5 deletions pyapp/BetterDict.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# -*- coding: utf-8 -*-

import dataclasses
import glob
import json
import os
Expand Down Expand Up @@ -408,8 +409,8 @@ def create_index(dict_id, db_path):
return index


def list_unimported_dicts(import_base_dir):
alfreditems = {"items": []}
def list_unimported_dicts(import_base_dir) -> list[alfred.Item]:
alfreditems = []

imported = []
imported_json_path = f"{import_base_dir}/imported.json"
Expand All @@ -424,10 +425,10 @@ def list_unimported_dicts(import_base_dir):
dict_name = get_dict_name(info)
if dict_name in imported:
continue
alfreditems["items"].append({"title": dict_name, "arg": dict_path})
alfreditems.append(alfred.Item(title=dict_name, arg=dict_path))
except Exception as e:
print(f'Unable to process "{dict_path}": {e}', file=sys.stderr)
print(json.dumps(alfreditems, indent=2))
return alfreditems


@click.group()
Expand All @@ -443,7 +444,12 @@ def main():
default=DEFAULT_WORKFLOW_DATA_DIR,
)
def list_unimported(workflow_data_dir: str):
list_unimported_dicts(workflow_data_dir)
unimported = list_unimported_dicts(workflow_data_dir)
print(
json.dumps(
{"items": [dataclasses.asdict(d) for d in unimported]}, indent=2
)
)


@main.command(name="import")
Expand Down
40 changes: 40 additions & 0 deletions pyapp/alfred.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
# -*- coding: utf-8 -*-

import json
import os
from dataclasses import dataclass, field
from typing import Optional, Dict


def infer_workflow_dir():
Expand Down Expand Up @@ -28,3 +31,40 @@ def default_workflow_data_dir(workflow_id):
return os.path.expanduser(
f"~/Library/Application Support/Alfred/Workflow Data/{workflow_id}"
)


@dataclass
class Mod:
subtitle: Optional[str] = None
arg: Optional[str] = None
valid: Optional[bool] = None
icon: Optional[Dict[str, str]] = field(default_factory=dict)


@dataclass
class Text:
copy: Optional[str] = None
largetype: Optional[str] = None


@dataclass
class Icon:
type: Optional[str] = None
path: Optional[str] = None


@dataclass
class Item(json.JSONEncoder):
"""https://www.alfredapp.com/help/workflows/inputs/script-filter/json/"""

title: str
uid: Optional[str] = None
subtitle: Optional[str] = None
arg: Optional[str] = None
autocomplete: Optional[str] = None
valid: Optional[bool] = None
match: Optional[str] = None
icon: Optional[Icon] = None
mods: Optional[Dict[str, Mod]] = field(default_factory=dict)
text: Optional[Text] = None
quicklookurl: Optional[str] = None

0 comments on commit 0d8b2b0

Please sign in to comment.