-
Notifications
You must be signed in to change notification settings - Fork 3
/
pp_utils.py
337 lines (270 loc) · 11.5 KB
/
pp_utils.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
# dec 2015 - hide terminal output if --manager option.
# 28/1/2016 - additional filter and log output for statistics
import time
import datetime
import sys
import os
import gc
import tkinter.messagebox
from tkinter import NW,N,W,CENTER,LEFT,RIGHT
from pp_statsrecorder import Statsrecorder
# from pympler.tracker import SummaryTracker
# from pympler import summary, muppy
# import types
def calculate_text_position(x_text,y_text,x1,y1,centre_x,centre_y,width,height,justify_text):
if x_text == '':
x=x1+centre_x
else:
x = int(x_text)+x1
if y_text == '':
y=y1+centre_y
else:
y=int(y_text)+y1
if x_text == '' and y_text=='':
anchor=CENTER
elif x_text == '' and y_text!='':
anchor = N
elif x_text != '' and y_text=='':
anchor = W
else:
anchor= NW
if justify_text=='left':
justify=LEFT
elif justify_text=='right':
justify=RIGHT
else:
justify=CENTER
return x,y,anchor,justify
def parse_rectangle(text):
if text.strip() == '':
return 'error','No points in rectangle: '+text,0,0,0,0
if '+' in text:
# parse x+y+width*height
fields=text.split('+')
if len(fields) != 3:
return 'error','Do not understand rectangle: '+ text,0,0,0,0
dimensions=fields[2].split('*')
if len(dimensions)!=2:
return 'error','Do not understand rectangle: '+text,0,0,0,0
if not fields[0].isdigit():
return 'error','x1 is not a positive integer: '+ text,0,0,0,0
else:
x1=int(fields[0])
if not fields[1].isdigit():
return 'error','y1 is not a positive integer: '+ text,0,0,0,0
else:
y1=int(fields[1])
if not dimensions[0].isdigit():
return 'error','width is not a positive integer: '+text,0,0,0,0
else:
width=int(dimensions[0])
if not dimensions[1].isdigit():
return 'error','height is not a positive integer: '+text,0,0,0,0
else:
height=int(dimensions[1])
return 'normal','',x1,y1,x1+width,y1+height
else:
fields=text.split()
if len(fields) == 4:
# rectangle is specified
if not (fields[0].isdigit() and fields[1].isdigit() and fields[2].isdigit() and fields[3].isdigit()):
return 'error','coordinates are not positive integers ' +text,0,0,0,0
return 'normal','',int(fields[0]),int(fields[1]),int(fields[2]),int(fields[3])
else:
# error
return 'error','illegal rectangle: '+ text,0,0,0,0
# used for web_editor only
def calculate_relative_path(file_path,pp_home_dir,pp_profile_dir):
# is media in the profile
# print 'pp_profile dir ',pp_profile_dir
in_profile=False
if pp_profile_dir in file_path:
in_profile=True
if in_profile is True:
# deal with media in profile @
relpath = os.path.relpath(file_path,pp_profile_dir)
# print "@ relative path ",relpath
common = os.path.commonprefix([file_path,pp_profile_dir])
# print "@ common ",common
if common == pp_profile_dir:
location = "@" + os.sep + relpath
location = str.replace(location,'\\','/')
# print '@location ',location
# print
return location
else:
# print '@absolute ',file_path
return file_path
else:
# deal with media in pp_home +
relpath = os.path.relpath(file_path,pp_home_dir)
# print "+ relative path ",relpath
common = os.path.commonprefix([file_path,pp_home_dir])
# print "+ common ",common
if common == pp_home_dir:
location = "+" + os.sep + relpath
location = str.replace(location,'\\','/')
# print '+location ', location
# print
return location
else:
# print '+ absolute ',file_path
# print
return file_path
class StopWatch(object):
global_enable=False
def __init__(self):
self.enable=False
def on(self):
self.enable=True
def off(self):
self.enable=False
def start(self):
if StopWatch.global_enable and self.enable:
self.sstart=time.clock()
def split(self,text):
if StopWatch.global_enable and self.enable:
self.end=time.clock()
print(text + " " + str(self.end-self.sstart) + " secs")
self.sstart=time.clock()
def stop(self,text):
if StopWatch.global_enable and self.enable:
self.end=time.clock()
print(text + " " + str(self.end-self.sstart) + " secs")
class Monitor(object):
delimiter=';'
m_fatal =1 # fatal erros caused by PiPresents, could be a consequence of an 'error'
m_err = 2 # PP cannot continue because of an error caused by user in profile or command
m_warn =4 # warning that something is not quite right but PP recovers gracefully
m_log =8 # log of interest to profile developers
m_trace =16 # trace for software development
m_trace_instance =32 # trace with id of instances of player and showers
m_leak = 64 # memory leak monitoring
m_stats = 128 #statistics
m_sched = 256 # time of day scheduler
classes = []
log_level=0
log_path="" # set in pipresents
ofile=None
start_time= time.time()
tracker=None
show_count=0
track_count=0
stats_file=None
sr=None #
# called at start by pipresents
def init(self):
# Monitor.tracker = SummaryTracker()
if Monitor.ofile is None:
bufsize=-1
Monitor.ofile=open(Monitor.log_path+ os.sep+'pp_logs' + os.sep + 'pp_log.txt','w',bufsize)
Monitor.log_level=0 # set in pipresents
Monitor.manager=False #set in pipresents
Monitor.classes = []
Monitor.enable_in_code = False # enables use of self.mon.set_log_level(nn) in classes
Monitor.sr=Statsrecorder()
Monitor.sr.init(Monitor.log_path)
def leak_diff(self):
Monitor.tracker.print_diff()
def leak_summary(self):
all_objects = muppy.get_objects()
sum1 = summary.summarize(all_objects)
summary.print_(sum1)
def leak_anal(self):
all_objects = muppy.get_objects()
my_types = muppy.filter(all_objects, Type=ImagePlayer)
print(len(my_types))
for t in my_types:
print(t,sys.getrefcount(t))
# ,gc.get_referrers(t)
# CONTROL
def __init__(self):
# default value for individual class logging
self.this_class_level= Monitor.m_fatal|Monitor.m_err|Monitor.m_warn
def set_log_level(self,level):
self.this_class_level = level
# PRINTING
def newline(self,num):
if Monitor.manager is False:
if Monitor.log_level & ~ (Monitor.m_warn|Monitor.m_err|Monitor.m_fatal|Monitor.m_sched) != 0:
for i in range(0,num):
print()
def fatal(self,caller,text):
r_class=caller.__class__.__name__
r_func = sys._getframe(1).f_code.co_name
r_line = str(sys._getframe(1).f_lineno)
if self.enabled(r_class,Monitor.m_fatal) is True:
print("%.2f" % (time.time()-Monitor.start_time), " System Error: ",r_class+"/"+ r_func + "/"+ r_line + ": ", text)
Monitor.ofile.write (" SYSTEM ERROR: " + r_class +"/"+ r_func + "/"+ r_line + ": " + text + "\n")
if Monitor.manager is False:
tkinter.messagebox.showwarning(r_class ,'System Error:\n'+text)
def err(self,caller,text):
r_class=caller.__class__.__name__
if self.enabled(r_class,Monitor.m_err) is True:
print("%.2f" % (time.time()-Monitor.start_time), " Profile Error: ",r_class+": ", text)
Monitor.ofile.write (" ERROR: " + self.pretty_inst(caller)+ ": " + text + "\n")
if Monitor.manager is False:
tkinter.messagebox.showwarning(r_class ,'Profile Error:\n'+text)
def warn(self,caller,text):
r_class=caller.__class__.__name__
if self.enabled(r_class,Monitor.m_warn) is True:
print("%.2f" % (time.time()-Monitor.start_time), " Warning: ",self.pretty_inst(caller) +": ", text)
Monitor.ofile.write (" WARNING: " + self.pretty_inst(caller)+ ": " + text + "\n")
def sched(self,caller,pipresents_time,text):
r_class=caller.__class__.__name__
if self.enabled(r_class,Monitor.m_sched) is True:
if pipresents_time is None:
ptime=' '
else:
ptime=str(pipresents_time)
print(ptime +" "+r_class+": " + text)
# print "%.2f" % (time.time()-Monitor.start_time) +" "+self.pretty_inst(caller)+": " + text
Monitor.ofile.write (time.strftime("%Y-%m-%d %H:%M") + " " + self.pretty_inst(caller)+": " + text+"\n")
def log(self,caller,text):
r_class=caller.__class__.__name__
if self.enabled(r_class,Monitor.m_log) is True:
print("%.2f" % (time.time()-Monitor.start_time) +" "+r_class+": " + text)
# print "%.2f" % (time.time()-Monitor.start_time) +" "+self.pretty_inst(caller)+": " + text
Monitor.ofile.write (str(time.time()-Monitor.start_time) + " " + self.pretty_inst(caller)+": " + text+"\n")
def start_stats(self,profile):
Monitor.profile = profile
self.stats((""),(""),(""),("start"),(""),(""),(""),(profile))
def stats(self,*args):
if (Monitor.m_stats & Monitor.log_level) != 0:
Monitor.sr.write_stats(datetime.datetime.now(),Monitor.profile,*args)
def trace(self,caller,text):
r_class=caller.__class__.__name__
r_class = type(caller).__name__
r_func = sys._getframe(1).f_code.co_name
r_line = str(sys._getframe(1).f_lineno)
r_id = 'id='+str(id(caller))
r_longid = caller
# self.print_info(r_class,Monitor.m_trace)
if self.enabled(r_class,Monitor.m_trace) is True:
print(self.pretty_inst(caller)+'/'+r_func, text)
Monitor.ofile.write ( self.pretty_inst(caller)+" /" + r_func +" " + text+"\n")
def print_info(self,r_class,mask):
print('called from', r_class)
print('Global Log level',Monitor.log_level)
print('Global enable in code', Monitor.enable_in_code)
print('in code log level',self.this_class_level)
print('Trace mask',mask)
def enabled(self,r_class,report):
enabled_in_code=(report & self.this_class_level) != 0 and Monitor.enable_in_code is True
globally_enabled=(report & Monitor.log_level) !=0 and r_class in Monitor.classes
if enabled_in_code is True or globally_enabled is True:
return True
else:
return False
def pretty_inst(self,inst):
if inst is None:
return 'None'
else:
return inst.__class__.__name__ + '_' + str(id(inst))
def finish(self):
Monitor.ofile.close()
Monitor.sr.close()
# krt
Monitor.ofile=None
## def id(self,caller):
## return self.pretty_inst(caller)