comparison sat_frontends/tools/misc.py @ 2615:b4ecbcc2fd08

tools (misc): do not remove flags while using them anymore, instead added "all_used" and "unused" properties in FlagsHandler
author Goffi <goffi@goffi.org>
date Thu, 21 Jun 2018 01:21:44 +0200
parents 26edcf3a30eb
children 56f94936df1e
comparison
equal deleted inserted replaced
2614:a5b96950b81a 2615:b4ecbcc2fd08
65 class FlagsHandler(object): 65 class FlagsHandler(object):
66 """Small class to handle easily option flags 66 """Small class to handle easily option flags
67 67
68 the instance is initialized with an iterable 68 the instance is initialized with an iterable
69 then attribute return True if flag is set, False else. 69 then attribute return True if flag is set, False else.
70 WARNING: If a flag is checked, it is removed (i.e. unset)
71 this is done to use bool(flags_handler) to check if all flags
72 have been used
73 """ 70 """
74 71
75 def __init__(self, flags): 72 def __init__(self, flags):
76 self.flags = set(flags or []) 73 self.flags = set(flags or [])
74 self._used_flags = set()
77 75
78 def __getattr__(self, flag): 76 def __getattr__(self, flag):
79 try: 77 self._used_flags.add(flag)
80 self.flags.remove(flag) 78 return flag in self.flags
81 except KeyError:
82 return False
83 else:
84 return True
85 79
86 def __len__(self): 80 def __len__(self):
87 return len(self.flags) 81 return len(self.flags)
88 82
89 def __iter__(self): 83 def __iter__(self):
90 return self.flags.__iter__() 84 return self.flags.__iter__()
85
86 @property
87 def all_used(self):
88 """Return True if all flags have been used"""
89 return self._used_flags.issuperset(self.flags)
90
91 @property
92 def unused(self):
93 """Return flags which has not been used yet"""
94 return self.flags.difference(self._used_flags)
95