-
Notifications
You must be signed in to change notification settings - Fork 0
/
My_Utils.py
303 lines (277 loc) · 9.56 KB
/
My_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
#!/usr/bin/python
# -*- coding: utf-8 -*-
# XXX KISS
import os
import sys
import time
import ctypes
import random
import string
import shutil
import logging
import datetime
import platform
class Tee(object):
""" tee for python
"""
def __init__(cls, _fd1, _fd2):
cls.fd1 = _fd1
cls.fd2 = _fd2
def __del__(cls):
if ((cls.fd1 != sys.stdout) and (cls.fd1 != sys.stderr)):
cls.fd1.close()
if ((cls.fd2 != sys.stdout) and (cls.fd2 != sys.stderr)):
cls.fd2.close()
def write(cls, text):
cls.fd1.write(text)
cls.fd2.write(text)
def flush(cls):
cls.fd1.flush()
cls.fd2.flush()
# STDOUT:
@classmethod
def stdout_start(cls, logfilename='stdout.log', append=True):
cls.stdoutsav = sys.stdout
if (append):
cls.LOGFILE = open(logfilename, 'a')
else:
cls.LOGFILE = open(logfilename, 'w')
sys.stdout = tee(cls.stdoutsav, cls.LOGFILE)
return cls.LOGFILE
@classmethod
def stdout_stop(cls):
cls.LOGFILE.close()
sys.stdout = cls.stdoutsav
# STDERR:
@classmethod
def stderr_start(cls, errfilename='stderr.log', append=True):
cls.stderrsav = sys.stderr
if (append):
cls.ERRFILE = open(errfilename, 'a')
else:
cls.ERRFILE = open(errfilename, 'w')
sys.stderr = tee(cls.stderrsav, cls.ERRFILE)
return cls.ERRFILE
@classmethod
def stderr_stop(cls):
cls.ERRFILE.close()
sys.stderr = cls.stderrsav
##==============------------------- End -------------------==============##
def Print_Aligned ( List_of_Strings ) :
'''
print formated table with the values provided
'''
lens = []
for col in zip(*List_of_Strings):
lens.append(max([len(v) for v in col]))
format = " ".join(["{:<" + str(l) + "}" for l in lens])
for row in List_of_Strings:
print(format.format(*row))
##==============------------------- End -------------------==============##
def Custom_logger( name ):
Frmt = logging.Formatter(fmt='%(asctime)s %(levelname)-8s %(message)s',
datefmt='%Y-%m-%d %H:%M:%S')
Hndlr = logging.FileHndlr('log.txt', mode='w')
Hndlr.setFormatter(Frmt)
screen_Hndlr = logging.StreamHndlr(stream=sys.stdout)
screen_Hndlr.setFormatter(Frmt)
logger = logging.getLogger(name)
logger.setLevel(logging.DEBUG)
logger.addHndlr(Hndlr)
logger.addHndlr(screen_Hndlr)
return logger
##==============------------------- End -------------------==============##
def Util_str_calc (the_string, DeBug=False ) :
message = sys._getframe().f_code.co_name
'''
Returns the result for string (a/b)
'''
(dividend, divisor) = the_string.split('/')
if DeBug : print ( "Util", the_string, dividend, divisor)
if divisor == '0' :
message += " ! Division by Zero ! {} / {}".format( dividend , divisor )
print ( message )
if DeBug :
input ('Wait')
return False
elif dividend == '0' :
message += " ! Zero Divided by ! {} / {}".format( dividend , divisor )
print ( message )
if DeBug :
input ('Wait')
return 0
else :
Rounded = float(dividend) / float(divisor)
if Rounded > 1 :
Rounded = round ( Rounded, 2 )
else :
Rounded = round ( 1 / Rounded, 2 )
return Rounded
##==============------------------- End -------------------==============##
def Random_String( length= 17 ):
rand_string = '_'
for letter in random.sample('_' + string.ascii_letters + string.hexdigits, length):
rand_string += letter
return rand_string
##==============------------------- End -------------------==============##
def ordinal( num ):
'''
Returns the ordinal number of a given integer, as a string.
eg. 1 -> 1st, 2 -> 2nd, 3 -> 3rd, etc.
'''
if 10 <= num % 100 < 20:
return '{0}\'th'.format(num)
else:
ord = {1 : '\'st', 2 : '\'nd', 3 : '\'rd'}.get(num % 10, '\'th')
return '{0}{1}'.format(num, ord)
##==============------------------- End -------------------==============##
def New_File_Name ( file_name, new_extension='', strip='' ) :
'''
Returns a new filename derived from the Old File by adding and or removing
'''
filename, extensi = os.path.splitext(file_name)
if len(strip) :
filename = filename.strip(strip)
extensi = extensi.replace('.','_')
New = filename + extensi + new_extension
# New = filename + new_extension
return New
##==============------------------- End -------------------==============##
def HuSa( nbyte ):
'''
Returns a human readable string from a number
'''
suffixes = ['B', 'K', 'M', 'G', 'T', 'P', 'Zilion']
byte_val = float( nbyte )
indx = 0
while byte_val >= 1024 and indx < len(suffixes) -1:
byte_val /= 1024
indx += 1
res = ( round( byte_val, 1 ) )
return '%s %s' % (res, suffixes[indx])
##==============------------------- End -------------------==============##
def Bild_Dict (key, value, TheDick) :
if key in TheDick : ## XXX: Add item to key location
TheDick[ key ].append ( [value] )
else : ## XXX: The is the first, create key location
TheDick[ key ] = [ value ]
return TheDick
##==============------------------- End -------------------==============##
def Parse_from_to ( Stream, Dictio, DeBug=False ) :
message = sys._getframe().f_code.co_name
if DeBug : print(message,':\n', len(Stream), Stream, '\n', len(Dictio), Dictio)
Result = Dictio
Pu_la_cnt = 0
if not Stream :
print ("WTF:? " ,message, " No Stream" ,'\t', Stream ,'\n', Dictio )
for key in ( Dictio.keys() ) :
Dictio[key] = 'Pu_la'
return tuple( Dictio.values() )
elif not Dictio :
print ("WTF:? ", message, " No Dictio", '\t', Stream, '\n', Dictio )
return False
if DeBug : print ("\n>", repr(Dictio) )
try :
for key in Dictio.keys() :
if DeBug : print( key, Dictio[key] )
item = Stream.get (key,'Pu_la')
if item == 'Pu_la' :
Result[key] = 'Pu_la'
Pu_la_cnt += 1
if DeBug : print ("\nPu_la_la ", key, '\n' )
else :
ty = type (item)
dy = type (Dictio[key])
if DeBug : print(ty, item ,'\n', dy , Dictio[key])
if ty == str and dy == int :
Result[key] = int (item)
elif ty == str and dy == float :
Result[key] = float (item)
else :
Result[key] = item
if DeBug : print("Got : ", item )
if DeBug : print (message , " Out ", repr(Dictio) , "\nPu_la_cnt = ", Pu_la_cnt), input("N")
except Exception as e:
print( message,':\n', len(Stream), Stream, '\n', len(Dictio), Dictio )
print("\n{} -> {!r}".format(message, e))
print("Is: {}".format( traceback.print_stack() ) )
print("Error: {}".format( traceback.print_exc() ) )
input("All Fuked up")
if len(Dictio) > 1 :
return tuple( Dictio.values() )
else :
return Dictio[key]
##==============------------------- End -------------------==============##
def Resource_Check (Folder='./') :
print("=" * 60)
message = sys._getframe().f_code.co_name
print (datetime.datetime.now().strftime('\n%A: %m/%d/%Y %H:%M:%S %p'))
print('\n:>', message )
print (os.getcwd())
print ("Python is:", '\n'.join(sorted(sys.path)), '\n' )
print('\nFile :', __file__)
print('Access time :', time.ctime(os.path.getatime(__file__)))
print('Modified time:', time.ctime(os.path.getmtime(__file__)))
print('Change time :', time.ctime(os.path.getctime(__file__)))
print('Size :', HuSa( os.path.getsize( __file__)))
if os.path.isfile( Folder ) :
print ('\n', Folder, " is a File")
elif os.path.isdir( Folder ) :
print ('\n',Folder, " is a Folder" )
elif os.path.islink( Folder ) :
print ('\n',Folder, " is a Link")
elif os.path.ismount (Folder) :
print ('\n',Folder, " is a Mountpoint")
else :
print ('\n',Folder, " is WTF?")
try :
sys_is = platform.uname()
print ('\nSystem Name :', sys_is.node , sys_is.system , sys_is.release, '(',sys_is.version,')', sys_is.processor )
if not (os.path.exists( ffmpeg_bin )) :
print (message, " ffMpeg Path Does not Exist:")
return False
print ("Log File :" ,Log_File)
print ("FFmpeg :" ,ffmpeg_bin )
total, free = ctypes.c_ulonglong(), ctypes.c_ulonglong()
if sys.version_info >= (3,) or isinstance(path, unicode):
fun = ctypes.windll.kernel32.GetDiskFreeSpaceExW
else:
fun = ctypes.windll.kernel32.GetDiskFreeSpaceExA
ret = fun( None , None , ctypes.byref(total), ctypes.byref(free))
if ret == 0:
raise ctypes.WinError()
return False
if ( free.value / total.value ) < 0.25 :
print (message, "Not Enough Space on Disk")
return False
print ("\nTotal : %s Free %s %s %s"
% (HuSa(total.value) ,HuSa(free.value) , round( free.value/total.value *100 ) ,'%') )
except Exception as e:
message = " WTF? Exception " + type(e)
print (message + message )
print( traceback.format_exc() )
print( traceback.print_stack() )
finally :
print ("\nResources OK\n" )
return True
##==============------------------- End -------------------==============##
def get_tree_size(path):
"""Return total size of files in path and subdirs. If
is_dir() or stat() fails, print an error message to stderr
and assume zero size (for example, file has been deleted).
"""
total = 0
for entry in os.scandir(path):
try:
is_dir = entry.is_dir(follow_symlinks=False)
except OSError as error:
print('Error calling is_dir():', error, file=sys.stderr)
continue
if is_dir:
total += get_tree_size(entry.path)
else:
try:
total += entry.stat(follow_symlinks=False).st_size
except OSError as error:
print('Error calling stat():', error, file=sys.stderr)
return total