changeset 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 0cb32e503aff
children 772447ec070f
files frontends/src/tools/misc.py
diffstat 1 files changed, 28 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/frontends/src/tools/misc.py	Wed Mar 21 19:07:00 2018 +0100
+++ b/frontends/src/tools/misc.py	Wed Mar 21 19:07:06 2018 +0100
@@ -60,3 +60,31 @@
             if callback is not None:
                 callback(self.input_history_tmp[index_tmp])
             self.input_history_index = index_tmp
+
+
+class FlagsHandler(object):
+    """Small class to handle easily option flags
+
+    the instance is initialized with an iterable
+    then attribute return True if flag is set, False else.
+    WARNING: If a flag is checked, it is removed (i.e. unset)
+        this is done to use bool(flags_handler) to check if all flags
+        have been used
+    """
+
+    def __init__(self, flags):
+        self.flags = set(flags or [])
+
+    def __getattr__(self, flag):
+        try:
+            self.flags.remove(flag)
+        except KeyError:
+            return False
+        else:
+            return True
+
+    def __len__(self):
+        return len(self.flags)
+
+    def __iter__(self):
+        return self.flags.__iter__()