devinit.py 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. #!/usr/bin/env python3
  2. #
  3. # MMGen Wallet, a terminal-based cryptocurrency wallet
  4. # Copyright (C)2013-2025 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-wallet
  9. # https://gitlab.com/mmgen/mmgen-wallet
  10. """
  11. devinit: Developer tools initialization 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. 'pmsg_r': lambda *args, **kwargs: devtools_call('pmsg_r', *args, **kwargs),
  17. 'pdie': lambda *args, **kwargs: devtools_call('pdie', *args, **kwargs),
  18. 'pexit': lambda *args, **kwargs: devtools_call('pexit', *args, **kwargs),
  19. 'Pmsg': lambda *args, **kwargs: devtools_call('Pmsg', *args, **kwargs),
  20. 'Pdie': lambda *args, **kwargs: devtools_call('Pdie', *args, **kwargs),
  21. 'Pexit': lambda *args, **kwargs: devtools_call('Pexit', *args, **kwargs),
  22. 'print_stack_trace': lambda *args, **kwargs: devtools_call('print_stack_trace', *args, **kwargs),
  23. 'get_diff': lambda *args, **kwargs: devtools_call('get_diff', *args, **kwargs),
  24. 'print_diff': lambda *args, **kwargs: devtools_call('print_diff', *args, **kwargs),
  25. 'get_ndiff': lambda *args, **kwargs: devtools_call('get_ndiff', *args, **kwargs),
  26. 'print_ndiff': lambda *args, **kwargs: devtools_call('print_ndiff', *args, **kwargs),
  27. }
  28. def devtools_call(funcname, *args, **kwargs):
  29. from . import devtools
  30. return getattr(devtools, funcname)(*args, **kwargs)
  31. def MMGenObject_call(methodname, *args, **kwargs):
  32. from .devtools import MMGenObjectMethods
  33. return getattr(MMGenObjectMethods, methodname)(*args, **kwargs)
  34. class MMGenObjectDevTools:
  35. pmsg = lambda *args, **kwargs: MMGenObject_call('pmsg', *args, **kwargs)
  36. pdie = lambda *args, **kwargs: MMGenObject_call('pdie', *args, **kwargs)
  37. pexit = lambda *args, **kwargs: MMGenObject_call('pexit', *args, **kwargs)
  38. pfmt = lambda *args, **kwargs: MMGenObject_call('pfmt', *args, **kwargs)
  39. # Check that all immutable attrs in ‘valid_attrs’ exist and have been initialized.
  40. # Expensive, so do only when testing.
  41. def immutable_attr_init_check(self):
  42. cls = type(self)
  43. for attrname in self.valid_attrs:
  44. # existence check:
  45. for o in cls.__mro__[:3]: # allow for 2 levels of subclassing
  46. if attrname in o.__dict__:
  47. attr = o.__dict__[attrname]
  48. break
  49. else:
  50. from .util import die
  51. die(4, f'unable to find descriptor {cls.__name__}.{attrname}')
  52. # initialization check:
  53. if type(attr).__name__ == 'ImmutableAttr' and attrname not in self.__dict__:
  54. from .util import die
  55. die(4, f'attribute {attrname!r} of {cls.__name__} has not been initialized in constructor!')
  56. def init_dev():
  57. import builtins
  58. # MMGenObject is added to the namespace by objmethods.py, so we must name the builtin differently
  59. # to avoid inadvertently adding MMGenObject to the global namespace here:
  60. setattr(builtins, 'MMGenObjectDevTools', MMGenObjectDevTools)
  61. for funcname, func in devtools_funcs.items():
  62. setattr(builtins, funcname, func)