Package gmisclib :: Module die2
[frames] | no frames]

Source Code for Module gmisclib.die2

  1   
  2   
  3   
  4   
  5  import inspect 
  6  import sys 
  7  import traceback 
  8   
  9  debug = True    # Set to False to disable dbg() messages. 
 10  compress = True 
 11   
 12  _name = sys.argv[0] 
 13  _ni = _name.rfind('/') 
 14  if _ni >=0 and _ni != len(_name)-1: 
 15          _name = _name[_ni+1:] 
 16   
 17  logfd = sys.stderr 
 18   
 19  _last = () 
 20  _counter = 0 
 21   
 22   
23 -def note(name, val):
24 stack = inspect.stack() 25 uplocal = stack[1][0].f_locals 26 stack = None 27 if not uplocal.has_key('__note'): 28 uplocal['__note'] = {} 29 uplocal['__note'][name] = val 30 uplocal = None
31 32
33 -def show(name):
34 stack = inspect.stack() 35 uplocal = stack[1][0].f_locals 36 stack = None 37 if not uplocal.has_key('__show'): 38 uplocal['__show'] = [] 39 uplocal['__show'].append(name) 40 uplocal = None
41 42 43
44 -def _notes(n):
45 # __note = {'WHOOPS': 'WHOOPS'} 46 stack = inspect.stack() 47 o = [] 48 for level in range(len(stack)-1, n, -1): 49 uplocal = stack[level][0].f_locals 50 if '__show' in uplocal: 51 for k in uplocal['__show']: 52 if k in uplocal: 53 o.append( (k, uplocal[k]) ) 54 del uplocal['__show'] 55 56 if '__note' in uplocal: 57 o.extend( uplocal['__note'].items() ) 58 del uplocal['__note'] 59 60 del uplocal 61 del stack 62 o.sort() 63 64 # Delete duplicate values. This can happen if 65 # a variable is noted at two different levels. 66 i = 1 67 while i < len(o): 68 if o[i] == o[i-1]: 69 del o[i] 70 else: 71 i += 1 72 73 return o
74 75
76 -def _dumpmem(n):
77 logfd.flush() 78 if logfd is not sys.stderr: 79 sys.stderr.flush() 80 sys.stdout.flush() 81 for (k, v) in _notes(n+1): 82 logfd.write('#NOTE: %s = %s\n' % (k, v)) 83 logfd.flush()
84 85
86 -def _display(prefix, name, text, level):
87 global _counter 88 global _last 89 if compress and _last == (prefix, name, text): 90 _counter += 1 91 return 92 elif _counter > 0: 93 logfd.write('#INFO: : preceeding item repeated %d times.\n' % (_counter+1)) 94 _counter = 0 95 _last = () 96 _dumpmem(level+1) 97 logfd.write('#%s: %s: %s\n' % (prefix, name, text)) 98 _last = (prefix, name, text) 99 logfd.flush()
100 101
102 -def die(s):
103 """Output a fatal error message and terminate.""" 104 e = 'ERR: %s: %s' % (_name, s) 105 exit(1, e)
106 107
108 -def warn(s):
109 """Output a non-fatal warning.""" 110 # __note = {'WHOOPS': 'WHOOPS'} 111 _display('WARN', _name, s, 1)
112 113
114 -def info(s):
115 """Output useful information.""" 116 global _q 117 _display('INFO', _name, s, 1)
118 119
120 -def catch(extext=None):
121 """Call this inside an except statement. 122 It will report the exception and any other information it has.""" 123 type, value, tback = sys.exc_info() 124 if extext is None: 125 extext = "die.catch: exception caught.\n" 126 _dumpmem(1) 127 traceback.print_exception(type, value, tback) 128 sys.stderr.flush() 129 sys.stdout.flush()
130 131
132 -def catchexit(extext=None, n=1, text=None):
133 """Call this inside an except statement. It will report 134 all information and then exit.""" 135 type, value, tback = sys.exc_info() 136 if extext is None: 137 extext = "die.catch: exception caught.\n" 138 _dumpmem(1) 139 traceback.print_exception(type, value, tback) 140 sys.stderr.flush() 141 sys.stdout.flush() 142 if text is not None: 143 sys.stdout.write('%s\n' % text) 144 sys.stdout.flush() 145 logfd.write('%s\n' % text) 146 logfd.flush() 147 sys.exit(n)
148 149
150 -def dbg(s):
151 """Output debugging information, if debug is nonzero.""" 152 if debug: 153 _display('DBG', _name, s, 1)
154 155
156 -def exit(n, text=None):
157 """Exit, after dumping accumulated messages.""" 158 _dumpmem(1) 159 if text is not None: 160 sys.stdout.write('%s\n' % text) 161 sys.stdout.flush() 162 logfd.write('%s\n' % text) 163 logfd.flush() 164 sys.exit(n)
165 166
167 -def get(key):
168 nl = _notes(0) 169 for (k, v) in nl: 170 if k == key: 171 return v 172 raise KeyError, key
173 174 175 176 if __name__ == '__main__': 177 debug = 1 178 info("You should see a debug message next.") 179 dbg("This is the debug message.") 180 debug = 0 181 note("gleep", "oldest note") 182 note("foo", "bar") 183 note("foo", "fleep") 184 note("foo", "foo") 185 note("farf", "newest note") 186 info("You should not see a debug message next.") 187 dbg("This is the debug message you shouldn't see.") 188 warn("This is a warning.") 189 warn("This is a warning.") 190 warn("This is a warning.") 191 info("You should have seen three identical warnings just above.") 192 die("This is the end.") 193