Skip to content

Commit 414e88a

Browse files
committed
Rewrite [UUID tool]
1 parent f169000 commit 414e88a

File tree

4 files changed

+136
-33
lines changed

4 files changed

+136
-33
lines changed

afw.py

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
#!/usr/bin/env python
22

3+
import dataclasses
34
import importlib
45
import json
56
import subprocess
67
import sys
8+
from logging import Logger
79
from pathlib import Path
810
from uuid import uuid4
911
from zipfile import ZipFile
@@ -13,6 +15,8 @@
1315

1416
from afw_runtime import afw_responses_to_feedback
1517

18+
logger = Logger(__file__)
19+
1620
AFW_RUNTIME_FILES = ["afw.py", "afw_runtime.py"]
1721
AFW_REQUIREMENTS = ["click", "ualfred"]
1822

@@ -107,7 +111,7 @@ def afw_entry(workflow):
107111
# This is also necessary for "magic" arguments to work.
108112
# args = wf.args
109113

110-
args = [workflow.args[1]]
114+
args = workflow.args[1:]
111115

112116
# Do stuff here ...
113117
from main import main as afw_workflow_main
@@ -147,15 +151,7 @@ def test(workflow_path: str, query: str):
147151
print(e)
148152
exit(1)
149153

150-
from logging import Logger
151-
import dataclasses
152-
import json
153-
154-
logger = Logger(__file__)
155-
156154
wf = Workflow3()
157-
print(wf.args)
158-
print(wf.args[2:])
159155

160156
responses = module.main(args=wf.args[2:], logger=logger)
161157
for response in responses:

afw_runtime.py

Lines changed: 79 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ class AFWResponse:
6565
title: str
6666
arg: str
6767

68-
subtitle: Optional[str] = None
68+
subtitle: str | None = None
6969
icon: str = ICON_NOTE
7070
valid: bool = False
7171

@@ -81,3 +81,81 @@ def afw_responses_to_feedback(responses: list[AFWResponse]) -> list[dict]:
8181
result.append(data)
8282

8383
return result
84+
85+
86+
class AFWFuncAbc:
87+
def __init__(self, args: list[str], logger) -> None:
88+
self.args = args
89+
self.logger = logger
90+
91+
@property
92+
def _data_defaulte(self) -> list:
93+
raise NotImplementedError
94+
95+
@property
96+
def _data_tips(self) -> list:
97+
raise NotImplementedError
98+
99+
@property
100+
def _data_success(self) -> list:
101+
raise NotImplementedError
102+
103+
@property
104+
def _data_error(self) -> list:
105+
raise NotImplementedError
106+
107+
def get_response_success(self) -> list[AFWResponse]:
108+
return [
109+
AFWResponse(
110+
title=title, arg=title, subtitle=subtitle, icon=ICON_INFO, valid=True
111+
)
112+
for title, subtitle in self._data_success
113+
] + [
114+
AFWResponse(
115+
title=title, arg=title, subtitle=subtitle, icon=ICON_NOTE, valid=False
116+
)
117+
for title, subtitle in self._data_tips
118+
]
119+
120+
def get_response_fail(self) -> list[AFWResponse]:
121+
return (
122+
[
123+
AFWResponse(
124+
title=title,
125+
arg=title,
126+
subtitle=subtitle,
127+
icon=ICON_ERROR,
128+
valid=False,
129+
)
130+
for title, subtitle in self._data_error
131+
]
132+
+ [
133+
AFWResponse(
134+
title=title,
135+
arg=title,
136+
subtitle=subtitle,
137+
icon=ICON_INFO,
138+
valid=True,
139+
)
140+
for title, subtitle in self._data_defaulte
141+
]
142+
+ [
143+
AFWResponse(
144+
title=title,
145+
arg=title,
146+
subtitle=subtitle,
147+
icon=ICON_NOTE,
148+
valid=False,
149+
)
150+
for title, subtitle in self._data_tips
151+
]
152+
)
153+
154+
def _process(self) -> bool:
155+
raise NotImplementedError
156+
157+
def __call__(self) -> list[AFWResponse]:
158+
if self._process():
159+
return self.get_response_success()
160+
161+
return self.get_response_fail()

afw_uuid_tool/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
from .main import main

afw_uuid_tool/main.py

Lines changed: 51 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,59 @@
11
import uuid
22

3-
from afw_runtime import *
3+
from afw_runtime import AFWFuncAbc, AFWResponse
44

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-
]
95

6+
class AFWFunc(AFWFuncAbc):
7+
_u: uuid.UUID
8+
_message_error: str
109

11-
def main(args: list[str], logger) -> list[AFWResponse]:
12-
logger.debug("test..")
13-
query = args[0]
14-
logger.debug(query)
15-
16-
responses = list()
17-
try:
18-
u = uuid.UUID(query)
19-
except ValueError as e:
20-
u = None
21-
v = str(uuid.uuid4()).upper()
22-
responses += [
23-
AFWResponse(title=v, arg=v, valid=True),
24-
AFWResponse(title=str(e), arg="", icon=ICON_ERROR),
10+
@staticmethod
11+
def _make_data_from_uuid(u: uuid.UUID) -> list[AFWResponse]:
12+
return (
13+
(u.hex, "32-character lowercase hexadecimal string."),
14+
(str(u), "32-character lowercase hexadecimal string with dash."),
15+
# (str(u).upper(), ""),
16+
(str(u.int), "The UUID as a 128-bit integer."),
17+
(str(u.urn), "The UUID as a URN as specified in RFC 4122."),
18+
(
19+
str(u.version),
20+
"The UUID version number (1 through 5, specified is RFC_4122).",
21+
),
22+
)
23+
24+
@property
25+
def _data_defaulte(self) -> list:
26+
u = uuid.uuid4()
27+
return self._make_data_from_uuid(u)
28+
29+
@property
30+
def _data_tips(self) -> list:
31+
return list()
32+
33+
@property
34+
def _data_success(self) -> list:
35+
return self._make_data_from_uuid(self._u)
36+
37+
@property
38+
def _data_error(self) -> list:
39+
return [
40+
(self._message_error, ""),
41+
# ("Please enter a UUID like string", ""),
42+
# ("Examples: 1111", ""),
2543
]
26-
responses += DEFAULT_RESPONSE
2744

28-
if u is None:
29-
return responses
45+
def _process(self) -> list[AFWResponse]:
46+
try:
47+
if len(self.args) == 0:
48+
raise ValueError(" Missing argument 'QUERY'")
3049

31-
return [AFWResponse(title=str(u), arg=str(u), icon=ICON_NOTE, valid=True)]
50+
self._u = uuid.UUID(self.args[0])
51+
return True
52+
53+
except ValueError as e:
54+
self._message_error = str(e)
55+
return False
56+
57+
58+
def main(args: list[str], logger) -> list[AFWResponse]:
59+
return AFWFunc(args, logger)()

0 commit comments

Comments
 (0)