-
Notifications
You must be signed in to change notification settings - Fork 0
/
time.py
421 lines (376 loc) · 16.1 KB
/
time.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
import keypirinha as kp
import keypirinha_net as kpn
import keypirinha_util as kpu
import contextlib
import datetime
import json
import locale
import os
import re
import site
import sys
import traceback
import urllib
# insert lib directory to path to import modules normally
site.addsitedir(os.path.join(os.path.dirname(__file__), "lib"))
import dateutil.parser
import dateutil.zoneinfo
class Time(kp.Plugin):
DEFAULT_FORMATS = [
"%c",
"%x",
]
DEFAULT_LOCALES = [
"",
"C",
]
DEFAULT_ITEM_LABEL = "Time:"
DEFAULT_ITEM_LABEL2 = "Timezone:"
DEFAULT_ONLINE = True
COPY_TO_CB = "(press Enter to copy to clipboard)"
def __init__(self):
super().__init__()
self._formats = self.DEFAULT_FORMATS
self._locales = self.DEFAULT_LOCALES
self._item_label = self.DEFAULT_ITEM_LABEL
self._item_label2 = self.DEFAULT_ITEM_LABEL2
self._online = self.DEFAULT_ONLINE
self._urlopener = self._build_urlopener()
self._location_cache = {}
self._latlon_cache = {}
def on_start(self):
self._read_config()
self.set_default_icon(
self.load_icon("res://{}/clock.ico".format(self.package_full_name()))
)
def on_events(self, flags):
"""Reloads the package config when its changed"""
if flags & kp.Events.PACKCONFIG:
self._read_config()
self.on_catalog()
if flags & kp.Events.NETOPTIONS:
self.dbg("Network settings changed: rebuilding urlopener")
self._urlopener = self._build_urlopener()
def _read_config(self):
"""Reads the config"""
self.dbg("Reading config")
settings = self.load_settings()
self._debug = settings.get_bool("debug", "main", False)
self._formats = settings.get_multiline("formats", "main", self.DEFAULT_FORMATS)
self.dbg("Formats =", self._formats)
self._locales = settings.get_multiline(
"locales", "main", self.DEFAULT_LOCALES, True
)
self.dbg("Locales =", self._locales)
self._item_label = settings.get("item_label", "main", self.DEFAULT_ITEM_LABEL)
self.dbg("item_label =", self._item_label)
self._item_label2 = settings.get(
"item_label2", "main", self.DEFAULT_ITEM_LABEL2
)
self.dbg("item_label2 =", self._item_label2)
self._online = settings.get_bool("online", "main", self.DEFAULT_ONLINE)
self.dbg("online =", self._online)
def _build_urlopener(self):
"""Creates an urllib opener with some request headers and returns it"""
self.dbg("Building urlopener")
user_agent = "{}/{} python-{}/{}.{}.{}".format(
kp.name(),
kp.version_string(),
urllib.__name__,
sys.version_info[0],
sys.version_info[1],
sys.version_info[2],
)
opener = kpn.build_urllib_opener()
opener.addheaders = [("Accept-Encoding", "gzip"), ("User-Agent", user_agent)]
return opener
def on_catalog(self):
"""Adds the kill command to the catalog"""
# clear cache
self._location_cache = {}
self._latlon_cache = {}
catalog = [
self.create_item(
category=kp.ItemCategory.KEYWORD,
label=self._item_label,
short_desc="Date and time parsing and formatting",
target="time",
args_hint=kp.ItemArgsHint.REQUIRED,
hit_hint=kp.ItemHitHint.KEEPALL,
),
self.create_item(
category=kp.ItemCategory.KEYWORD,
label=self._item_label2,
short_desc="Current date in different timezones",
target="timezone",
args_hint=kp.ItemArgsHint.REQUIRED,
hit_hint=kp.ItemHitHint.KEEPALL,
),
]
self.set_catalog(catalog)
def _create_suggestions(self, timetoshow):
"""Creates various catalog items with different formats and locales for a given datetime object"""
suggestions = []
try:
suggestions.append(
self.create_item(
category=kp.ItemCategory.KEYWORD,
label=str(int(timetoshow.timestamp())),
short_desc="Time as unix timestamp (seconds since Jan 01 1970. (UTC)) {}".format(
self.COPY_TO_CB
),
target="timestamp_int",
args_hint=kp.ItemArgsHint.ACCEPTED,
hit_hint=kp.ItemHitHint.IGNORE,
loop_on_suggest=True,
)
)
suggestions.append(
self.create_item(
category=kp.ItemCategory.KEYWORD,
label=str(int(timetoshow.timestamp() * 1000)),
short_desc="Time as timestamp (milliseconds since Jan 01 1970. (UTC)) {}".format(
self.COPY_TO_CB
),
target="timestamp_float",
args_hint=kp.ItemArgsHint.ACCEPTED,
hit_hint=kp.ItemHitHint.IGNORE,
loop_on_suggest=True,
)
)
except OSError as ex:
self.dbg("Timestamp failed:", ex)
suggestions.append(
self.create_item(
category=kp.ItemCategory.KEYWORD,
label=timetoshow.isoformat(timespec="seconds"),
short_desc="Time in ISO 8601 format {}".format(self.COPY_TO_CB),
target="isoformat_s",
args_hint=kp.ItemArgsHint.ACCEPTED,
hit_hint=kp.ItemHitHint.IGNORE,
loop_on_suggest=True,
)
)
suggestions.append(
self.create_item(
category=kp.ItemCategory.KEYWORD,
label=timetoshow.isoformat(timespec="microseconds"),
short_desc="Time in ISO 8601 format {}".format(self.COPY_TO_CB),
target="isoformat_ms",
args_hint=kp.ItemArgsHint.ACCEPTED,
hit_hint=kp.ItemHitHint.IGNORE,
loop_on_suggest=True,
)
)
for idx, frmt in enumerate(self._formats):
for loc in self._locales:
try:
with self.__setlocale(loc):
item = self.create_item(
category=kp.ItemCategory.KEYWORD,
label=str(timetoshow.strftime(frmt)),
short_desc="Time in format '{}' in locale {} {}".format(
frmt, loc if loc else "system default", self.COPY_TO_CB
),
target="format_{}_{}".format(idx, loc),
args_hint=kp.ItemArgsHint.ACCEPTED,
hit_hint=kp.ItemHitHint.IGNORE,
loop_on_suggest=True,
)
if not self.__contains_item(suggestions, item):
suggestions.append(item)
except locale.Error as ex:
self.warn("Error with format ", frmt, "on locale", loc, ":", ex)
return suggestions
@staticmethod
def __contains_item(suggestions, search):
"""Checks if a catalog item with the same label is already in the collection"""
for item in suggestions:
if item.label() == search.label():
return True
return False
@contextlib.contextmanager
def __setlocale(self, name):
"""Sets the locale for time formatting functions to be used in a with statement.
:param name: See https://docs.microsoft.com/en-us/cpp/c-runtime-library/language-strings?view=vs-2017 for
possible values
"""
saved = locale.setlocale(locale.LC_TIME)
try:
yield locale.setlocale(locale.LC_TIME, name)
finally:
locale.setlocale(locale.LC_TIME, saved)
def on_suggest(self, user_input, items_chain):
if not items_chain:
return
if (len(items_chain) % 2 == 1 and items_chain[0].target() == "time") or (
len(items_chain) > 1
and len(items_chain) % 2 == 0
and items_chain[0].target() == "timezone"
):
timezone = items_chain[-1].target() if len(items_chain) > 1 else None
self.dbg("timezone", timezone)
if user_input:
try:
if int(user_input) < 86400:
self.dbg("Timestamps smaller than 86400 do not work.")
return
except ValueError as ex:
self.dbg("Input error: ", ex, "\n", traceback.format_exc())
parsed = self._tryparse(user_input)
self.dbg("parsed time", parsed)
if parsed is None:
return
if timezone:
timetoshow = parsed.replace(tzinfo=dateutil.tz.gettz(timezone))
else:
timetoshow = parsed.astimezone()
else:
if (
items_chain[0].target() == "time"
and len(items_chain) > 1
or items_chain[0].target() == "timezone"
and len(items_chain) > 2
):
time_wo_zone = self._tryparse(items_chain[-2].label())
else:
time_wo_zone = datetime.datetime.now()
if timezone:
timetoshow = time_wo_zone.astimezone(tz=dateutil.tz.gettz(timezone))
else:
timetoshow = time_wo_zone.astimezone()
self.dbg("timetoshow", timetoshow)
if timetoshow:
suggestions = self._create_suggestions(timetoshow)
self.set_suggestions(suggestions, kp.Match.ANY, kp.Sort.NONE)
elif (
(len(items_chain) % 2 == 1) and items_chain[0].target() == "timezone"
) or (
len(items_chain) > 1
and len(items_chain) % 2 == 0
and items_chain[0].target() == "time"
):
suggestions = []
for (
name,
timezone,
) in dateutil.zoneinfo.get_zonefile_instance().zones.items():
suggestions.append(
self.create_item(
category=kp.ItemCategory.KEYWORD,
label="{} ({})".format(
name.replace("_", " "),
datetime.datetime.utcnow()
.astimezone(tz=timezone)
.strftime("%z"),
),
short_desc="Time in timezone '{}'".format(name),
target=name,
args_hint=kp.ItemArgsHint.REQUIRED,
hit_hint=kp.ItemHitHint.IGNORE,
loop_on_suggest=True,
)
)
self.set_suggestions(suggestions)
if self._online and user_input and not self.should_terminate(0.5):
suggestions.extend(self._get_online_suggestions(user_input))
self.set_suggestions(suggestions)
def _get_online_suggestions(self, user_input):
"""Trys to search the user_input as location name and queries their respective timezones. Returns a list of
keypirinha suggestions"""
suggestions = []
if user_input in self._location_cache:
self.dbg("using cached results for input:", user_input)
results = self._location_cache[user_input]
else:
req = urllib.request.Request(
"https://nominatim.openstreetmap.org/search?format=json&q="
+ urllib.parse.quote_plus(user_input)
)
with self._urlopener.open(req) as resp:
if resp.info().get("Content-Encoding") == "gzip":
results = json.loads(gzip.decompress(resp.read()).decode())
else:
results = json.loads(resp.read().decode())
self.dbg("putting results in cache for input:", user_input)
self._location_cache[user_input] = results
self.dbg(results)
count = 0
for result in results:
if count >= 5:
break
lat = result["lat"]
lon = result["lon"]
display_name = result["display_name"]
name = result["name"]
if (lat, lon) in self._latlon_cache:
self.dbg("using cached results for lat/lon:", (lat, lon))
results2 = self._latlon_cache[(lat, lon)]
else:
req = urllib.request.Request(
"https://api.geotimezone.com/public/timezone?latitude="
+ lat
+ "&longitude="
+ lon
)
with self._urlopener.open(req) as resp:
if resp.info().get("Content-Encoding") == "gzip":
results2 = json.loads(gzip.decompress(resp.read()).decode())
else:
results2 = json.loads(resp.read().decode())
self.dbg("putting results in cache for lat/lon:", (lat, lon))
self._latlon_cache[(lat, lon)] = results2
self.dbg(results2)
tz = results2["iana_timezone"]
timezone = dateutil.tz.gettz(tz)
suggestions.append(
self.create_item(
category=kp.ItemCategory.KEYWORD,
label="{}: {} in timezone {} ({})".format(
user_input,
name,
tz,
datetime.datetime.utcnow()
.astimezone(tz=timezone)
.strftime("%z"),
),
short_desc="Time in '{}'".format(display_name),
target=tz,
args_hint=kp.ItemArgsHint.REQUIRED,
hit_hint=kp.ItemHitHint.IGNORE,
loop_on_suggest=True,
)
)
count += 1
return suggestions
def _tryparse(self, in_str):
"""Tries to parse a string into a datetime object"""
# Maybe its a timestamp
if re.match(r"^\d+$", in_str):
try:
return datetime.datetime.fromtimestamp(int(in_str))
except Exception as ex:
self.dbg("Parsing failed: ", ex, "\n", traceback.format_exc())
try:
return datetime.datetime.fromtimestamp(int(in_str) / 1000)
except Exception as ex:
self.dbg("Parsing failed: ", ex, "\n", traceback.format_exc())
if re.match(r"^\d+\.\d*$", in_str):
try:
return datetime.datetime.fromtimestamp(float(in_str))
except OSError as ex:
self.dbg("Parsing failed: ", ex, "\n", traceback.format_exc())
try:
return datetime.datetime.fromtimestamp(float(in_str) / 1000)
except OSError as ex:
self.dbg("Parsing failed: ", ex, "\n", traceback.format_exc())
# do your magic dateutil
try:
return dateutil.parser.parse(in_str)
except (ValueError, OverflowError) as ex:
self.dbg("Parsing failed: ", ex, "\n", traceback.format_exc())
return None
def on_execute(self, item, action):
"""Copies the item label to the clipboard"""
self.dbg("on_execute:", item.target())
kpu.set_clipboard(item.label())