-
Notifications
You must be signed in to change notification settings - Fork 0
/
time.py
275 lines (241 loc) · 11 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
import keypirinha as kp
import keypirinha_util as kpu
import datetime
import locale
import contextlib
import os
import re
import traceback
import site
# 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:"
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
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()
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)
def on_catalog(self):
"""Adds the kill command to the catalog
"""
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)
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())