4 # Copyright (C) 2010-2018 CEA/DEN
6 # This library is free software; you can redistribute it and/or
7 # modify it under the terms of the GNU Lesser General Public
8 # License as published by the Free Software Foundation; either
9 # version 2.1 of the License.
11 # This library is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 # Lesser General Public License for more details.
16 # You should have received a copy of the GNU Lesser General Public
17 # License along with this library; if not, write to the Free Software
18 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 This file assume DEBUG functionalities use
22 - print debug messages in sys.stderr for salomeTools
23 - show pretty print debug representation from instances of SAT classes
24 (pretty print src.pyconf.Config), and python dict/list etc. (as 'aVariable')
26 WARNING: obviously supposedly show messages in SAT development phase, not production
29 >> import debug as DBG
30 >> DBG.write("aTitle", aVariable) # not shown in production
31 >> DBG.write("aTitle", aVariable, True) # unconditionaly shown (as show=True)
33 to set show message as development phase:
34 >> DBG.push_debug(True)
36 to set no show message as production phase:
37 >> DBG.push_debug(False)
39 to set show message temporary as development phase, only in a method:
40 >> def aMethodToDebug(...):
41 >> DBG.push_debug(True) #force show as appended status
42 >> etc. method code with some DBG.write()
43 >> DBG.pop_debug(False) #restore previous status (show or not show)
46 to set a message for future fix, as temporary problem to not forget:
47 DBG.tofix("aTitle", aVariable, True/False) #True/False in production shown, or not
52 import StringIO as SIO
55 _debug = [False] #support push/pop for temporary activate debug outputs
57 def indent(text, amount=2, ch=' '):
58 """indent multi lines message"""
60 return ''.join(padding + line for line in text.splitlines(True))
62 def write(title, var="", force=None, fmt="\n#### DEBUG: %s:\n%s\n"):
63 """write sys.stderr a message if _debug[-1]==True or optionaly force=True"""
64 if _debug[-1] or force:
65 if 'src.pyconf.' in str(type(var)):
66 sys.stderr.write(fmt % (title, indent(getStrConfigDbg(var))))
67 elif type(var) is not str:
68 sys.stderr.write(fmt % (title, indent(PP.pformat(var))))
70 sys.stderr.write(fmt % (title, indent(var)))
73 def tofix(title, var="", force=None):
75 write sys.stderr a message if _debug[-1]==True or optionaly force=True
76 use this only if no logger accessible for classic
77 logger.warning(message) or logger.debug(message)
79 fmt = "\n#### TOFIX: %s:\n%s\n"
80 write(title, var, force, fmt)
82 def push_debug(aBool):
83 """set debug outputs activated, or not"""
87 """restore previous debug outputs status"""
91 sys.stderr.write("\nERROR: pop_debug: too much pop.")
94 ###############################################
95 # utilitaires divers pour debug
96 ###############################################
98 class OutStream(SIO.StringIO):
99 """utility class for pyconf.Config output iostream"""
101 """because Config.__save__ calls close() stream as file
102 keep value before lost as self.value
104 self.value = self.getvalue()
105 SIO.StringIO.close(self)
107 class InStream(SIO.StringIO):
108 """utility class for pyconf.Config input iostream"""
112 """get string for environment variables representation"""
114 for i in sorted(os.environ):
115 res += "%s : %s\n" % (i, os.environ[i])
118 # save as initial Config.save() moved as Config.__save__()
119 def saveConfigStd(config, aStream):
120 """returns as file .pyconf"""
122 config.__save__(aStream, indent)
124 def getStrConfigStd(config):
125 """set string as saveConfigStd,
127 outStream = OutStream()
128 saveConfigStd(config, outStream)
129 return outStream.value
131 def getStrConfigDbg(config):
132 """set string as saveConfigDbg,
133 as (path expression evaluation) for debug"""
134 outStream = OutStream()
135 saveConfigDbg(config, outStream)
136 return outStream.value
138 def saveConfigDbg(config, aStream, indent=0, path=""):
139 """pyconf returns multilines (path expression evaluation) for debug"""
140 _saveConfigRecursiveDbg(config, aStream, indent, path)
141 aStream.close() # as config.__save__()
143 def _saveConfigRecursiveDbg(config, aStream, indent, path):
144 """pyconf inspired from Mapping.__save__"""
149 indentp = indentp + 2
150 indstr = indent * ' ' # '':no indent, ' ':indent
151 strType = str(type(config))
152 if "Sequence" in strType:
153 for i in range(len(config)):
154 _saveConfigRecursiveDbg(config[i], aStream, indentp, path+"[%i]" % i)
157 order = object.__getattribute__(config, 'order')
158 data = object.__getattribute__(config, 'data')
160 aStream.write("%s%s : '%s'\n" % (indstr, path, str(config)))
162 for key in sorted(order):
164 strType = str(type(value))
165 if debug: print indstr + 'strType = %s' % strType, key
166 if "Config" in strType:
167 _saveConfigRecursiveDbg(value, aStream, indentp, path+"."+key)
169 if "Mapping" in strType:
170 _saveConfigRecursiveDbg(value, aStream, indentp, path+"."+key)
172 if "Sequence" in strType:
173 for i in range(len(value)):
174 _saveConfigRecursiveDbg(value[i], aStream, indentp, path+"."+key+"[%i]" % i)
176 if "Expression" in strType:
178 evaluate = value.evaluate(config)
179 aStream.write("%s%s.%s : %s --> '%s'\n" % (indstr, path, key, str(value), evaluate))
180 except Exception as e:
181 aStream.write("%s%s.%s : !!! ERROR: %s !!!\n" % (indstr, path, key, e.message))
183 if "Reference" in strType:
185 evaluate = value.resolve(config)
186 aStream.write("%s%s.%s : %s --> '%s'\n" % (indstr, path, key, str(value), evaluate))
187 except Exception as e:
188 aStream.write("%s%s.%s : !!! ERROR: %s !!!\n" % (indstr, path, key, e.message))
190 if type(value) in [str, bool, int, type(None), unicode]:
191 aStream.write("%s%s.%s : '%s'\n" % (indstr, path, key, str(value)))
194 aStream.write("!!! TODO fix that %s %s%s.%s : %s\n" % (type(value), indstr, path, key, str(value)))
195 except Exception as e:
196 aStream.write("%s%s.%s : !!! %s\n" % (indstr, path, key, e.message))