changeset 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 a5b96950b81a
children 1cc88adb5142
files sat_frontends/jp/base.py sat_frontends/tools/misc.py
diffstat 2 files changed, 16 insertions(+), 11 deletions(-) [+]
line wrap: on
line diff
--- a/sat_frontends/jp/base.py	Thu Jun 21 01:21:44 2018 +0200
+++ b/sat_frontends/jp/base.py	Thu Jun 21 01:21:44 2018 +0200
@@ -369,8 +369,8 @@
                 pubsub_group.add_argument("-m", "--max", type=int, default=10,
                     help=_(u"maximum number of items to get ({no_limit} to get all items)".format(no_limit=C.NO_LIMIT)))
 
-        if flags:
-            raise exceptions.InternalError('unknowns flags: {flags}'.format(flags=u', '.join(flags)))
+        if not flags.all_used:
+            raise exceptions.InternalError('unknown flags: {flags}'.format(flags=u', '.join(flags.unused)))
         if defaults:
             raise exceptions.InternalError('unused defaults: {defaults}'.format(defaults=defaults))
 
--- a/sat_frontends/tools/misc.py	Thu Jun 21 01:21:44 2018 +0200
+++ b/sat_frontends/tools/misc.py	Thu Jun 21 01:21:44 2018 +0200
@@ -67,24 +67,29 @@
 
     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 [])
+        self._used_flags = set()
 
     def __getattr__(self, flag):
-        try:
-            self.flags.remove(flag)
-        except KeyError:
-            return False
-        else:
-            return True
+        self._used_flags.add(flag)
+        return flag in self.flags
 
     def __len__(self):
         return len(self.flags)
 
     def __iter__(self):
         return self.flags.__iter__()
+
+    @property
+    def all_used(self):
+        """Return True if all flags have been used"""
+        return self._used_flags.issuperset(self.flags)
+
+    @property
+    def unused(self):
+        """Return flags which has not been used yet"""
+        return self.flags.difference(self._used_flags)
+