Skip to content

Commit 2bb97a2

Browse files
committed
Refactor
1 parent baca625 commit 2bb97a2

File tree

5 files changed

+65
-37
lines changed

5 files changed

+65
-37
lines changed

afw.py

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

3-
import dataclasses
43
import importlib
54
import json
65
import subprocess
@@ -13,8 +12,6 @@
1312
import click
1413
from ualfred import Workflow3
1514

16-
from afw_runtime import afw_responses_to_feedback
17-
1815
logger = Logger(__file__)
1916

2017
AFW_RUNTIME_FILES = ["afw.py", "afw_runtime.py"]
@@ -116,8 +113,7 @@ def afw_entry(workflow):
116113
# Do stuff here ...
117114
from main import main as afw_workflow_main
118115

119-
responses = afw_workflow_main(args, workflow.logger)
120-
feedback = afw_responses_to_feedback(responses)
116+
feedback = afw_workflow_main(args, workflow.logger)
121117

122118
# Add an item to Alfred feedback
123119
# wf.add_item(u'Item title', u'Item subtitle')
@@ -153,9 +149,8 @@ def test(workflow_path: str, query: str):
153149

154150
wf = Workflow3()
155151

156-
responses = module.main(args=wf.args[2:], logger=logger)
157-
for response in responses:
158-
print(json.dumps(dataclasses.asdict(response), indent=4))
152+
feedback = module.main(args=wf.args[2:], logger=logger)
153+
print(json.dumps(feedback, indent=4))
159154

160155

161156
if __name__ == "__main__":

afw_runtime.py

Lines changed: 44 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import dataclasses
2-
from typing import Optional
32

43
from ualfred import (
54
ICON_ACCOUNT,
@@ -84,47 +83,74 @@ def afw_responses_to_feedback(responses: list[AFWResponse]) -> list[dict]:
8483

8584

8685
class AFWFuncAbc:
86+
_icon_success = ICON_INFO
87+
_icon_default = ICON_INFO
88+
_icon_error = ICON_ERROR
89+
_icon_tip = ICON_NOTE
90+
8791
def __init__(self, args: list[str], logger) -> None:
8892
self.args = args
8993
self.logger = logger
9094

9195
@property
92-
def _data_defaulte(self) -> list:
96+
def _data_success(self) -> list:
9397
raise NotImplementedError
9498

9599
@property
96-
def _data_tips(self) -> list:
100+
def _data_defaulte(self) -> list:
97101
raise NotImplementedError
98102

99103
@property
100-
def _data_success(self) -> list:
104+
def _data_error(self) -> list:
101105
raise NotImplementedError
102106

103107
@property
104-
def _data_error(self) -> list:
108+
def _data_tips(self) -> list:
105109
raise NotImplementedError
106110

107-
def get_response_success(self) -> list[AFWResponse]:
108-
return [
111+
@staticmethod
112+
def _afw_response_to_feedback_data(responses: list[AFWResponse]) -> list[dict]:
113+
result = list()
114+
for obj in responses:
115+
data = dataclasses.asdict(obj)
116+
for k in list(data.keys()):
117+
if data[k] is None:
118+
data.pop(k)
119+
120+
result.append(data)
121+
122+
return result
123+
124+
def get_feedback_success(self) -> list[dict]:
125+
responses = [
109126
AFWResponse(
110-
title=title, arg=title, subtitle=subtitle, icon=ICON_INFO, valid=True
127+
title=title,
128+
arg=title,
129+
subtitle=subtitle,
130+
icon=self._icon_success,
131+
valid=True,
111132
)
112133
for title, subtitle in self._data_success
113134
] + [
114135
AFWResponse(
115-
title=title, arg=title, subtitle=subtitle, icon=ICON_NOTE, valid=False
136+
title=title,
137+
arg=title,
138+
subtitle=subtitle,
139+
icon=self._icon_tip,
140+
valid=False,
116141
)
117142
for title, subtitle in self._data_tips
118143
]
144+
return self._afw_response_to_feedback_data(responses)
119145

120-
def get_response_fail(self) -> list[AFWResponse]:
121-
return (
146+
def get_feedback_fail(self) -> list[dict]:
147+
responses = (
122148
[
123149
AFWResponse(
124150
title=title,
125151
arg=title,
126152
subtitle=subtitle,
127-
icon=ICON_ERROR,
153+
icon=self._icon_error,
128154
valid=False,
129155
)
130156
for title, subtitle in self._data_error
@@ -134,7 +160,7 @@ def get_response_fail(self) -> list[AFWResponse]:
134160
title=title,
135161
arg=title,
136162
subtitle=subtitle,
137-
icon=ICON_INFO,
163+
icon=self._icon_default,
138164
valid=True,
139165
)
140166
for title, subtitle in self._data_defaulte
@@ -144,18 +170,19 @@ def get_response_fail(self) -> list[AFWResponse]:
144170
title=title,
145171
arg=title,
146172
subtitle=subtitle,
147-
icon=ICON_NOTE,
173+
icon=self._data_tips,
148174
valid=False,
149175
)
150176
for title, subtitle in self._data_tips
151177
]
152178
)
179+
return self._afw_response_to_feedback_data(responses)
153180

154181
def _process(self) -> bool:
155182
raise NotImplementedError
156183

157-
def __call__(self) -> list[AFWResponse]:
184+
def __call__(self) -> list[dict]:
158185
if self._process():
159-
return self.get_response_success()
186+
return self.get_feedback_success()
160187

161-
return self.get_response_fail()
188+
return self.get_feedback_fail()

afw_time_converter/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
from .workflow_main import call_workflow
1+
from .main import main

afw_time_converter/main.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,14 @@
11
import re
22

33
import arrow
4-
from afw_runtime import *
4+
5+
from afw_runtime import (
6+
ICON_CLOCK,
7+
ICON_HELP,
8+
ICON_NOTE,
9+
AFWResponse,
10+
afw_responses_to_feedback,
11+
)
512

613
DEFAULT_RESPONSE = [
714
AFWResponse(
@@ -202,5 +209,4 @@ def get_feedback(self):
202209

203210

204211
def main(args: list[str], logger) -> list[AFWResponse]:
205-
time_convert = Time(args, logger)
206-
return time_convert()
212+
return afw_responses_to_feedback(Time(args, logger)())

afw_uuid_tool/main.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,19 +21,15 @@ def _make_data_from_uuid(u: uuid.UUID) -> list[AFWResponse]:
2121
),
2222
)
2323

24+
@property
25+
def _data_success(self) -> list:
26+
return self._make_data_from_uuid(self._u)
27+
2428
@property
2529
def _data_defaulte(self) -> list:
2630
u = uuid.uuid4()
2731
return self._make_data_from_uuid(u)
2832

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-
3733
@property
3834
def _data_error(self) -> list:
3935
return [
@@ -42,6 +38,10 @@ def _data_error(self) -> list:
4238
# ("Examples: 1111", ""),
4339
]
4440

41+
@property
42+
def _data_tips(self) -> list:
43+
return list()
44+
4545
def _process(self) -> list[AFWResponse]:
4646
try:
4747
if len(self.args) == 0:

0 commit comments

Comments
 (0)