forked from frans-fuerst/track
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrack_common.py
executable file
·157 lines (123 loc) · 4.17 KB
/
track_common.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
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
from datetime import datetime
import time
def mins_to_date(mins):
_result = ""
_minutes = mins
if _minutes >= 60:
_result = "%2d:" %(_minutes / 60)
_minutes %= 60
_result += "%02d" % _minutes
return _result
def secs_to_dur(mins):
_result = ""
_minutes = mins
if _minutes >= 60:
_result = str(int(_minutes / 60))+"m "
_minutes %= 60
_result += str(_minutes ) + "s"
return _result
def mins_to_dur(mins):
_result = ""
_minutes = mins
if _minutes >= 60:
_result = str(int(_minutes / 60)) + "h "
_minutes %= 60
_result += "%02d" % _minutes + "m"
else:
_result += "%d" % _minutes + "m"
return _result
def today_str():
return datetime.fromtimestamp(time.time()).strftime('%Y%m%d')
def today_int():
now = datetime.now()
return now.year * 10000 + now.month * 100 + now.day
def seconds_since_midnight():
now = datetime.now()
return int((now - now.replace(hour=0, minute=0, second=0, microsecond=0)).total_seconds())
def minutes_since_midnight():
#return int(seconds_since_midnight() / 2)
return int(seconds_since_midnight() / 60)
class app_info():
def __init__(self, windowtitle="", cmdline=""):
self._wndtitle = windowtitle
self._cmdline = cmdline
self._category = 0
self._count = 0
def __eq__(self, other):
if not self._wndtitle == other._wndtitle:
return False
if not self._cmdline == other._cmdline:
return False
if not self._category == other._category:
return False
if not self._count == other._count:
return False
return True
def generate_identifier(self):
return self._wndtitle
def __hash__(self):
x = hash((self._wndtitle, self._cmdline))
return x
def __str__(self):
return "%s - [%d %d]" % (self._wndtitle, self._category, self._count)
def load(self, data):
try:
self._wndtitle, self._category, self._count, self._cmdline = data
except:
print('tried to expand %s to (title, category, count, cmdline)' % (
str(data)))
raise Exception('could not load app_info data')
return self
def __data__(self): # const
return (self._wndtitle, self._category, self._count, self._cmdline)
class minute():
""" a minute holds a category and a list of apps
"""
def __init__(self, category=0, apps=None):
self._category = 0
if apps is None:
self._apps = {}
else:
self._apps = apps # app_info -> count
def __eq__(self, other):
if not self._category == other._category:
return False
if not self._apps == other._apps:
for a, c in self._apps.items():
print("s: %s:'%s' - %d" % (hex(id(a)), a, c))
for a, c in other._apps.items():
print("o: %s - %d" % (a, c))
return False
return True
def dump(self):
print("category %d" % self._category)
def init(self, data):
self._category, self._apps = data
return self
def _rebuild(self):
if len(self._apps) == 0:
return 0 # todo: need undefined
_categories = {} # category -> sum
for a, c in self._apps.items():
try:
if a._category not in _categories:
_categories[a._category] = c
else:
_categories[a._category] += c
except:
pass
self._category = _categories.keys()[
_categories.values().index(
max(_categories.values()))]
# print(self._category)
def add(self, app_instance):
if app_instance not in self._apps:
self._apps[app_instance] = 1
else:
self._apps[app_instance] += 1
self._rebuild()
def get_main_app(self):
_a = max(self._apps, key=lambda x: self._apps[x])
return _a._wndtitle