Skip to content

Commit 7db732e

Browse files
committed
Add new workflow: UUID tools[WIP]
1 parent 592feeb commit 7db732e

File tree

9 files changed

+268
-12
lines changed

9 files changed

+268
-12
lines changed

afw.py

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,12 @@
1111
import click
1212
from ualfred import Workflow3
1313

14-
WORKFLOW_BASE_FILES = ["info.plist", "workflow_main.py", "__init__.py"]
15-
# AFW_ENTRY_POINT_FILE = "afw_entry_point.py"
16-
AFW_ENTRY_POINT_FILE = "afw.py"
17-
AFW_REQUIREMENTS = ["click"]
14+
from afw_runtime import afw_responses_to_feedback
15+
16+
AFW_RUNTIME_FILES = ["afw.py", "afw_runtime.py"]
17+
AFW_REQUIREMENTS = ["click", "ualfred"]
18+
19+
AFW_WORKFLOW_BASIC_FILES = ["info.plist", "main.py"]
1820

1921

2022
@click.group()
@@ -74,21 +76,22 @@ def build(workflow_path: str):
7476
package_file.write(file, arcname=arc_name)
7577

7678
print("Add workflow files...")
77-
workflow_files: list[str] = WORKFLOW_BASE_FILES
79+
workflow_files: list[str] = AFW_WORKFLOW_BASIC_FILES
7880
icon_file = package_info.get("icon")
7981
if icon_file:
8082
workflow_files.append(icon_file)
8183

8284
for file in workflow_files:
8385
package_file.write(workflow_path.joinpath(file), arcname=file)
8486

85-
package_file.write(AFW_ENTRY_POINT_FILE)
87+
for file in AFW_RUNTIME_FILES:
88+
package_file.write(file)
8689

8790
package_file.close()
8891
print(f"Create Alfred workflow[{package_path}] finished.")
8992

9093

91-
def main(workflow):
94+
def afw_entry(workflow):
9295
# The Workflow3 instance will be passed to the function
9396
# you call from `Workflow3.run`.
9497
# Not super useful, as the `wf` object created in
@@ -104,10 +107,13 @@ def main(workflow):
104107
# This is also necessary for "magic" arguments to work.
105108
# args = wf.args
106109

110+
args = [workflow.args[1]]
111+
107112
# Do stuff here ...
108-
from workflow_main import call_workflow
113+
from main import main as afw_workflow_main
109114

110-
feedback = call_workflow(workflow)
115+
responses = afw_workflow_main(args, workflow.logger)
116+
feedback = afw_responses_to_feedback(responses)
111117

112118
# Add an item to Alfred feedback
113119
# wf.add_item(u'Item title', u'Item subtitle')
@@ -128,7 +134,7 @@ def call(query):
128134
# Call your entry function via `Workflow3.run()` to enable its
129135
# helper functions, like exception catching, ARGV normalization,
130136
# magic arguments etc.
131-
sys.exit(wf.run(main))
137+
sys.exit(wf.run(afw_entry))
132138

133139

134140
@cli.command()

afw_runtime.py

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
import dataclasses
2+
from typing import Optional
3+
4+
from ualfred import (
5+
ICON_ACCOUNT,
6+
ICON_BURN,
7+
ICON_CLOCK,
8+
ICON_COLOR,
9+
ICON_COLOUR,
10+
ICON_EJECT,
11+
ICON_ERROR,
12+
ICON_FAVORITE,
13+
ICON_FAVOURITE,
14+
ICON_GROUP,
15+
ICON_HELP,
16+
ICON_HOME,
17+
ICON_INFO,
18+
ICON_NETWORK,
19+
ICON_NOTE,
20+
ICON_SETTINGS,
21+
ICON_SWIRL,
22+
ICON_SWITCH,
23+
ICON_SYNC,
24+
ICON_TRASH,
25+
ICON_USER,
26+
ICON_WARNING,
27+
ICON_WEB,
28+
)
29+
30+
__all__ = [
31+
"ICON_ACCOUNT",
32+
"ICON_BURN",
33+
"ICON_CLOCK",
34+
"ICON_COLOR",
35+
"ICON_COLOUR",
36+
"ICON_EJECT",
37+
"ICON_ERROR",
38+
"ICON_FAVORITE",
39+
"ICON_FAVOURITE",
40+
"ICON_GROUP",
41+
"ICON_HELP",
42+
"ICON_HOME",
43+
"ICON_INFO",
44+
"ICON_NETWORK",
45+
"ICON_NOTE",
46+
"ICON_SETTINGS",
47+
"ICON_SWIRL",
48+
"ICON_SWITCH",
49+
"ICON_SYNC",
50+
"ICON_TRASH",
51+
"ICON_USER",
52+
"ICON_WARNING",
53+
"ICON_WEB",
54+
"AFWResponse",
55+
"afw_responses_to_feedback",
56+
]
57+
58+
59+
@dataclasses.dataclass
60+
class AFWResponse:
61+
"""
62+
https://www.alfredapp.com/help/workflows/inputs/script-filter/json/
63+
"""
64+
65+
title: str
66+
arg: str
67+
68+
subtitle: Optional[str] = None
69+
icon: str = ICON_NOTE
70+
valid: bool = False
71+
72+
73+
def afw_responses_to_feedback(responses: list[AFWResponse]) -> list[dict]:
74+
result = list()
75+
for obj in responses:
76+
data = dataclasses.asdict(obj)
77+
for k in list(data.keys()):
78+
if data[k] is None:
79+
data.pop(k)
80+
81+
result.append(data)
82+
83+
return result
Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
1-
ualfred
21
arrow

afw_uuid_tool/__init__.py

Whitespace-only changes.

