-
-
Notifications
You must be signed in to change notification settings - Fork 242
/
utils.py
402 lines (331 loc) · 11.7 KB
/
utils.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
import collections as co
import csv
import datetime
import itertools
import json
import operator
import os
import shutil
import tempfile
from io import StringIO
import click
import arrow
import watson as _watson
from .fullmoon import get_last_full_moon
from click.exceptions import UsageError
def create_watson():
return _watson.Watson(config_dir=os.environ.get('WATSON_DIR'))
def confirm_project(project, watson_projects):
"""
Ask user to confirm creation of a new project
'project' must be a string
'watson_projects' must be an interable.
Returns True on accept and raises click.exceptions.Abort on reject
"""
if project not in watson_projects:
msg = ("Project '%s' does not exist yet. Create it?"
% style('project', project))
click.confirm(msg, abort=True)
return True
def confirm_tags(tags, watson_tags):
"""
Ask user to confirm creation of new tags (each separately)
Both 'tags' and 'watson_tags" must be iterables.
Returns True if all accepted and raises click.exceptions.Abort on reject
"""
for tag in tags:
if tag not in watson_tags:
msg = "Tag '%s' does not exist yet. Create it?" % style('tag', tag)
click.confirm(msg, abort=True)
return True
def style(name, element):
def _style_tags(tags):
if not tags:
return ''
return '[{}]'.format(', '.join(
style('tag', tag) for tag in tags
))
def _style_short_id(id):
return style('id', id[:7])
formats = {
'project': {'fg': 'magenta'},
'tags': _style_tags,
'tag': {'fg': 'blue'},
'time': {'fg': 'green'},
'error': {'fg': 'red'},
'date': {'fg': 'cyan'},
'short_id': _style_short_id,
'id': {'fg': 'white'}
}
fmt = formats.get(name, {})
if isinstance(fmt, dict):
return click.style(element, **fmt)
else:
# The fmt might be a function if we need to do some computation
return fmt(element)
def format_timedelta(delta):
"""
Return a string roughly representing a timedelta.
"""
seconds = int(delta.total_seconds())
neg = seconds < 0
seconds = abs(seconds)
total = seconds
stems = []
if total >= 3600:
hours = seconds // 3600
stems.append('{}h'.format(hours))
seconds -= hours * 3600
if total >= 60:
mins = seconds // 60
stems.append('{:02}m'.format(mins))
seconds -= mins * 60
stems.append('{:02}s'.format(seconds))
return ('-' if neg else '') + ' '.join(stems)
def sorted_groupby(iterator, key, reverse=False):
"""
Similar to `itertools.groupby`, but sorts the iterator with the same
key first.
"""
return itertools.groupby(sorted(iterator, key=key, reverse=reverse), key)
def options(opt_list):
"""
Wrapper for the `value_proc` field in `click.prompt`, which validates
that the user response is part of the list of accepted responses.
"""
def value_proc(user_input):
if user_input in opt_list:
return user_input
else:
raise UsageError("Response should be one of [{}]".format(
','.join(str(x) for x in opt_list)))
return value_proc
def get_frame_from_argument(watson, arg):
"""
Get a frame from a command line argument which can either be a
position index (-1) or a frame id.
"""
# first we try to see if we are refering to a frame by
# its position (for example -2). We only take negative indexes
# as a positive index might also be an existing id
try:
index = int(arg)
if index < 0:
return watson.frames[index]
except IndexError:
raise click.ClickException(
style('error', "No frame found for index {}.".format(arg))
)
except (ValueError, TypeError):
pass
# if we didn't find a frame by position, we try by id
try:
return watson.frames[arg]
except KeyError:
raise click.ClickException("{} {}.".format(
style('error', "No frame found with id"),
style('short_id', arg))
)
def get_start_time_for_period(period):
# Using now() from datetime instead of arrow for mocking compatibility.
now = arrow.Arrow.fromdatetime(datetime.datetime.now())
date = now.date()
day = date.day
month = date.month
year = date.year
weekday = now.weekday()
if period == 'day':
start_time = arrow.Arrow(year, month, day)
elif period == 'week':
start_time = arrow.Arrow.fromdate(now.shift(days=-weekday).date())
elif period == 'month':
start_time = arrow.Arrow(year, month, 1)
elif period == 'luna':
start_time = get_last_full_moon(now)
elif period == 'year':
start_time = arrow.Arrow(year, 1, 1)
elif period == 'all':
# approximately timestamp `0`
start_time = arrow.Arrow(1970, 1, 1)
else:
raise ValueError('Unsupported period value: {}'.format(period))
return start_time
def apply_weekday_offset(start_time, week_start):
"""
Apply the offset required to move the start date `start_time` of a week
starting on Monday to that of a week starting on `week_start`.
"""
weekdays = dict(zip(
["monday", "tuesday", "wednesday", "thursday", "friday", "saturday",
"sunday"], range(0, 7)))
new_start = week_start.lower()
if new_start not in weekdays:
return start_time
now = datetime.datetime.now()
offset = weekdays[new_start] - 7 * (weekdays[new_start] > now.weekday())
return start_time.shift(days=offset)
def make_json_writer(func, *args, **kwargs):
"""
Return a function that receives a file-like object and writes the return
value of func(*args, **kwargs) as JSON to it.
"""
def writer(f):
dump = json.dumps(func(*args, **kwargs), indent=1, ensure_ascii=False)
f.write(dump)
return writer
def safe_save(path, content, ext='.bak'):
"""
Save given content to file at given path safely.
`content` may either be a (unicode) string to write to the file, or a
function taking one argument, a file object opened for writing. The
function may write (unicode) strings to the file object (but doesn't need
to close it).
The file to write to is created at a temporary location first. If there is
an error creating or writing to the temp file or calling `content`, the
destination file is left untouched. Otherwise, if all is well, an existing
destination file is backed up to `path` + `ext` (defaults to '.bak') and
the temporary file moved into its place.
"""
tmpfp = tempfile.NamedTemporaryFile(mode='w+', delete=False)
try:
with tmpfp:
if isinstance(content, str):
tmpfp.write(content)
else:
content(tmpfp)
except Exception:
try:
os.unlink(tmpfp.name)
except (IOError, OSError):
pass
raise
else:
if os.path.exists(path):
try:
os.unlink(path + ext)
except OSError:
pass
shutil.move(path, path + ext)
shutil.move(tmpfp.name, path)
def deduplicate(sequence):
"""
Return a list with all items of the input sequence but duplicates removed.
Leaves the input sequence unaltered.
"""
return [element
for index, element in enumerate(sequence)
if element not in sequence[:index]]
def parse_tags(values_list):
"""
Return a list of tags parsed from the input values list.
Find all the tags starting by a '+', even if there are spaces in them,
then strip each tag and filter out the empty ones
"""
return list(filter(None, map(operator.methodcaller('strip'), (
# We concatenate the word with the '+' to the following words
# not starting with a '+'
w[1:] + ' ' + ' '.join(itertools.takewhile(
lambda s: not s.startswith('+'), values_list[i + 1:]
))
for i, w in enumerate(values_list) if w.startswith('+')
)))) # pile of pancakes !
def frames_to_json(frames):
"""
Transform a sequence of frames into a JSON-formatted string.
Each frame object has an equivalent pair name/value in the JSON string,
except for 'updated_at', which is not included.
.. seealso:: :class:`Frame`
"""
log = [
co.OrderedDict([
('id', frame.id),
('start', frame.start.isoformat()),
('stop', frame.stop.isoformat()),
('project', frame.project),
('tags', frame.tags),
])
for frame in frames
]
return json.dumps(log, indent=4, sort_keys=True)
def frames_to_csv(frames):
"""
Transform a sequence of frames into a CSV-formatted string.
Each frame object has an equivalent pair name/value in the CSV string,
except for 'updated_at', which is not included.
.. seealso:: :class:`Frame`
"""
entries = [
co.OrderedDict([
('id', frame.id[:7]),
('start', frame.start.format('YYYY-MM-DD HH:mm:ss')),
('stop', frame.stop.format('YYYY-MM-DD HH:mm:ss')),
('project', frame.project),
('tags', ', '.join(frame.tags)),
])
for frame in frames
]
return build_csv(entries)
def build_csv(entries):
"""
Creates a CSV string from a list of dict objects.
The dictionary keys of the first item in the list are used as the header
row for the built CSV. All item's keys are supposed to be identical.
"""
if entries:
header = entries[0].keys()
else:
return ''
memfile = StringIO()
writer = csv.DictWriter(memfile, header, lineterminator=os.linesep)
writer.writeheader()
writer.writerows(entries)
output = memfile.getvalue()
memfile.close()
return output
def flatten_report_for_csv(report):
"""
Flattens the data structure returned by `watson.report()` for a csv export.
Dates are formatted in a way that Excel (default csv module dialect) can
handle them (i.e. YYYY-MM-DD HH:mm:ss).
The result is a list of dictionaries where each element can contain two
different things:
1. The total `time` spent in a project during the report interval. In this
case, the `tag` value will be empty.
2. The partial `time` spent in a tag and project during the report
interval. In this case, the `tag` value will contain a tag associated
with the project.
The sum of all elements where `tag` is empty corresponds to the total time
of the report.
"""
result = []
datetime_from = report['timespan']['from'].format('YYYY-MM-DD HH:mm:ss')
datetime_to = report['timespan']['to'].format('YYYY-MM-DD HH:mm:ss')
for project in report['projects']:
result.append({
'from': datetime_from,
'to': datetime_to,
'project': project['name'],
'tag': '',
'time': project['time']
})
for tag in project['tags']:
result.append({
'from': datetime_from,
'to': datetime_to,
'project': project['name'],
'tag': tag['name'],
'time': tag['time']
})
return result
def json_arrow_encoder(obj):
"""
Encodes Arrow objects for JSON output.
This function can be used with
`json.dumps(..., default=json_arrow_encoder)`, for example.
If the object is not an Arrow type, a TypeError is raised
:param obj: Object to encode
:return: JSON representation of Arrow object as defined by Arrow
"""
if isinstance(obj, arrow.Arrow):
return obj.for_json()
raise TypeError("Object {} is not JSON serializable".format(obj))