-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcategorize_time.py
104 lines (81 loc) · 2.64 KB
/
categorize_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
from datetime import datetime
import paramparse
import win32clipboard
import pyperclip
def is_time(line):
time_found = 0
time_obj = None
try:
time_obj = datetime.strptime(line, '%I:%M:%S %p')
except ValueError:
try:
time_obj = datetime.strptime(line, '%I:%M %p')
except ValueError:
try:
time_obj = datetime.strptime(line, '%H:%M:%S')
except ValueError:
pass
# else:
# else:
# temp2 = line.split(' ')
# _time, _pm = temp2
# line = '{}:00 {}'.format(_time, _pm)
# time_found = 1
# else:
# time_found = 1
if time_obj is not None:
line = time_obj.strftime('%I:%M:%S %p')
time_found = 1
return line, time_found, time_obj
def main():
_params = {
'horz': 1,
'category': 2,
'category_sep': ' :: ',
'date_sep': ' – ',
'pairwise': 1,
'first_and_last': 0,
'add_date': 1,
'add_diff': 1,
}
paramparse.process_dict(_params)
category = _params['category']
category_sep = _params['category_sep']
try:
win32clipboard.OpenClipboard()
in_txt = win32clipboard.GetClipboardData() # type: str
except BaseException as e:
print('Tk().clipboard_get() failed: {}'.format(e))
win32clipboard.CloseClipboard()
return
else:
win32clipboard.CloseClipboard()
lines = in_txt.split('\n')
out_lines = []
for line in lines:
in_category = None
if category_sep in line:
temp = line.split(category_sep)
# print('line: {}'.format(line))
# print('temp: {}'.format(temp))
if len(temp) == 2:
line, in_category = temp
line = line.strip()
in_category = int(in_category.strip())
line, time_found, time_obj = is_time(line)
if time_found:
if in_category is not None and in_category != category:
print('replacing category {} with {} in {}'.format(in_category, category, line))
line = '{} :: {}'.format(line, category)
out_lines.append(line)
field_sep = '\n'
out_txt = field_sep.join(out_lines)
out_txt = out_txt.rstrip()
print('out_txt:\n{}'.format(out_txt))
try:
pyperclip.copy(out_txt)
spam = pyperclip.paste()
except BaseException as e:
print('Copying to clipboard failed: {}'.format(e))
if __name__ == '__main__':
main()