Mercurial > libervia-backend
comparison frontends/src/tools/misc.py @ 2531:1dfc5516dead
frontends (tools/misc): new FlagsHandler class, a simple class to help handling option flags
author | Goffi <goffi@goffi.org> |
---|---|
date | Wed, 21 Mar 2018 19:07:06 +0100 |
parents | 2daf7b4c6756 |
children |
comparison
equal
deleted
inserted
replaced
2530:0cb32e503aff | 2531:1dfc5516dead |
---|---|
58 index_tmp = self.input_history_index + step | 58 index_tmp = self.input_history_index + step |
59 if index_tmp >= 0 and index_tmp < len(self.input_history_tmp): | 59 if index_tmp >= 0 and index_tmp < len(self.input_history_tmp): |
60 if callback is not None: | 60 if callback is not None: |
61 callback(self.input_history_tmp[index_tmp]) | 61 callback(self.input_history_tmp[index_tmp]) |
62 self.input_history_index = index_tmp | 62 self.input_history_index = index_tmp |
63 | |
64 | |
65 class FlagsHandler(object): | |
66 """Small class to handle easily option flags | |
67 | |
68 the instance is initialized with an iterable | |
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 """ | |
74 | |
75 def __init__(self, flags): | |
76 self.flags = set(flags or []) | |
77 | |
78 def __getattr__(self, flag): | |
79 try: | |
80 self.flags.remove(flag) | |
81 except KeyError: | |
82 return False | |
83 else: | |
84 return True | |
85 | |
86 def __len__(self): | |
87 return len(self.flags) | |
88 | |
89 def __iter__(self): | |
90 return self.flags.__iter__() |