-
Notifications
You must be signed in to change notification settings - Fork 1
/
time_capture.py
executable file
·248 lines (173 loc) · 7.06 KB
/
time_capture.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
#!/usr/bin/env python3
# Copyright 2019 Michael Köcher
# 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 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
'''Calculates the work time from a given start time and breaks.
Needs to be called cyclical to be able to update the end time of the work.
The times of breaks and targets can be configured in the json file
that is created on the first run.
Writes a log entry for each day into a csv file for each month
(this can be disabled in the json file).'''
import argparse
import datetime
import json
import os
def update(path, now):
'''Updates the stash file and output with the current time'''
# load stash file
stash, stash_file_path = _get_stash(path, now)
# date changed?
if now.date() != stash['work']['start'].date():
if stash['log']:
_write_log(path, stash)
stash['work']['start'] = now
stash['work']['end'] = now
# write stash to file
stash_file = open(stash_file_path, 'w')
json.dump(stash, stash_file, default=_datetime_to_string, indent=2)
stash_file.close()
# calculate presence
presence = get_presence(stash['work'], stash['breaks'])
presence_str = get_hour_minute_str(presence)
print('Beginn: {: >10}'.format(stash['work']['start'].strftime('%H:%M')))
print('Anwesenheit: {0: >5s} h'.format(presence_str))
print_target_times(stash)
def _get_stash(path, now):
'''load the stash file'''
try:
stash_file_path = os.path.join(path, 'timeStash.json')
with open(stash_file_path, 'r') as stash_file:
stash = json.load(
stash_file, object_pairs_hook=_string_to_datetime)
except IOError:
stash = _init_stash(now)
return (stash, stash_file_path)
def _init_stash(now):
'''initialize stash file'''
work = dict()
work['start'] = now
work['end'] = now
stash = dict()
stash['log'] = True
stash['work'] = work
stash['breaks'] = [{'start': datetime.time(9),
'end': datetime.time(9, 15)},
{'start': datetime.time(12, 30),
'end': datetime.time(13)}]
stash['targets'] = [480, 600]
return stash
def _datetime_to_string(obj):
'''converts datetime.datetime and datetime.time objects to strings'''
if isinstance(obj, datetime.datetime):
value = obj.strftime('%Y-%m-%dT%H:%M')
elif isinstance(obj, datetime.time):
value = obj.strftime('%H:%M')
else:
raise TypeError
return value
def _string_to_datetime(obj_list):
'''converts datetime.datetime and datetime.time strings
in given key value list to datetime objects'''
new_dict = dict()
for key, value in obj_list:
new_value = value
if isinstance(value, str):
try:
new_value = datetime.datetime.strptime(value, '%Y-%m-%dT%H:%M')
except ValueError:
try:
new_value = datetime.time.fromisoformat(value)
except ValueError:
# if value is not a timestring do nothing
pass
new_dict[key] = new_value
return new_dict
def _write_log(path, stash):
'''writes a new log entry to the logfile'''
# calculate attendance of last day
presence = get_presence(stash['work'], stash['breaks'])
presence_str = get_hour_minute_str(presence)
file_name = stash['work']['start'].strftime('%Y_%m.csv')
with open(os.path.join(path, file_name), 'a') as log_file:
log_file.write(stash['work']['start'].strftime('%d.%m.%Y;%H:%M;')
+ stash['work']['end'].strftime('%H:%M;')
+ presence_str
+ '\n')
def calc_time_overlap(first, second):
'''calculates the overlap between two timespans
first needs to be of type datetime
second may be of type datetime or time'''
second = _set_dict_to_date(first['start'].date(), second)
overlap_start = max(first['start'], second['start'])
overlap_end = min(first['end'], second['end'])
overlap = datetime.timedelta()
if overlap_end > overlap_start:
overlap = overlap_end - overlap_start
return overlap
def get_breaks_duration(work, breaks):
'''calculates the overall duration of the given breaks during the given
worktime'''
breaks_duration = datetime.timedelta()
for single_break in breaks:
breaks_duration += calc_time_overlap(work, single_break)
return breaks_duration
def get_presence(work, breaks):
'''calculates the presence for given work and break times'''
return work['end'] - work['start'] - get_breaks_duration(work, breaks)
def get_hour_minute_str(timedelta):
'''returns a string of format HH:MM from the given timedelta'''
presence_str = str(timedelta)
return presence_str[:presence_str.rindex(':')]
def _set_dict_to_date(date, duration):
'''converts time durations into datetime durations'''
new_duration = dict()
for key, value in duration.items():
if isinstance(value, datetime.time):
new_duration[key] = datetime.datetime.combine(date, value)
else:
new_duration[key] = value
return new_duration
def print_target_times(stash):
'''prints the target times'''
for target in stash['targets']:
target = datetime.timedelta(minutes=target)
target_time = get_target_time(stash, target)
target_time_str = target_time.strftime('%H:%M')
target_str = str(target)[:-3] # seconds are ignored
print('{: >5s} h{: >11}'.format(target_str, target_time_str))
def get_target_time(stash, target):
'''calculates the time when the target work time is reached'''
work = dict()
work['start'] = stash['work']['start']
work['end'] = work['start'] + target
while True:
presence = get_presence(work, stash['breaks'])
missing_time = target - presence
if missing_time > datetime.timedelta():
work['end'] += missing_time
else:
break
return work['end']
def main():
'''main entry point'''
formatter = argparse.RawDescriptionHelpFormatter
parser = argparse.ArgumentParser(description=__doc__,
formatter_class=formatter)
parser.add_argument('--path', '-p',
help='path to folder where logs and stash are stored',
dest='path', default=None, required=True)
args = parser.parse_args()
now = datetime.datetime.now()
update(args.path, now)
if __name__ == '__main__':
main()