devtools.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. #!/usr/bin/env python3
  2. class MMGenObject(object):
  3. 'placeholder - overridden when testing'
  4. def immutable_attr_init_check(self): pass
  5. import os
  6. if os.getenv('MMGEN_DEBUG') or os.getenv('MMGEN_TEST_SUITE') or os.getenv('MMGEN_TRACEBACK'):
  7. import sys,re,traceback,json,pprint
  8. from decimal import Decimal
  9. from difflib import unified_diff,ndiff
  10. def pmsg(*args,out=sys.stderr):
  11. d = args if len(args) > 1 else '' if not args else args[0]
  12. out.write(pprint.PrettyPrinter(indent=4).pformat(d) + '\n')
  13. def pdie(*args,exit_val=1,out=sys.stderr):
  14. pmsg(*args,out=out)
  15. sys.exit(exit_val)
  16. def pexit(*args,out=sys.stderr):
  17. pdie(*args,exit_val=0,out=out)
  18. def Pmsg(*args):
  19. pmsg(*args,out=sys.stdout)
  20. def Pdie(*args):
  21. pdie(*args,out=sys.stdout)
  22. def Pexit(*args):
  23. pexit(*args,out=sys.stdout)
  24. def print_stack_trace(message=None,fh=[],nl='\n',sep='\n '):
  25. if not fh:
  26. fh.append(open(f'devtools.trace.{os.getpid()}','w'))
  27. nl = ''
  28. tb = [t for t in traceback.extract_stack() if t.filename[:1] != '<'][:-1]
  29. fs = '{}:{}: in {}:\n {}'
  30. out = [
  31. fs.format(
  32. re.sub(r'^\./','',os.path.relpath(t.filename)),
  33. t.lineno,
  34. (t.name+'()' if t.name[-1] != '>' else t.name),
  35. t.line or '(none)')
  36. for t in tb ]
  37. text = f'{nl}STACK TRACE {message or "[unnamed]"}:{sep}{sep.join(out)}\n'
  38. sys.stderr.write(text)
  39. fh[0].write(text)
  40. class MMGenObject(object):
  41. # Pretty-print any object subclassed from MMGenObject, recursing into sub-objects - WIP
  42. def pmsg(self):
  43. print(self.pfmt())
  44. def pdie(self):
  45. print(self.pfmt())
  46. sys.exit(1)
  47. def pfmt(self,lvl=0,id_list=[]):
  48. scalars = (str,int,float,Decimal)
  49. def do_list(out,e,lvl=0,is_dict=False):
  50. out.append('\n')
  51. for i in e:
  52. el = i if not is_dict else e[i]
  53. if is_dict:
  54. out.append('{s}{:<{l}}'.format(i,s=' '*(4*lvl+8),l=10,l2=8*(lvl+1)+8))
  55. if hasattr(el,'pfmt'):
  56. out.append('{:>{l}}{}'.format(
  57. '',
  58. el.pfmt( lvl=lvl+1, id_list=id_list+[id(self)] ),
  59. l = (lvl+1)*8 ))
  60. elif isinstance(el,scalars):
  61. if isList(e):
  62. out.append( '{:>{l}}{!r:16}\n'.format( '', el, l=lvl*8 ))
  63. else:
  64. out.append(f' {el!r}')
  65. elif isList(el) or isDict(el):
  66. indent = 1 if is_dict else lvl*8+4
  67. out.append('{:>{l}}{:16}'.format( '', f'<{type(el).__name__}>', l=indent ))
  68. if isList(el) and isinstance(el[0],scalars):
  69. out.append('\n')
  70. do_list(out,el,lvl=lvl+1,is_dict=isDict(el))
  71. else:
  72. out.append('{:>{l}}{:16} {!r}\n'.format( '', f'<{type(el).__name__}>', el, l=(lvl*8)+8 ))
  73. out.append('\n')
  74. if not e:
  75. out.append(f'{e!r}\n')
  76. def isDict(obj):
  77. return isinstance(obj,dict)
  78. def isList(obj):
  79. return isinstance(obj,list)
  80. def isScalar(obj):
  81. return isinstance(obj,scalars)
  82. out = [f'<{type(self).__name__}>{" "+repr(self) if isScalar(self) else ""}\n']
  83. if id(self) in id_list:
  84. return out[-1].rstrip() + ' [RECURSION]\n'
  85. if isList(self) or isDict(self):
  86. do_list(out,self,lvl=lvl,is_dict=isDict(self))
  87. for k in self.__dict__:
  88. e = getattr(self,k)
  89. if isList(e) or isDict(e):
  90. out.append('{:>{l}}{:<10} {:16}'.format( '', k, f'<{type(e).__name__}>', l=(lvl*8)+4 ))
  91. do_list(out,e,lvl=lvl,is_dict=isDict(e))
  92. elif hasattr(e,'pfmt') and type(e) != type:
  93. out.append('{:>{l}}{:10} {}'.format(
  94. '',
  95. k,
  96. e.pfmt( lvl=lvl+1, id_list=id_list+[id(self)] ),
  97. l = (lvl*8)+4 ))
  98. else:
  99. out.append('{:>{l}}{:<10} {:16} {}\n'.format(
  100. '',
  101. k,
  102. f'<{type(e).__name__}>',
  103. repr(e),
  104. l=(lvl*8)+4 ))
  105. import re
  106. return re.sub('\n+','\n',''.join(out))
  107. # Check that all immutables have been initialized. Expensive, so do only when testing.
  108. def immutable_attr_init_check(self):
  109. from .globalvars import g
  110. if g.test_suite:
  111. from .util import rdie
  112. cls = type(self)
  113. for attrname in sorted({a for a in self.valid_attrs if a[0] != '_'}):
  114. for o in (cls,cls.__bases__[0]): # assume there's only one base class
  115. if attrname in o.__dict__:
  116. attr = o.__dict__[attrname]
  117. break
  118. else:
  119. rdie(3,f'unable to find descriptor {cls.__name__}.{attrname}')
  120. if type(attr).__name__ == 'ImmutableAttr':
  121. if attrname not in self.__dict__:
  122. rdie(3,
  123. f'attribute {attrname!r} of {cls.__name__} has not been initialized in constructor!')
  124. def print_diff(a,b,from_file='',to_file='',from_json=True):
  125. if from_json:
  126. a = json.dumps(json.loads(a),indent=4).split('\n') if a else []
  127. b = json.dumps(json.loads(b),indent=4).split('\n') if b else []
  128. else:
  129. a = a.split('\n')
  130. b = b.split('\n')
  131. sys.stderr.write(' DIFF:\n {}\n'.format(
  132. '\n '.join(unified_diff(a,b,from_file,to_file)) ))
  133. def get_ndiff(a,b):
  134. a = a.split('\n')
  135. b = b.split('\n')
  136. return list(ndiff(a,b))