afw_uuid_tool/info.plist

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
3+
<plist version="1.0">
4+
<dict>
5+
<key>bundleid</key>
6+
<string>com.rexzhang.alfred-workflows.uuid-tool</string>
7+
<key>category</key>
8+
<string>Tools</string>
9+
<key>connections</key>
10+
<dict>
11+
<key>19F264BF-E5DF-4AFC-A2AA-5E0B2967246B</key>
12+
<array>
13+
<dict>
14+
<key>destinationuid</key>
15+
<string>D95F1A6B-C989-4DDD-9CB1-1F458A49495A</string>
16+
<key>modifiers</key>
17+
<integer>0</integer>
18+
<key>modifiersubtext</key>
19+
<string></string>
20+
<key>vitoclose</key>
21+
<false/>
22+
</dict>
23+
</array>
24+
</dict>
25+
<key>createdby</key>
26+
<string></string>
27+
<key>description</key>
28+
<string></string>
29+
<key>disabled</key>
30+
<false/>
31+
<key>name</key>
32+
<string>UUID Tool</string>
33+
<key>objects</key>
34+
<array>
35+
<dict>
36+
<key>config</key>
37+
<dict>
38+
<key>alfredfiltersresults</key>
39+
<false/>
40+
<key>alfredfiltersresultsmatchmode</key>
41+
<integer>0</integer>
42+
<key>argumenttreatemptyqueryasnil</key>
43+
<true/>
44+
<key>argumenttrimmode</key>
45+
<integer>0</integer>
46+
<key>argumenttype</key>
47+
<integer>0</integer>
48+
<key>escaping</key>
49+
<integer>102</integer>
50+
<key>keyword</key>
51+
<string>uuid</string>
52+
<key>queuedelaycustom</key>
53+
<integer>3</integer>
54+
<key>queuedelayimmediatelyinitially</key>
55+
<true/>
56+
<key>queuedelaymode</key>
57+
<integer>0</integer>
58+
<key>queuemode</key>
59+
<integer>1</integer>
60+
<key>runningsubtext</key>
61+
<string></string>
62+
<key>script</key>
63+
<string>query=$1
64+
65+
python3 afw.py call $query</string>
66+
<key>scriptargtype</key>
67+
<integer>1</integer>
68+
<key>scriptfile</key>
69+
<string></string>
70+
<key>subtext</key>
71+
<string></string>
72+
<key>title</key>
73+
<string></string>
74+
<key>type</key>
75+
<integer>5</integer>
76+
<key>withspace</key>
77+
<true/>
78+
</dict>
79+
<key>type</key>
80+
<string>alfred.workflow.input.scriptfilter</string>
81+
<key>uid</key>
82+
<string>19F264BF-E5DF-4AFC-A2AA-5E0B2967246B</string>
83+
<key>version</key>
84+
<integer>3</integer>
85+
</dict>
86+
<dict>
87+
<key>config</key>
88+
<dict>
89+
<key>autopaste</key>
90+
<false/>
91+
<key>clipboardtext</key>
92+
<string>aa {query}</string>
93+
<key>ignoredynamicplaceholders</key>
94+
<false/>
95+
<key>transient</key>
96+
<false/>
97+
</dict>
98+
<key>type</key>
99+
<string>alfred.workflow.output.clipboard</string>
100+
<key>uid</key>
101+
<string>D95F1A6B-C989-4DDD-9CB1-1F458A49495A</string>
102+
<key>version</key>
103+
<integer>3</integer>
104+
</dict>
105+
</array>
106+
<key>readme</key>
107+
<string></string>
108+
<key>uidata</key>
109+
<dict>
110+
<key>19F264BF-E5DF-4AFC-A2AA-5E0B2967246B</key>
111+
<dict>
112+
<key>xpos</key>
113+
<real>110</real>
114+
<key>ypos</key>
115+
<real>250</real>
116+
</dict>
117+
<key>D95F1A6B-C989-4DDD-9CB1-1F458A49495A</key>
118+
<dict>
119+
<key>xpos</key>
120+
<real>330</real>
121+
<key>ypos</key>
122+
<real>250</real>
123+
</dict>
124+
</dict>
125+
<key>userconfigurationconfig</key>
126+
<array/>
127+
<key>webaddress</key>
128+
<string></string>
129+
</dict>
130+
</plist>

afw_uuid_tool/main.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import uuid
2+
3+
from afw_runtime import *
4+
5+
DEFAULT_RESPONSE = [
6+
AFWResponse(title="Please enter a UUID like string", arg="", icon=ICON_HELP),
7+
AFWResponse(title="Examples: 1111", arg="", icon=ICON_HELP),
8+
]
9+
10+
11+
def main(args: list[str], logger) -> list[AFWResponse]:
12+
logger.debug("test..")
13+
query = args[0]
14+
logger.debug(query)
15+
16+
response = list()
17+
try:
18+
u = uuid.UUID(query)
19+
except ValueError as e:
20+
u = None
21+
v = str(uuid.uuid4()).upper()
22+
response += [
23+
AFWResponse(title=v, arg=v),
24+
AFWResponse(title=str(e), arg="", icon=ICON_ERROR),
25+
]
26+
response += DEFAULT_RESPONSE
27+
28+
if u is None:
29+
return response
30+
31+
return [AFWResponse(title=str(u), arg=str(u), icon=ICON_NOTE, valid=True)]

afw_uuid_tool/package.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"name": "uuid-tool",
3+
"version": "0.1.0"
4+
}

afw_uuid_tool/requirements.txt

Whitespace-only changes.

requirements.txt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1-
# dev
1+
# base
2+
ualfred
23
click
4+
5+
# dev
36
deptree
47

58
-r afw_time_converter/requirements.txt

0 commit comments

Comments
 (0)