-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommon.py
340 lines (294 loc) · 11.6 KB
/
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
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
337
338
339
# ######################################################
# Christian Klugesherz
# March 2020
# ######################################################
from os import listdir,rename,getcwd
from os.path import isfile, isdir, join, splitext, exists
import sys
# ######################################################
# Global Vars
# ######################################################
APPLICATION_DEBUG_LEVEL = 0
# ######################################################
# Functions Prototype
# ######################################################
# def setApplicationDebugLevel(val):
# def getApplicationDebugLevel():
# def pError(text):
# def pDbg(text,functDlv=0,appDLv=0):
# def pDbg0(text):
# def pDbg1(text):
# def pDbg2(text):
# def pDbg3(text):
# def pDbg4(text):
# def pDbg5(text):
# def pFlaskAppConf(my_dict, printAll=False):
# ######################################################
# Functions
# ######################################################
# -----------------------------------------------------
# Set and Get application Debug Level
# -----------------------------------------------------
def setApplicationDebugLevel(val):
'''
set Global variable: APPLICATION_DEBUG_LEVEL
'''
global APPLICATION_DEBUG_LEVEL
APPLICATION_DEBUG_LEVEL = val
pDbg1("(setApplicationDebugLevel) Set Application Debug Level: {0}".format(APPLICATION_DEBUG_LEVEL))
def getApplicationDebugLevel():
'''
get Global variable: APPLICATION_DEBUG_LEVEL
'''
pDbg1("(getApplicationDebugLevel) Debug Level: {0}".format(APPLICATION_DEBUG_LEVEL))
return (APPLICATION_DEBUG_LEVEL)
# -----------------------------------------------------
# Print Error
# -----------------------------------------------------
def pError(text):
'''
Print Error
'''
out = "!! ERROR: "+ text + " !!"
for x in range(len(out)):
print('-', end='')
print()
print(out)
for x in range(len(out)):
print('-', end='')
print()
# -----------------------------------------------------
# Display the 'text' in the consol for debugging usage.
# text will be displayed as soon
# application debug level is >= than the level in the function
# -----------------------------------------------------
def pDbg(text,functDlv=0,appDLv=0):
'''
Display the 'text' in the consol for debugging usage.
We have to consider 2 parameters :
* appDLv
The Overall debug level needed by the application
For exemple : app.config["WEBAPP_DEBUG_LEVEL"] = 3
* functDlv
The Debug level for information to display in the function
--> text will be displayed as soon application debug level is >= than the level in the function
Exemple
def test()
....
pDebg("Value of i="+i,3,appDLv)
....
--> means if appDLv>=3, then the information will be dispayed, otherwise not
'''
if appDLv >= functDlv :
print(text)
# -----------------------------------------------------
# Print debug level 0 == like print()
# BUT THIS FUNCTION CAN BE EMPTY ONCE THE CODE IS PERFECT
# IT IS A TRICK TO DISPLAY THE INFORMATION ONCE APPLICATION_DEBUG_LEVEL IS NOT KNOWN
# -----------------------------------------------------
def pDbg0(text):
'''
idem as pDbg(text,0,APPLICATION_DEBUG_LEVEL)
'''
pDbg(text,0,APPLICATION_DEBUG_LEVEL)
# -----------------------------------------------------
# Print debug level 1
# -----------------------------------------------------
def pDbg1(text):
'''
idem as pDbg(text,1,APPLICATION_DEBUG_LEVEL)
'''
pDbg(" "+text,1,APPLICATION_DEBUG_LEVEL)
# -----------------------------------------------------
# Print debug level 2
# -----------------------------------------------------
def pDbg2(text):
'''
idem as pDbg(text,2,APPLICATION_DEBUG_LEVEL)
'''
pDbg(" "+text,2,APPLICATION_DEBUG_LEVEL)
# -----------------------------------------------------
# Print debug level 3
# -----------------------------------------------------
def pDbg3(text):
'''
idem as pDbg(text,3,APPLICATION_DEBUG_LEVEL)
'''
pDbg(" "+text,3,APPLICATION_DEBUG_LEVEL)
# -----------------------------------------------------
# Print debug level 4
# -----------------------------------------------------
def pDbg4(text):
'''
idem as pDbg(text,4,APPLICATION_DEBUG_LEVEL)
'''
pDbg(" "+text,4,APPLICATION_DEBUG_LEVEL)
# -----------------------------------------------------
# Print debug level 5
# -----------------------------------------------------
def pDbg5(text):
'''
idem as pDbg(text,5,APPLICATION_DEBUG_LEVEL)
'''
pDbg(" "+text,5,APPLICATION_DEBUG_LEVEL)
# -----------------------------------------------------
# function which rename all files in a directory by replacing : charin by charout
# -----------------------------------------------------
def renFileDir(dirin,charin,charout):
"""
function which rename all files in a directory by replacing : charin by charout
parameters
* dirin : absolute or relative directory path
* charin : characters to replace
* charout : character to replace
Return the number of files which have been converted, or -1 if issue
"""
retval = 0
pDbg4("(renFileDir): Rename all files containing '{0}' by '{1}' in directory {2}".format(charin,charout,dirin) )
# test if Directory exists
if not exists(dirin):
pError("(renFileDir): chek if : " + dirin +" exists")
retval = -1
return retval
for f in listdir(dirin): # lis all of item in a directory
if isfile(join(dirin, f)): # Check if 'f' it is file
pDbg5("(renFileDir): Checking file: "+ f)
if f.find(charin) > 0: # Check if in the file there is charin ?
fout = f.replace(charin,charout)
retval = retval+1
pDbg5("(renFileDir): --> new file="+ fout)
rename(join(dirin, f),join(dirin, fout))
return retval
# -----------------------------------------------------
# function which rename all files in all Sub directory by replacing : charin by charout
# -----------------------------------------------------
def renFileSubdir(dirin,charin,charout):
"""
function which will rename all files in all Sub directories of a Directory by replacing : charin by charout
Parameters
* dirin : absolute or relative directory path
* charin : characters to replace
* charout : character to replace
Return the number of files which have been converted, or -1 if issue
"""
retval = 0
pDbg3("(renFileSubdir): Rename all files containing '{0}' by '{1}' in directory {2}".format(charin,charout,dirin) )
if not exists(dirin):
pError("(renFileSubdir): chek if : " + dirin +" exists")
retval = -1
return retval
for d in listdir(dirin): # lis all of item in a directory
pDbg4("(renFileSubdir): Checking dir :" + d)
if isdir(join(dirin, d)): # Check if it is directory
retval = retval + renFileDir(join(dirin, d),charin,charout)
return retval
# -----------------------------------------------------
# function which retrurn the number of file type in a directory
# -----------------------------------------------------
def cntExtFileDir(dirin,fext):
"""
function which return the number of file type in a directory
parameters
* dirin : absolute or relative directory path
* fext : file extension
Return the number of files which have been converted, or -1 if issue
"""
retval = 0
pDbg4("(cntExtFileDir): Count all files with extension '{0}' in directory {1}".format(fext,dirin) )
# test if Directory exists
if not exists(dirin):
pError("(cntExtFileDir): chek if : " + dirin +" exists")
retval = -1
return retval
for f in listdir(dirin): # lis all of item in a directory
if isfile(join(dirin, f)): # Check if 'f' it is file
pDbg5("(cntExtFileDir): Checking file: "+ f)
filename, file_extension = splitext(f)
if file_extension == fext :
retval = retval+1
return retval
# -----------------------------------------------------
# function which rename all Directories in a directory by replacing : charin by charout
# -----------------------------------------------------
def renSubDir(dirin,charin,charout):
"""
function which rename all Directories in a directory by replacing : charin by charout
Parameters
* dirin : absolute or relative directory path
* charin : characters to replace
* charout : character to replace
Return the number of files which have been converted, or -1 if issue
"""
retval = 0
pDbg3("(renSubDir): Rename all directories containing '{0}' by '{1}' in directory {2}".format(charin,charout,dirin) )
if not exists(dirin):
pError("(renSubDir): chek if : " + dirin +" exists")
retval = -1
return retval
for f in listdir(dirin): # lis all of item in a directory
if isdir(join(dirin, f)): # Check if it is directory
pDbg4("(renSubDir): Checking dir :" + f)
if f.find(charin) > 0: # Check if in the file there is charin ?
fout = f.replace(charin,charout)
rename(join(dirin, f),join(dirin, fout))
pDbg4("(renSubDir): --> new directory ="+ fout)
retval = retval+1
return retval
# -----------------------------------------------------
# Format the command to send to os.system
# -----------------------------------------------------
def formatCmd2os(cmd):
'''
Format the command to send to os.system
Example:
Aigle_D'Or,_Le_(1983) is a correct name for a Directory, or file: This data is saved in oricdic
However, for a Linux command the chars: "'", or "(", or ")" are not allowed alone, so we must add prefix "\"
Auto-completion in Linux will give
>>:~/Work/Orpic/src/webapp/static/Tapes$ ls Aigle_D\'Or,_Le_\(1984\)/
For example we will replace an (apostrophe) ' by \' in order to make a "cd"
'''
pDbg3("(formatCmd2os): in="+ cmd)
out = cmd
out = out.replace("'","\\'")
out = out.replace("(","\(")
out = out.replace(")","\)")
pDbg3("(formatCmd2os): out="+ out)
return(out)
def pFlaskAppConf(my_dict, printAll=False):
'''
Print the configuration information for Flask app.Config instance !
We will check the Debug Level to display the information
"WEBAPP_DEBUG_LEVEL" > 1 then the information is displayed
'''
DList = [
"WEBAPP_DEBUG_LEVEL",
"WEBAPP_VERSION",
"DEBUG",
"TAP2WAV_FORM_BAUD_ID",
"TAP2WAV_FORM_FREQUENCE_ID",
"TAP2WAV_FORM_SPLIT_ID",
"TAP2WAV_VERSION"
]
if my_dict["WEBAPP_DEBUG_LEVEL"] > 1:
if printAll == True:
for item in my_dict:
print("{0} = {1}".format(item,my_dict[item]))
else:
for item in my_dict:
for vl in DList:
if vl == item:
print("{0} = {1}".format(item,my_dict[item]))
# ######################################################
# TESTS
# ######################################################
if __name__ =='__main__':
# Test print debug level functions
setApplicationDebugLevel(3)
print("\nWill test print debug level functions (pDbg) with overall Applicationn Debug Level={0}".format(getApplicationDebugLevel()))
pError("Something is going wrong here")
pDbg0("Debug level 0")
pDbg1("Debug level 1")
pDbg2("Debug level 2")
pDbg3("Debug level 3")
pDbg4("Debug level 4")
pDbg5("Debug level 5")