Skip to content

Commit 9e49a10

Browse files
committed
Update time convert
1 parent 7db732e commit 9e49a10

File tree

2 files changed

+44
-46
lines changed

2 files changed

+44
-46
lines changed

afw_time_converter/workflow_main.py renamed to afw_time_converter/main.py

Lines changed: 40 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
11
import re
22

33
import arrow
4-
from ualfred import ICON_CLOCK, ICON_ERROR, ICON_NOTE
5-
6-
DEFAULT_FEEDBACK = [
7-
{
8-
"title": 'Please enter timestamp, datetime string, "now", or space',
9-
"subtitle": "Examples: 1607609661, 2020-12-10 22:14:33, now",
10-
"valid": False,
11-
"icon": ICON_ERROR,
12-
},
13-
{
14-
"title": "Change time zone or time shift",
15-
"subtitle": "Examples: now +08, now +1d",
16-
"valid": False,
17-
"icon": ICON_ERROR,
18-
},
4+
from afw_runtime import *
5+
6+
DEFAULT_RESPONSE = [
7+
AFWResponse(
8+
title='Please enter timestamp, datetime string, "now", or space',
9+
arg="",
10+
subtitle="Examples: 1607609661, 2020-12-10 22:14:33, now",
11+
icon=ICON_HELP,
12+
),
13+
AFWResponse(
14+
title="Change time zone or time shift",
15+
arg="",
16+
subtitle="Examples: now +08, now +1d",
17+
icon=ICON_HELP,
18+
),
1919
]
2020

2121
SHIFT_UNIT_MAP = {
@@ -51,7 +51,6 @@
5151

5252

5353
class Time:
54-
wf = None
5554
_query: str = None
5655

5756
time = None
@@ -60,8 +59,9 @@ class Time:
6059
zone = None
6160
shift = None
6261

63-
def __init__(self, wf):
64-
self.wf = wf
62+
def __init__(self, args: list[str], logger):
63+
self.args = args
64+
self.logger = logger
6565

6666
@property
6767
def query(self):
@@ -71,15 +71,19 @@ def query(self):
7171
def query(self, value):
7272
self._query = value.strip(" ")
7373

74+
def __call__(self, *args, **kwargs):
75+
self.do_parser()
76+
return self.get_feedback()
77+
7478
def do_parser(self):
75-
query = self.wf.args[1]
76-
self.wf.logger.debug(f"query string:{type(query)} {query}")
79+
query = self.args[0]
80+
self.logger.debug(f"query string:{type(query)} {query}")
7781

7882
try:
7983
# self.query = self.wf.args[0].encode("utf8")
8084
self.query = query
8185
except IndexError:
82-
self.wf.logger.debug("parser workflow args failed.")
86+
self.logger.debug("parser workflow args failed.")
8387
return False
8488

8589
while True:
@@ -103,22 +107,22 @@ def _parser_extend_info(self):
103107
if info.upper() == "UTC" or info == "+00" or info == "-00":
104108
self.query = query
105109
self.zone = "UTC"
106-
self.wf.logger.debug(f"found zone info:{self.zone}")
110+
self.logger.debug(f"found zone info:{self.zone}")
107111
return True
108112

109113
r = re.match(RE_TIMEZONE, info)
110114
if r:
111115
self.query = query
112116
self.zone = info
113-
self.wf.logger.debug(f"found zone info:{self.zone}")
117+
self.logger.debug(f"found zone info:{self.zone}")
114118
return True
115119

116120
# time shift TODO
117121
r = re.match(RE_SHIFT, info)
118122
if r:
119123
self.query = query
120124
self.shift = info
121-
self.wf.logger.debug(f"found shift info:{self.shift}")
125+
self.logger.debug(f"found shift info:{self.shift}")
122126
return True
123127

124128
return False
@@ -141,7 +145,7 @@ def _parser_datetime(self):
141145
self.time = arrow.now()
142146
return True
143147

144-
self.wf.logger.debug(f"parser datetime error,query string:{self.query}")
148+
self.logger.debug(f"parser datetime error,query string:{self.query}")
145149
return False
146150

147151
def _apply_shift(self):
@@ -158,22 +162,22 @@ def _apply_shift(self):
158162

159163
def get_feedback(self):
160164
if self.time is None:
161-
return DEFAULT_FEEDBACK
165+
return DEFAULT_RESPONSE
162166

163167
if self.now:
164168
desc_now = "Now, "
165169
else:
166170
desc_now = ""
167171

168-
f = list()
172+
responses = list()
169173
for icon, force_utc, fmt, desc_format in FORMAT_LIST:
170174
# time shift
171175
if self.shift:
172176
desc_shift = f"{self.shift}, "
173177
else:
174178
desc_shift = ""
175179

176-
# time znone
180+
# time zone
177181
if force_utc:
178182
self.time = self.time.to("UTC")
179183
desc_zone = "UTC"
@@ -188,21 +192,15 @@ def get_feedback(self):
188192
subtitle = "{}{}[{}] {}".format(
189193
desc_now, desc_shift, desc_zone, desc_format
190194
)
191-
f.append(
192-
{
193-
"title": value,
194-
"subtitle": subtitle,
195-
"valid": True,
196-
"arg": value,
197-
"icon": icon,
198-
}
195+
responses.append(
196+
AFWResponse(
197+
title=value, arg=value, subtitle=subtitle, icon=icon, valid=True
198+
)
199199
)
200200

201-
return f
202-
201+
return responses
203202

204-
def call_workflow(wf):
205-
time = Time(wf)
206-
time.do_parser()
207203

208-
return time.get_feedback()
204+
def main(args: list[str], logger) -> list[AFWResponse]:
205+
time_convert = Time(args, logger)
206+
return time_convert()

afw_uuid_tool/main.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,19 @@ def main(args: list[str], logger) -> list[AFWResponse]:
1313
query = args[0]
1414
logger.debug(query)
1515

16-
response = list()
16+
responses = list()
1717
try:
1818
u = uuid.UUID(query)
1919
except ValueError as e:
2020
u = None
2121
v = str(uuid.uuid4()).upper()
22-
response += [
22+
responses += [
2323
AFWResponse(title=v, arg=v),
2424
AFWResponse(title=str(e), arg="", icon=ICON_ERROR),
2525
]
26-
response += DEFAULT_RESPONSE
26+
responses += DEFAULT_RESPONSE
2727

2828
if u is None:
29-
return response
29+
return responses
3030

3131
return [AFWResponse(title=str(u), arg=str(u), icon=ICON_NOTE, valid=True)]

0 commit comments

Comments
 (0)