devinit.py 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. #!/usr/bin/env python3
  2. #
  3. # mmgen = Multi-Mode GENerator, a command-line cryptocurrency wallet
  4. # Copyright (C)2013-2022 The MMGen Project <mmgen@tuta.io>
  5. # Licensed under the GNU General Public License, Version 3:
  6. # https://www.gnu.org/licenses
  7. # Public project repositories:
  8. # https://github.com/mmgen/mmgen
  9. # https://gitlab.com/mmgen/mmgen
  10. """
  11. devinit: Developer tools init/launch code for the MMGen suite
  12. """
  13. devtools_funcs = {
  14. 'pfmt': lambda *args,**kwargs: devtools_call('pfmt',*args,**kwargs),
  15. 'pmsg': lambda *args,**kwargs: devtools_call('pmsg',*args,**kwargs),
  16. 'pdie': lambda *args,**kwargs: devtools_call('pdie',*args,**kwargs),
  17. 'pexit': lambda *args,**kwargs: devtools_call('pexit',*args,**kwargs),
  18. 'Pmsg': lambda *args,**kwargs: devtools_call('Pmsg',*args,**kwargs),
  19. 'Pdie': lambda *args,**kwargs: devtools_call('Pdie',*args,**kwargs),
  20. 'Pexit': lambda *args,**kwargs: devtools_call('Pexit',*args,**kwargs),
  21. 'print_stack_trace': lambda *args,**kwargs: devtools_call('print_stack_trace',*args,**kwargs),
  22. 'get_diff': lambda *args,**kwargs: devtools_call('get_diff',*args,**kwargs),
  23. 'print_diff': lambda *args,**kwargs: devtools_call('print_diff',*args,**kwargs),
  24. 'get_ndiff': lambda *args,**kwargs: devtools_call('get_ndiff',*args,**kwargs),
  25. 'print_ndiff': lambda *args,**kwargs: devtools_call('print_ndiff',*args,**kwargs),
  26. }
  27. def devtools_call(funcname,*args,**kwargs):
  28. import mmgen.devtools
  29. return getattr(mmgen.devtools,funcname)(*args,**kwargs)
  30. def MMGenObject_call(methodname,*args,**kwargs):
  31. from .devtools import MMGenObjectMethods
  32. return getattr(MMGenObjectMethods,methodname)(*args,**kwargs)
  33. class MMGenObjectDevTools:
  34. pmsg = lambda *args,**kwargs: MMGenObject_call('pmsg',*args,**kwargs)
  35. pdie = lambda *args,**kwargs: MMGenObject_call('pdie',*args,**kwargs)
  36. pexit = lambda *args,**kwargs: MMGenObject_call('pexit',*args,**kwargs)
  37. pfmt = lambda *args,**kwargs: MMGenObject_call('pfmt',*args,**kwargs)
  38. # Check that all immutables have been initialized. Expensive, so do only when testing.
  39. def immutable_attr_init_check(self):
  40. cls = type(self)
  41. for attrname in self.valid_attrs:
  42. for o in (cls,cls.__bases__[0]): # assume there's only one base class
  43. if attrname in o.__dict__:
  44. attr = o.__dict__[attrname]
  45. break
  46. else:
  47. from .util import die
  48. die(4,f'unable to find descriptor {cls.__name__}.{attrname}')
  49. if type(attr).__name__ == 'ImmutableAttr' and attrname not in self.__dict__:
  50. from .util import die
  51. die(4,f'attribute {attrname!r} of {cls.__name__} has not been initialized in constructor!')
  52. def init_dev():
  53. import builtins
  54. # MMGenObject is added to the namespace by objmethods.py, so we must name the builtin differently
  55. # to avoid inadvertently adding MMGenObject to the global namespace here:
  56. setattr(builtins,'MMGenObjectDevTools',MMGenObjectDevTools)
  57. for funcname,func in devtools_funcs.items():
  58. setattr(builtins,funcname,func)