-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprintoutputdebug_module.py
More file actions
142 lines (140 loc) · 4.92 KB
/
Copy pathprintoutputdebug_module.py
File metadata and controls
142 lines (140 loc) · 4.92 KB
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
#!python3
#VERSION=0.7
import os
import io, re
from datetime import datetime
from pprint import pprint
myoutputs=None
NORMAL = 1
VERBOSE = 2
DEBUG = 4
SILENT = 0
class outputs:
global NORMAL
global VERBOSE
global DEBUG
global SILENT
def __init__(self,level=NORMAL,printout=True,debugFileName='',outputFileName='',logFileName=''):
self.NORMAL = 1
self.VERBOSE = 2
self.DEBUG = 4
self.SILENT = 0
self.printout=printout
self.debugFileName=debugFileName
self.outputFileName=outputFileName
self.logFileName=logFileName
self.suspendprintout=False
self.suspenddebug=False
if level:
self.setLevel(level)
else:
self.setLevel(NORMAL)
#self.progName, self.file_extension = os.path.splitext(sys.argv[0])
def setLevel(self,lvl):
if type(lvl) is str:
lvl1=lvl.upper()
if lvl1=="NONE": lvl=NONE
if lvl1=="NORMAL": lvl=NORMAL
if lvl1=="VERBOSE": lvl= VERBOSE
if lvl1=="DEBUG": lvl=DEBUG
if lvl1=="SILENT": lvl=SILENT
self.level=lvl
#INIT files
thedate=datetime.strftime(datetime.now(),'%Y-%m-%d %H:%M:%S')
msg='BEGIN:'+thedate
if self.level == DEBUG:
folder=os.path.dirname(self.debugFileName)
fdebug = open(self.debugFileName, "w")
fdebug.write(msg+'\n')
fdebug.close()
if self.logFileName != "":
folder=os.path.dirname(self.logFileName)
if folder=='': folder='./'
flog = open(self.logFileName, "w")
flog.write(msg+'\n')
flog.close()
if self.outputFileName != "":
folder=os.path.dirname(self.outputFileName)
if folder=='': folder='./'
fout = open(self.outputFileName, "w")
fout.write('')
fout.close()
def humanJsonFormat(self,jsonToConvert,depth=None, compact=False):
"""
used for printing with the format JSON not human python
"""
f = io.StringIO()
pprint(jsonToConvert,f,depth=depth, compact=compact)
txt=f.getvalue()
return txt
def list_cmpact(self,jsonToConvert,depth=99):
"""
used for printing List one item per line
"""
f = io.StringIO()
pprint(jsonToConvert,f)
txt=f.getvalue()
sub=re.findall(r'(\n +)[^]|(\r\n )[^]',txt)
sub2=list(reversed(sorted(list(dict.fromkeys(sub)))))
l=len(sub2)
for indx,r in enumerate(sub2):
if l-indx > depth:
txt=txt.replace(r,"")
return txt
def echo(self,messagelevel,msg):
if self.level>=messagelevel and self.printout:
if not self.suspendprintout:
print(msg)
if self.level==DEBUG and not self.suspenddebug:
if os.path.exists(self.debugFileName):
fdebug = open(self.debugFileName, "a")
fdebug.write(msg+'\n')
fdebug.close()
else:
fdebug = open(self.debugFileName, "w")
fdebug.write(msg+'\n')
fdebug.close()
if self.logFileName != "":
lvlmaxlog=self.level
if lvlmaxlog>=DEBUG: lvlmaxlog=VERBOSE
if (lvlmaxlog>=messagelevel and not self.suspendprintout) or (lvlmaxlog==DEBUG and not self.suspenddebug):
if os.path.exists(self.logFileName):
flog = open(self.logFileName, "a")
flog.write(msg+'\n')
flog.close()
else:
flog = open(self.logFileName, "w")
flog.write(msg+'\n')
flog.close()
def suspend_print(self):
#help for reducing debug between modules
self.suspendprintout=True
def continue_print(self):
self.suspendprintout=False
def suspend_debug(self):
#help for reducing debug between modules
self.suspenddebug=True
def continue_debug(self):
self.suspenddebug=False
def writeOutput(self,mode='w',text='',filename=''):
msg=str(text)
outputFileName=self.outputFileName
if filename!='': outputFileName=filename
if outputFileName!='':
if os.path.exists(outputFileName):
foutput = open(outputFileName, mode)
foutput.write(msg+'\n')
else:
foutput = open(outputFileName, "w")
foutput.write(msg+'\n')
def writeLog(self,mode='w',text='',filename=''):
msg=str(text)
logFileName=self.logFileName
if filename!='': logFileName=filename
if logFileName!='':
if os.path.exists(logFileName):
foutput = open(logFileName, mode)
foutput.write(msg+'\n')
else:
foutput = open(logFileName, "w")
foutput.write(msg+'\n')