-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
64 lines (43 loc) · 1.41 KB
/
main.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
#!/bin/bash
import os
import sys
import utility
import time
THRD_PARTY_DIR = './/3rd_party//'
sys.path.append(THRD_PARTY_DIR)
def get_active_window_now():
s = os.system(THRD_PARTY_DIR + "get_active.sh > shell_output_temp.txt");
f= open('shell_output_temp.txt' , 'r')
s = str(f.read()).strip()
return s[s.find('\"')+1:-1]
def write_dict_to_file(data_dict , filename) :
f = open (filename , 'w+')
f.seek(0)
f.truncate() #delete old content
for app , t in data_dict.items():
f.write(app+';'+t+'\n')
f.close()
def track_and_record(file_name = "data.dat" , delay_in_mins = 3):
file_dict = record_file_to_dict(file_name)
while True :
active_app = get_active_window_now()
file_dict.setdefault(active_app , "00:00")
file_dict[active_app] = utility.add_time(file_dict[active_app] ,"00:"+ str(delay_in_mins))
write_dict_to_file(file_dict , file_name)
time.sleep(delay_in_mins * 60);
def record_file_to_dict(file_name) :
ret_dict = {}
f = open(file_name , 'w+')
for line in f.readlines():
app , time = line.split(';')
ret_dict[app] = time.strip()
f.close()
return ret_dict
def main():
if sys.argv[1] == "--start" :
track_and_record(delay_in_mins=1);
elif sys.argv[1] == "--stats" :
print_stats();
else :
print "unknown command\n"
main()