-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWorktimeLogger.py
565 lines (445 loc) · 16.9 KB
/
WorktimeLogger.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
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
#!/usr/bin/env python
WL_VERSION='c19fa4d'
import os
import sys
import time
import pynotify
import sqlite3
from datetime import datetime, timedelta
from calendar import monthrange
from math import floor
from PyQt4 import uic, QtGui, QtCore
base_dir = os.path.dirname(os.path.realpath(__file__))
month_name = ("--", "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec")
GLOBAL_DB = None
GLOBAL_CONFIG = None
def sec_to_hm(secs):
h = floor(secs / 3600)
m = round((secs - h*3600) / 60)
if m == 60:
m = 0
h += 1
return (h, m)
def hm_to_sec(h, m):
return int(round(float(h)*3600 + float(m)*60))
def ordinal(n):
m = n % 10;
if m == 1 and n != 11:
return "st"
elif m == 2 and n != 12:
return "nd"
elif m == 3 and n != 13:
return "rd"
else:
return "th"
class __Error(Exception):
def __init__(self, info):
self.info = info
def __str__(self):
return "%s: %s" % (self.__class__.__name__, self.info)
class DatabaseInvalidError(__Error):
pass
class OptionNotFoundError(__Error):
pass
class Database:
def __init__(self, filename="~/.WorktimeLogger/wl.sqlite"):
filename = os.path.realpath(os.path.expanduser(filename))
if not os.path.exists(filename):
db_dir = os.path.dirname(filename)
if not os.path.exists(db_dir):
os.system("mkdir -p %s" % db_dir)
os.system("cp %s/wl.sqlite %s" % (base_dir, filename))
self.db = sqlite3.connect(filename)
self.cur = self.db.cursor()
def commit(self):
self.db.commit()
def close(self):
self.db.close()
def q(self, query):
self.cur.execute(query)
res = self.cur.fetchall()
reslist = []
for row in res:
row_dict = {}
for i, field in enumerate(row):
row_dict[self.cur.description[i][0]] = field
reslist.append(row_dict)
return reslist
def getLog(self, log_id):
res = self.q("SELECT * FROM logs WHERE id == %d" % log_id)
return res[0]
def getActiveLog(self):
res = self.q("SELECT * FROM logs WHERE active == 1 ORDER BY id DESC")
if not res: # no active log
return self.createNewLog()
elif len(res) > 1: # multiple active logs
raise DatabaseInvalidError("Multiple logs are active. Your database may be corrupted.")
else:
return res[0]
def createNewLog(self):
self.q("INSERT INTO logs (active) values (1)")
self.commit()
return self.getActiveLog()
def setTimeIn(self, log_id, time):
self.q("UPDATE logs SET time_in = %d WHERE id = %d" % (time, log_id))
self.commit()
def setTimeOut(self, log_id, time):
self.q("UPDATE logs SET time_out = %d WHERE id = %d" % (time, log_id))
self.commit()
def deactivateLog(self, log_id):
self.q("UPDATE logs SET active = 0 WHERE id = %d" % log_id)
self.commit()
def getLogsFrom(self, time_start, time_end):
return self.q("SELECT * FROM logs WHERE time_in >= %d AND time_out <= %d AND active = 0" % (time_start, time_end))
def clearLogs(self):
self.q("DELETE FROM logs")
self.commit()
def getAllLogs(self):
return self.q("SELECT * FROM logs WHERE active = 0")
def getLastInTime(self):
return self.q("SELECT time_in FROM logs ORDER BY time_in DESC LIMIT 1")[0]
def getLastOutTime(self):
return self.q("SELECT time_out FROM logs ORDER BY time_out DESC LIMIT 1")[0]
def getConfig(self):
conf = self.q("SELECT * FROM config")
conf_dict = {}
for row in conf:
conf_dict[row["option"]] = row["value"]
return conf_dict
def getOption(self, option):
opt = self.q("SELECT * FROM config WHERE option = \"%s\"" % option)
if not opt:
raise OptionNotFoundError("No option '%s' in database." % option)
return
return opt[0]["value"]
def setOption(self, option, value):
self.q("UPDATE config SET value = \"%s\" WHERE option = \"%s\"" % (value, option))
self.commit()
def addOption(self, option, value):
self.q("INSERT INTO config (option, value) VALUES (\"%s\", \"%s\")" % (option, value))
self.commit()
def removeOption(self, option):
self.q("DELETE FROM config WHERE option = \"%s\"" % option)
self.commit()
class Config:
def __init__(self):
self.db = GLOBAL_DB
self.config = None
self.updateDB()
def updateDB(self):
self.config = self.db.getConfig()
def getOption(self, option):
return self.config[option]
def setOption(self, option, value):
if option not in self.config:
self.db.addOption(option, value)
else:
self.db.setOption(option, value)
self.updateDB()
def removeOption(self, option):
self.db.removeOption(option)
self.updateDB()
def getAll(self):
return self.config
class Log:
def __init__(self):
pynotify.init("WorktimeLogger")
self.t = time.time()
self.logged_in = False
self.db = GLOBAL_DB
self.log = self.db.getActiveLog()
if self.log["time_in"]:
self.t = self.log["time_in"]
self.logged_in = True
self.need_new_log = False
self.config = GLOBAL_CONFIG
def updateTime(self):
self.t = time.time()
def getTime(self):
return self.t
def logIn(self):
self.updateTime()
if self.need_new_log:
self.log = self.db.getActiveLog()
self.db.setTimeIn(self.log["id"], self.getTime())
self.updateDB()
self.logged_in = True
pynotify.Notification("Worktime Logger",
"Logged in at %s" % time.strftime(self.config.getOption("datetime_fmt"), time.localtime(self.getTime()))
).show()
def logOut(self):
self.updateTime()
self.db.setTimeOut(self.log["id"], self.getTime())
self.db.deactivateLog(self.log["id"])
self.updateDB()
self.logged_in = False
pynotify.Notification("Worktime Logger",
"Logged out at %s" % time.strftime(self.config.getOption("datetime_fmt"), time.localtime(self.getTime()))
).show()
self.invalidate()
def isLoggedIn(self):
return self.logged_in
def updateDB(self):
self.log = self.db.getLog(self.log["id"])
def invalidate(self):
self.need_new_log = True
def getTimeBetween(self, time_start, time_end):
res = self.db.getLogsFrom(time_start, time_end)
total = 0
for row in res:
total += row["time_out"] - row["time_in"]
if self.isLoggedIn() and self.getTime() >= time_start and self.getTime() <= time_end:
total += time.time() - self.getTime()
return total
def getTotalTime(self):
res = self.db.getAllLogs()
total = 0
for row in res:
total += row["time_out"] - row["time_in"]
if self.isLoggedIn():
total += time.time() - self.getTime()
return total
def getLastInTime(self):
return self.db.getLastInTime()["time_in"]
def getLastOutTime(self):
return self.db.getLastOutTime()["time_out"]
class WLLoginDialog(QtGui.QDialog):
def __init__(self, main):
QtGui.QDialog.__init__(self)
uic.loadUi("%s/ui/LogInDialog.ui" % base_dir, self)
self.main = main
self.config = GLOBAL_CONFIG
self.TextLabel.setText("Log in at %s?" % time.strftime(self.config.getOption("datetime_fmt")))
self.connect(self.YesButton, QtCore.SIGNAL("clicked()"), self.logIn)
self.connect(self.NoButton, QtCore.SIGNAL("clicked()"), self.hide)
self.connect(self.PanelButton, QtCore.SIGNAL("clicked()"), self.openPanel)
self.show()
def logIn(self):
self.main.logIn()
self.hide()
def openPanel(self):
self.main.show()
self.hide()
class WLArchivalDataBrowser(QtGui.QWidget):
def __init__(self):
QtGui.QWidget.__init__(self)
uic.loadUi("%s/ui/ArchivalData.ui" % base_dir, self)
self.config = GLOBAL_CONFIG
self.log = Log()
self.connect(self.Calendar, QtCore.SIGNAL("clicked(QDate)"), self.dateChanged)
self.connect(self.Calendar, QtCore.SIGNAL("currentPageChanged(int,int)"), self.pageChanged)
self.date = self.Calendar.selectedDate()
self.update()
self.show()
def dateChanged(self, date):
self.date = date
self.update()
def pageChanged(self, year, month):
self.date.setDate(year, month, self.date.day())
if not self.date.isValid():
self.date.setDate(year, month, 1)
self.update()
def update(self):
self.DayLabel.setText("%d%s %s" % (self.date.day(), ordinal(self.date.day()), month_name[self.date.month()]))
self.WeekLabel.setText("Week %02d of %d" % self.date.weekNumber())
self.MonthLabel.setText("%s %d" % (month_name[self.date.month()], self.date.year()))
timefmt = self.config.getOption("timeshort_fmt")
d = datetime(self.date.year(), self.date.month(), self.date.day())
mtarget = hm_to_sec(self.config.getOption("hours"), self.config.getOption("minutes")) # time to work in a month
wtarget = mtarget/4 # time to work in a week
dtarget = wtarget/5 # time to work in a day
# start time of selected day
dstart = time.mktime(d.replace(hour=0, minute=0, second=0, microsecond=0).timetuple())
# end time of selected month
dend = time.mktime(d.replace(hour=23, minute=59, second=59, microsecond=999999).timetuple())
dtime = self.log.getTimeBetween(dstart, dend)
self.WorkedDayLabel.setText(timefmt % sec_to_hm(dtime))
if dtime > dtarget:
self.LeftDayLabel.setText("-"+timefmt % sec_to_hm(dtime - dtarget))
else:
self.LeftDayLabel.setText(timefmt % sec_to_hm(dtarget - dtime))
# start time of selected week
wstart_d = (d - timedelta(days=d.weekday())).replace(hour=0, minute=0, second=0, microsecond=0)
wstart = time.mktime(wstart_d.timetuple())
# end time of selected week
wend = time.mktime((wstart_d + timedelta(days=6)).replace(hour=23, minute=59, second=59, microsecond=999999).timetuple())
wtime = self.log.getTimeBetween(wstart, wend)
self.WorkedWeekLabel.setText(timefmt % sec_to_hm(wtime))
if wtime > wtarget:
self.LeftWeekLabel.setText("-"+timefmt % sec_to_hm(wtime - wtarget))
else:
self.LeftWeekLabel.setText(timefmt % sec_to_hm(wtarget - wtime))
# start time of selected month
mstart = time.mktime(d.replace(day=1, hour=0, minute=0, second=0, microsecond=0).timetuple())
# end time of selected month
mend = time.mktime(d.replace(day=monthrange(d.year, d.month)[1], hour=23, minute=59, second=59, microsecond=999999).timetuple())
mtime = self.log.getTimeBetween(mstart, mend)
self.WorkedMonthLabel.setText(timefmt % sec_to_hm(mtime))
if mtime > mtarget:
self.LeftMonthLabel.setText("-%02d:%02d" % sec_to_hm(mtime - mtarget))
else:
self.LeftMonthLabel.setText(timefmt % sec_to_hm(mtarget - mtime))
class WLClearLogDialog(QtGui.QWidget):
def __init__(self, parent, main):
QtGui.QWidget.__init__(self)
uic.loadUi("%s/ui/ClearLogDialog.ui" % base_dir, self)
self.parent = parent
self.main = main
self.connect(self.ClearButton, QtCore.SIGNAL("clicked()"), self.clearLog)
self.connect(self.CancelButton, QtCore.SIGNAL("clicked()"), self.hide)
self.update()
self.show()
def clearLog(self):
self.main.logOut()
GLOBAL_DB.clearLogs()
self.main.update()
self.hide()
self.parent.hide()
class WLConfigBrowser(QtGui.QWidget):
def __init__(self, parent):
QtGui.QWidget.__init__(self)
uic.loadUi("%s/ui/ConfEditor.ui" % base_dir, self)
self.config = GLOBAL_CONFIG
self.parent = parent
self.connect(self.AddButton, QtCore.SIGNAL("clicked()"), self.addOption)
self.connect(self.RemoveButton, QtCore.SIGNAL("clicked()"), self.removeOption)
self.connect(self.ApplyButton, QtCore.SIGNAL("clicked()"), self.apply)
self.connect(self.CancelButton, QtCore.SIGNAL("clicked()"), self.hide)
self.connect(self.ClearButton, QtCore.SIGNAL("clicked()"), self.clearLog)
self.update()
self.show()
def update(self):
opts = self.config.getAll()
self.OptionsTable.setRowCount(0)
row = 0
for k,v in opts.iteritems():
self.OptionsTable.setRowCount(row+1)
self.OptionsTable.setItem(row, 0, QtGui.QTableWidgetItem(k))
self.OptionsTable.setItem(row, 1, QtGui.QTableWidgetItem(v))
row += 1
def addOption(self):
self.OptionsTable.setRowCount(self.OptionsTable.rowCount()+1)
def removeOption(self):
self.OptionsTable.removeRow(self.OptionsTable.currentRow())
def apply(self):
present_options = []
for row in range(0, self.OptionsTable.rowCount()):
k = str(self.OptionsTable.item(row, 0).text())
v = str(self.OptionsTable.item(row, 1).text())
self.config.setOption(k, v)
present_options.append(k)
for opt in [ k for k in self.config.getAll() if k not in present_options ]:
self.config.removeOption(opt)
self.parent.update()
self.hide()
def clearLog(self):
self.ClearDialog = WLClearLogDialog(self, self.parent)
class WLMain(QtGui.QMainWindow):
def __init__(self, app):
QtGui.QMainWindow.__init__(self)
uic.loadUi("%s/ui/WorktimeLogger.ui" % base_dir, self)
self.VersionLabel.setText(WL_VERSION)
self.traymenu = QtGui.QMenu()
self.traymenu.timeAction = self.traymenu.addAction("Not logged in")
self.traymenu.timeAction.setEnabled(False)
self.traymenu.addSeparator()
self.traymenu.openPanelAction = self.traymenu.addAction("Open panel")
self.traymenu.logAction = self.traymenu.addAction("Log in")
self.trayicon = QtGui.QSystemTrayIcon(QtGui.QIcon("%s/icons/wl.png" % base_dir), app)
self.trayicon.setContextMenu(self.traymenu)
self.trayicon.show()
self.timer = QtCore.QTimer()
self.connect(self.timer, QtCore.SIGNAL("timeout()"), self.update)
self.timer.start(30000) # 30s update time
# workaround for not working "activated" signal in QSystemTrayIcon
self.connect(self.traymenu, QtCore.SIGNAL("aboutToShow()"), self.updateMenu)
self.connect(self.traymenu.openPanelAction, QtCore.SIGNAL("triggered()"), self.show)
self.connect(self.traymenu.logAction, QtCore.SIGNAL("triggered()"), self.logIn)
self.connect(self.LogInButton, QtCore.SIGNAL("clicked()"), self.logIn)
self.connect(self.LogOutButton, QtCore.SIGNAL("clicked()"), self.logOut)
self.connect(self.ArchiveButton, QtCore.SIGNAL("clicked()"), self.openArchive)
self.connect(self.ConfigureButton, QtCore.SIGNAL("clicked()"), self.openConfig)
self.connect(self.QuitButton, QtCore.SIGNAL("clicked()"), self.exit)
self.log = Log()
if self.log.isLoggedIn():
self.logInGUIAction()
else:
self.logInPrompt = WLLoginDialog(self)
self.config = GLOBAL_CONFIG
self.update()
def logOut(self):
self.log.logOut()
self.LogInButton.setEnabled(True)
self.LogOutButton.setEnabled(False)
self.traymenu.timeAction.setText("Not logged in")
self.traymenu.logAction.setText("Log in")
self.disconnect(self.traymenu.logAction, QtCore.SIGNAL("triggered()"), self.logOut)
self.connect(self.traymenu.logAction, QtCore.SIGNAL("triggered()"), self.logIn)
def logIn(self):
self.log.logIn()
self.logInGUIAction()
def logInGUIAction(self):
self.LogInButton.setEnabled(False)
self.LogOutButton.setEnabled(True)
self.traymenu.timeAction.setText("Logged in since XXh XXmin")
self.traymenu.logAction.setText("Log out")
self.disconnect(self.traymenu.logAction, QtCore.SIGNAL("triggered()"), self.logIn)
self.connect(self.traymenu.logAction, QtCore.SIGNAL("triggered()"), self.logOut)
def updateMenu(self):
if self.log.isLoggedIn():
timefmt = self.config.getOption("timelong_fmt")
diff = time.time() - self.log.getTime()
self.traymenu.timeAction.setText("Logged in since "+timefmt % sec_to_hm(diff))
def closeEvent(self, ev):
self.hide()
ev.ignore()
def exit(self):
GLOBAL_DB.close()
sys.exit(0)
def update(self):
d = datetime.today()
timefmt = self.config.getOption("timeshort_fmt")
mtarget = hm_to_sec(self.config.getOption("hours"), self.config.getOption("minutes")) # time to work in a month
wtarget = mtarget/4 # time to work in a week
# start time of current week
wstart_d = (d - timedelta(days=d.weekday())).replace(hour=0, minute=0, second=0, microsecond=0)
wstart = time.mktime(wstart_d.timetuple())
# end time of current week
wend = time.mktime((wstart_d + timedelta(days=6)).replace(hour=23, minute=59, second=59, microsecond=999999).timetuple())
wtime = self.log.getTimeBetween(wstart, wend)
self.WorkedThisWeekLabel.setText(timefmt % sec_to_hm(wtime))
if wtime > wtarget:
self.LeftThisWeekLabel.setText("-"+timefmt % sec_to_hm(wtime - wtarget))
else:
self.LeftThisWeekLabel.setText(timefmt % sec_to_hm(wtarget - wtime))
# start time of current month
mstart = time.mktime(d.replace(day=1, hour=0, minute=0, second=0, microsecond=0).timetuple())
# end time of current month
mend = time.mktime(d.replace(day=monthrange(d.year, d.month)[1], hour=23, minute=59, second=59, microsecond=999999).timetuple())
mtime = self.log.getTimeBetween(mstart, mend)
self.WorkedThisMonthLabel.setText(timefmt % sec_to_hm(mtime))
if mtime > mtarget:
self.LeftThisMonthLabel.setText("-"+timefmt % sec_to_hm(mtime - mtarget))
else:
self.LeftThisMonthLabel.setText(timefmt % sec_to_hm(mtarget - mtime))
datefmt = self.config.getOption("datetime_fmt")
longtimefmt = self.config.getOption("timelong_fmt")
self.LastLogInLabel.setText(time.strftime(datefmt, time.localtime(self.log.getLastInTime())))
self.LastLogOutLabel.setText(time.strftime(datefmt, time.localtime(self.log.getLastOutTime())))
self.TotalTimeLabel.setText(longtimefmt % sec_to_hm(self.log.getTotalTime()))
def openArchive(self):
self.ArchiveBrowser = WLArchivalDataBrowser()
def openConfig(self):
self.ConfigBrowser = WLConfigBrowser(self)
def main():
from signal import signal, SIGINT, SIG_DFL
signal(SIGINT, SIG_DFL)
global GLOBAL_DB, GLOBAL_CONFIG
if len(sys.argv) > 1:
GLOBAL_DB = Database(sys.argv[1])
else:
GLOBAL_DB = Database()
GLOBAL_CONFIG = Config()
Application = QtGui.QApplication([])
Main = WLMain(Application)
sys.exit(Application.exec_())