flags.py 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. #!/usr/bin/env python3
  2. #
  3. # mmgen = Multi-Mode GENerator, command-line Bitcoin cold storage solution
  4. # Copyright (C)2013-2021 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.py: Class flags and opts for the MMGen suite
  20. """
  21. from .exception import ClassFlagsError
  22. from .base_obj import AttrCtrl,Lockable
  23. from .util import fmt_list
  24. class ClassFlags(AttrCtrl):
  25. _name = 'flags'
  26. _desc = 'flag'
  27. reserved_attrs = ('lock',)
  28. def __init__(self,parent,arg):
  29. self._parent = parent
  30. self._available = getattr(self._parent,'avail_'+self._name)
  31. for a in self._available:
  32. if a.startswith('_'):
  33. raise ClassFlagsError(f'{a!r}: {self._desc} cannot begin with an underscore')
  34. for b in self.reserved_attrs:
  35. if a == b:
  36. raise ClassFlagsError(f'{a!r}: {b} is a reserved name for {self._desc}')
  37. if arg:
  38. assert type(arg) in (list,tuple), f"{arg!r}: {self._name!r} must be list or tuple"
  39. else:
  40. arg = []
  41. for e in arg:
  42. if e not in self._available:
  43. self.not_available_error(e)
  44. for e in self._available:
  45. setattr(self,e,e in arg)
  46. def __dir__(self):
  47. return [k for k in self.__dict__ if not k.startswith('_') and not k in self.reserved_attrs]
  48. def __str__(self):
  49. return ' '.join(f'{k}={getattr(self,k)}' for k in dir(self))
  50. def __setattr__(self,name,val):
  51. if self._lock:
  52. if name not in self._available:
  53. self.not_available_error(name)
  54. if self._name == 'flags':
  55. assert type(val) is bool, f'{val!r} not boolean'
  56. old_val = getattr(self,name)
  57. if val and old_val:
  58. raise ClassFlagsError(f'{self._desc} {name!r} already set')
  59. if not val and not old_val:
  60. raise ClassFlagsError(f'{self._desc} {name!r} not set, so cannot be unset')
  61. super().__setattr__(name,val)
  62. def not_available_error(self,name):
  63. raise ClassFlagsError('{!r}: unrecognized {} for {}: (available {}: {})'.format(
  64. name,
  65. self._desc,
  66. type(self._parent).__name__,
  67. self._name,
  68. fmt_list(self._available,fmt='bare') ))
  69. class ClassOpts(ClassFlags,Lockable):
  70. _name = 'opts'
  71. _desc = 'opt'