-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtl_sum.py
executable file
·185 lines (159 loc) · 5.8 KB
/
tl_sum.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
#!/usr/bin/python3
# Add a new task to the gtimelog log file.
# Propose a list of task if only the category is supplied
#
# Copyright (c) 2015 Canonical Ltd.
# Author: Louis Bouchard <[email protected]>
#
# This program is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; either version 2 of the License, or (at your
# option) any later version. See http://www.gnu.org/copyleft/gpl.html for
# the full text of the license.
import argparse
import re
import sys
import datetime
from os.path import expanduser
from gtimelog.timelog import TimeLog
virtual_midnight = datetime.time(2, 0)
LogFile = '%s/.local/share/gtimelog/timelog.txt' % expanduser("~")
Categories = {
'train': 'Mentoring / Edu / Training',
'meet': 'Meetings',
'doc': 'Documentation',
'pers': 'Personal management',
'pto': 'Paid Timeout',
'cp': 'Compute Team',
'dev': 'Non Compute development',
'dr': 'Compute Doctor',
'self': 'Self Training',
'help': 'Help Out La Maison',
}
ListLimit = 10
def print_categories():
for k in sorted(Categories):
print(k)
def print_tasks(category, **kwargs):
tasks = get_tasks(category)
if "escape" in kwargs and kwargs["escape"]:
new_tasks = [t.replace(" ", "\\ ") for t in tasks]
tasks = new_tasks
print("\n".join(tasks))
def show_help():
try:
# python3-prettytable will be used if installed
import prettytable
categories = prettytable.PrettyTable(["Key", "Description"],
sortby="Key", padding_width=1)
categories.align["Key"] = "l"
categories.align["Description"] = "l"
categories.padding_width = 1
for keys, description in Categories.items():
categories.add_row([keys, description])
print(categories)
except:
for keys, description in Categories.items():
print("%s : %s" % (keys, description))
def get_tasks(category):
cases = []
regex = re.compile(r'{}'.format(Categories[category]))
with open(LogFile, 'r') as timelog:
all_cases = timelog.readlines()
all_cases.reverse()
for line in all_cases:
if regex.findall(line):
case = regex.split(line)[-1].strip()
if case.lstrip(": ") not in cases:
cases.append(case.lstrip(": "))
return cases
def select_tasks(category):
cases = get_tasks(category)
mytask = 0
for I in cases:
if (cases.index(I) + 1) % ListLimit:
print("{}) {}".format(cases.index(I) + 1, I))
else:
# account for modulo = 0 item
print("{}) {}".format(cases.index(I) + 1, I))
if cases.index(I) + 1 < len(cases):
try:
mytask = input("Select task (0 to exit,<CR> to continue): "
)
if mytask == '0':
return(None, None)
if mytask == '' or not mytask.isdecimal():
continue
else:
mytask = int(mytask)
break
except KeyboardInterrupt:
print("Terminated\n")
sys.exit(1)
if not mytask:
try:
mytask = input("Select task (0 or <CR> to exit): ")
if mytask == '0' or mytask == '' or not mytask.isdecimal():
return(None, None)
else:
mytask = int(mytask)
except KeyboardInterrupt:
print("Terminated\n")
sys.exit(1)
if mytask > 0 and mytask <= len(cases):
return(category, cases[mytask - 1])
else:
print("Invalid task number")
return(None, None)
def task_summary(category, task=None):
total_delta = datetime.timedelta()
today = datetime.datetime.today()
epoch = datetime.datetime(1970, 10, 1)
Log = TimeLog(LogFile, virtual_midnight)
log_entries = Log.window_for(epoch, today)
entries, _ = log_entries.categorized_work_entries()
for (_, entry, entry_time) in entries[Categories[category] + ' ']:
if entry.lstrip() == task:
total_delta += entry_time
print("total time spent on %s : %d.%d" % (
task, int(total_delta.seconds / 3600) + int(total_delta.days * 24),
int(total_delta.seconds / 60 % 60)))
return 0
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('task', nargs='*',
help='category | category : task title')
parser.add_argument('-c', '--list-categories',
help='list available task categories',
action='store_true')
parser.add_argument('-t', '--list-tasks', nargs=1, metavar='CATEGORY',
help='list available tasks for a given category')
parser.add_argument('-r', '--raw',
help='produce raw output (without pretty formatting)',
action='store_true')
args = parser.parse_args()
if args.list_categories:
if args.raw:
print_categories()
else:
show_help()
sys.exit(0)
if args.list_tasks:
print_tasks(args.list_tasks[0], escape=args.raw)
sys.exit(0)
if args.task:
if len(args.task) == 1:
if args.task[0] == '?':
show_help()
sys.exit(0)
elif args.task[0] in Categories:
(category, task) = select_tasks(args.task[0])
if category:
task_summary(category, task)
else:
print("Unknown category : %s" % args.task[0])
else:
show_help()
else:
parser.print_help()
sys.exit(0)