-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtime_tracker.py
More file actions
69 lines (52 loc) · 1.86 KB
/
time_tracker.py
File metadata and controls
69 lines (52 loc) · 1.86 KB
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
import datetime
import os
import shlex
import subprocess
import sys
import PySimpleGUIQt as sg
# read config file
from read_config import get_config
config = get_config()
# log file location
log_file = os.path.expanduser(config["log_file"])
if not os.path.exists(log_file):
f = open(log_file, 'a')
f.write('date,action\n')
f.close()
# put labels into layout
options = [entry[0] for entry in config["labels"]]
menu_def = ['Menu', [*options, '---', 'Done', '---', 'Show report', 'Edit log', '---', 'Exit']]
default_icon = os.path.join(config["default_icon"])
tray = sg.SystemTray(menu=menu_def, filename=default_icon)
while True:
menu_item = tray.Read(timeout=5000) # timeout of 5 seconds
print(menu_item)
# end program, if selected
if menu_item == 'Exit':
break
elif menu_item == "Show report":
cmd = shlex.split(config["report"].format(PY_BIN=sys.executable).replace("\\", "/"))
p = subprocess.Popen(cmd, cwd=os.getcwd())
continue
elif menu_item == "Edit log":
cmd = shlex.split(config["editor"].format(LOG_FILE=log_file).replace("\\", "/"))
p = subprocess.Popen(cmd)
continue
# otherwise: log entry and change icon
elif menu_item not in [None, '__TIMEOUT__', '__ACTIVATED__']:
# log
if config["use_utc"]:
timestamp = datetime.datetime.utcnow()
else:
timestamp = datetime.datetime.now()
report_string = str(timestamp.replace(microsecond=0)) + ',' + menu_item + '\n'
f = open(log_file, 'a')
f.write(report_string)
f.close()
# change icon according to selected item
if menu_item == "Done":
icon_path = default_icon
else:
icon_path = [icon for name, icon in config["labels"] if name == menu_item][0]
tray.Update(filename = icon_path)
tray.close()