flags.py 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. #
  6. # This program is free software: you can redistribute it and/or modify
  7. # it under the terms of the GNU General Public License as published by
  8. # the Free Software Foundation, either version 3 of the License, or
  9. # (at your option) any later version.
  10. #
  11. # This program 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
  14. # GNU General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU General Public License
  17. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. """
  19. flags: Class flags and opts for the MMGen suite
  20. """
  21. from .base_obj import AttrCtrl, Lockable
  22. from .util import fmt_list, die
  23. class ClassFlags(AttrCtrl):
  24. _name = 'flags'
  25. _desc = 'flag'
  26. reserved_attrs = ()
  27. def __init__(self, parent, arg):
  28. self._parent = parent
  29. self._available = getattr(self._parent, 'avail_'+self._name)
  30. for a in self._available:
  31. if a.startswith('_'):
  32. die('ClassFlagsError', f'{a!r}: {self._desc} cannot begin with an underscore')
  33. for b in self.reserved_attrs:
  34. if a == b:
  35. die('ClassFlagsError', f'{a!r}: {b} is a reserved name for {self._desc}')
  36. if arg:
  37. assert type(arg) in (list, tuple), f"{arg!r}: {self._name!r} must be list or tuple"
  38. else:
  39. arg = []
  40. for e in arg:
  41. if e not in self._available:
  42. self.not_available_error(e)
  43. for e in self._available:
  44. setattr(self, e, e in arg)
  45. def __dir__(self):
  46. return [k for k in self.__dict__ if not k.startswith('_') and not k in self.reserved_attrs]
  47. def __str__(self):
  48. return ' '.join(f'{k}={getattr(self, k)}' for k in dir(self))
  49. def __setattr__(self, name, val):
  50. if self._locked:
  51. if name not in self._available:
  52. self.not_available_error(name)
  53. if self._name == 'flags':
  54. assert isinstance(val, bool), f'{val!r} not boolean'
  55. old_val = getattr(self, name)
  56. if val and old_val:
  57. die('ClassFlagsError', f'{self._desc} {name!r} already set')
  58. if not val and not old_val:
  59. die('ClassFlagsError', f'{self._desc} {name!r} not set, so cannot be unset')
  60. super().__setattr__(name, val)
  61. def not_available_error(self, name):
  62. die('ClassFlagsError', '{!r}: unrecognized {} for {}: (available {}: {})'.format(
  63. name,
  64. self._desc,
  65. type(self._parent).__name__,
  66. self._name,
  67. fmt_list(self._available, fmt='bare')))
  68. class ClassOpts(ClassFlags, Lockable):
  69. _name = 'opts'
  70. _desc = 'opt'