Skip to content

Commit 62ff890

Browse files
committed
Update [uuid]
1 parent 2bb97a2 commit 62ff890

File tree

8 files changed

+147
-151
lines changed

8 files changed

+147
-151
lines changed

afw.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ def afw_entry(workflow):
127127

128128

129129
@cli.command()
130-
@click.argument("query")
130+
@click.argument("query", default="")
131131
def call(query):
132132
# Create a global `Workflow3` object
133133
wf = Workflow3()
@@ -139,7 +139,7 @@ def call(query):
139139

140140
@cli.command()
141141
@click.argument("workflow_path")
142-
@click.argument("query")
142+
@click.argument("query", default="")
143143
def test(workflow_path: str, query: str):
144144
try:
145145
module = importlib.import_module(workflow_path, "main")

afw_runtime.py

Lines changed: 39 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
"ICON_WARNING",
5252
"ICON_WEB",
5353
"AFWResponse",
54+
"AFWException",
5455
"afw_responses_to_feedback",
5556
]
5657

@@ -62,11 +63,18 @@ class AFWResponse:
6263
"""
6364

6465
title: str
65-
arg: str
66-
6766
subtitle: str | None = None
68-
icon: str = ICON_NOTE
69-
valid: bool = False
67+
icon: str = ICON_INFO
68+
69+
arg: str | None = None
70+
valid: bool = True
71+
72+
def __post_init__(self):
73+
if self.arg is not None:
74+
return
75+
76+
if self.valid:
77+
self.arg = self.title
7078

7179

7280
def afw_responses_to_feedback(responses: list[AFWResponse]) -> list[dict]:
@@ -82,31 +90,27 @@ def afw_responses_to_feedback(responses: list[AFWResponse]) -> list[dict]:
8290
return result
8391

8492

93+
class AFWException(Exception):
94+
pass
95+
96+
8597
class AFWFuncAbc:
86-
_icon_success = ICON_INFO
87-
_icon_default = ICON_INFO
88-
_icon_error = ICON_ERROR
89-
_icon_tip = ICON_NOTE
98+
icon_info = ICON_INFO
99+
icon_note = ICON_NOTE
100+
icon_error = ICON_ERROR
101+
102+
responses: list[AFWResponse]
90103

91104
def __init__(self, args: list[str], logger) -> None:
92105
self.args = args
93106
self.logger = logger
107+
self.responses = list()
94108

95-
@property
96-
def _data_success(self) -> list:
97-
raise NotImplementedError
109+
def append_response(self, response: AFWResponse):
110+
self.responses.append(response)
98111

99-
@property
100-
def _data_defaulte(self) -> list:
101-
raise NotImplementedError
102-
103-
@property
104-
def _data_error(self) -> list:
105-
raise NotImplementedError
106-
107-
@property
108-
def _data_tips(self) -> list:
109-
raise NotImplementedError
112+
def append_responses(self, responses: list[AFWResponse]):
113+
self.responses += responses
110114

111115
@staticmethod
112116
def _afw_response_to_feedback_data(responses: list[AFWResponse]) -> list[dict]:
@@ -121,68 +125,22 @@ def _afw_response_to_feedback_data(responses: list[AFWResponse]) -> list[dict]:
121125

122126
return result
123127

124-
def get_feedback_success(self) -> list[dict]:
125-
responses = [
126-
AFWResponse(
127-
title=title,
128-
arg=title,
129-
subtitle=subtitle,
130-
icon=self._icon_success,
131-
valid=True,
132-
)
133-
for title, subtitle in self._data_success
134-
] + [
135-
AFWResponse(
136-
title=title,
137-
arg=title,
138-
subtitle=subtitle,
139-
icon=self._icon_tip,
140-
valid=False,
141-
)
142-
for title, subtitle in self._data_tips
143-
]
144-
return self._afw_response_to_feedback_data(responses)
145-
146-
def get_feedback_fail(self) -> list[dict]:
147-
responses = (
148-
[
149-
AFWResponse(
150-
title=title,
151-
arg=title,
152-
subtitle=subtitle,
153-
icon=self._icon_error,
154-
valid=False,
155-
)
156-
for title, subtitle in self._data_error
157-
]
158-
+ [
159-
AFWResponse(
160-
title=title,
161-
arg=title,
162-
subtitle=subtitle,
163-
icon=self._icon_default,
164-
valid=True,
165-
)
166-
for title, subtitle in self._data_defaulte
167-
]
168-
+ [
169-
AFWResponse(
170-
title=title,
171-
arg=title,
172-
subtitle=subtitle,
173-
icon=self._data_tips,
174-
valid=False,
175-
)
176-
for title, subtitle in self._data_tips
177-
]
178-
)
179-
return self._afw_response_to_feedback_data(responses)
128+
def get_reponse_error(self, message: str) -> AFWResponse:
129+
return AFWResponse(title=message, icon=self.icon_error, valid=False)
180130

181131
def _process(self) -> bool:
182132
raise NotImplementedError
183133

184134
def __call__(self) -> list[dict]:
185-
if self._process():
186-
return self.get_feedback_success()
135+
self.logger.debug(self.args)
136+
137+
try:
138+
self._process()
139+
except AFWException as e:
140+
self.append_response(self.get_reponse_error(e))
141+
return self._afw_response_to_feedback_data(self.responses)
142+
143+
if not self.responses:
144+
pass
187145

188-
return self.get_feedback_fail()
146+
return self._afw_response_to_feedback_data(self.responses)
File renamed without changes.

afw_uuid_tool/info.plist renamed to afw_uuid/info.plist

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<plist version="1.0">
44
<dict>
55
<key>bundleid</key>
6-
<string>com.rexzhang.alfred-workflows.uuid-tool</string>
6+
<string>com.rexzhang.alfred-workflows.uuid</string>
77
<key>connections</key>
88
<dict>
99
<key>19F264BF-E5DF-4AFC-A2AA-5E0B2967246B</key>
@@ -21,13 +21,13 @@
2121
</array>
2222
</dict>
2323
<key>createdby</key>
24-
<string></string>
24+
<string>Rex Zhang</string>
2525
<key>description</key>
2626
<string></string>
2727
<key>disabled</key>
2828
<false/>
2929
<key>name</key>
30-
<string>UUID Tool</string>
30+
<string>UUID</string>
3131
<key>objects</key>
3232
<array>
3333
<dict>
@@ -58,9 +58,7 @@
5858
<key>runningsubtext</key>
5959
<string></string>
6060
<key>script</key>
61-
<string>query=$1
62-
63-
python3 afw.py call $query</string>
61+
<string>python3 afw.py call $1</string>
6462
<key>scriptargtype</key>
6563
<integer>1</integer>
6664
<key>scriptfile</key>
@@ -72,7 +70,7 @@ python3 afw.py call $query</string>
7270
<key>type</key>
7371
<integer>5</integer>
7472
<key>withspace</key>
75-
<false/>
73+
<true/>
7674
</dict>
7775
<key>type</key>
7876
<string>alfred.workflow.input.scriptfilter</string>
@@ -123,6 +121,6 @@ python3 afw.py call $query</string>
123121
<key>userconfigurationconfig</key>
124122
<array/>
125123
<key>webaddress</key>
126-
<string></string>
124+
<string>https://github.com/rexzhang/alfred-workflows</string>
127125
</dict>
128126
</plist>

afw_uuid/main.py

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
import uuid
2+
3+
from afw_runtime import AFWFuncAbc, AFWResponse
4+
5+
6+
class AFWFunc(AFWFuncAbc):
7+
_data: uuid.UUID | None = None
8+
_tips = False
9+
10+
def make_responses_from_uuid(self, u: uuid.UUID) -> list[AFWResponse]:
11+
return [
12+
AFWResponse(
13+
title=str(u),
14+
subtitle="The UUID as a 32-character lowercase hexadecimal string with dash.",
15+
icon=self.icon_info,
16+
),
17+
AFWResponse(
18+
title=u.hex,
19+
subtitle="The UUID as a 32-character lowercase hexadecimal string.",
20+
icon=self.icon_info,
21+
),
22+
AFWResponse(
23+
title=str(u.int),
24+
subtitle="The UUID as a 128-bit integer.",
25+
icon=self.icon_info,
26+
),
27+
AFWResponse(
28+
title=str(u.urn),
29+
subtitle="The UUID as a URN as specified in RFC 4122.",
30+
icon=self.icon_info,
31+
),
32+
AFWResponse(
33+
title=f"{u.version}",
34+
subtitle="The UUID version number (1 through 5, specified is RFC_4122).",
35+
icon=self.icon_info,
36+
arg=4,
37+
),
38+
]
39+
40+
def append_tips(self):
41+
example1 = "uuid 4"
42+
example2 = "uuid 8d402c08-8155-11ee-9f73-a45e60bae6e9"
43+
self.responses += [
44+
AFWResponse(
45+
title="Example", subtitle=example1, icon=self.icon_note, arg=example1
46+
),
47+
AFWResponse(
48+
title="Example", subtitle=example2, icon=self.icon_note, arg=example2
49+
),
50+
]
51+
52+
def _guess_input(self) -> uuid.UUID | None:
53+
if len(self.args) == 0:
54+
self._tips = True
55+
query = "4"
56+
57+
else:
58+
query = self.args[0]
59+
60+
try:
61+
v = int(query)
62+
match v:
63+
case 1:
64+
u = uuid.uuid1()
65+
case 4:
66+
u = uuid.uuid4()
67+
case _:
68+
raise ValueError
69+
70+
return u
71+
72+
except ValueError:
73+
pass
74+
75+
try:
76+
u = uuid.UUID(query)
77+
except ValueError:
78+
return None
79+
80+
return u
81+
82+
def _createa_responses(self):
83+
if self._data:
84+
self.append_responses(self.make_responses_from_uuid(self._data))
85+
86+
if self._tips:
87+
self.append_tips()
88+
89+
def _process(self) -> None:
90+
self._data = self._guess_input()
91+
if self._data is None:
92+
self._tips = True
93+
94+
self._createa_responses()
95+
return
96+
97+
98+
def main(args: list[str], logger) -> list[AFWResponse]:
99+
return AFWFunc(args, logger)()
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
{
2-
"name": "uuid-tool",
2+
"name": "uuid",
33
"version": "0.1.0"
44
}
File renamed without changes.

afw_uuid_tool/main.py

Lines changed: 0 additions & 59 deletions
This file was deleted.

0 commit comments

Comments
 (0)