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 26, 2024
1 parent a550d70 commit f9d7906
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 5 deletions.
11 changes: 6 additions & 5 deletions pyapp/BetterDict.py
Original file line number Diff line number Diff line change
Expand Up @@ -408,8 +408,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 +424,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 +443,8 @@ 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": [d.as_dict() for d in unimported]}, indent=2))


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

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


def infer_workflow_dir():
Expand Down Expand Up @@ -28,3 +30,46 @@ 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:
"""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]] = None
text: Optional[Text] = None
quicklookurl: Optional[str] = None

def as_dict(self):
def exclude_unset_fields(d):
return {k: v for (k, v) in d if v is not None}

return asdict(self, dict_factory=exclude_unset_fields)

0 comments on commit f9d7906

Please sign in to comment.