diff sat_frontends/quick_frontend/quick_widgets.py @ 2624:56f94936df1e

code style reformatting using black
author Goffi <goffi@goffi.org>
date Wed, 27 Jun 2018 20:14:46 +0200
parents 26edcf3a30eb
children 003b8b4b56a7
line wrap: on
line diff
--- a/sat_frontends/quick_frontend/quick_widgets.py	Wed Jun 27 07:51:29 2018 +0200
+++ b/sat_frontends/quick_frontend/quick_widgets.py	Wed Jun 27 20:14:46 2018 +0200
@@ -18,19 +18,23 @@
 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
 from sat.core.log import getLogger
+
 log = getLogger(__name__)
 from sat.core import exceptions
 from sat_frontends.quick_frontend.constants import Const as C
 
 
-NEW_INSTANCE_SUFF = '_new_instance_'
+NEW_INSTANCE_SUFF = "_new_instance_"
 classes_map = {}
 
 
 try:
     # FIXME: to be removed when an acceptable solution is here
-    unicode('') # XXX: unicode doesn't exist in pyjamas
-except (TypeError, AttributeError): # Error raised is not the same depending on pyjsbuild options
+    unicode("")  # XXX: unicode doesn't exist in pyjamas
+except (
+    TypeError,
+    AttributeError,
+):  # Error raised is not the same depending on pyjsbuild options
     unicode = str
 
 
@@ -77,7 +81,9 @@
         except KeyError:
             cls = class_
         if cls is None:
-            raise exceptions.InternalError("There is not class registered for {}".format(class_))
+            raise exceptions.InternalError(
+                "There is not class registered for {}".format(class_)
+            )
         return cls
 
     def getRootHash(self, hash_):
@@ -109,7 +115,10 @@
                 filter_hash = None
             for w_hash, w in widgets_map.iteritems():
                 if profiles is None or w.profiles.intersection(profiles):
-                    if filter_hash is not None and self.getRootHash(w_hash) != filter_hash:
+                    if (
+                        filter_hash is not None
+                        and self.getRootHash(w_hash) != filter_hash
+                    ):
                         continue
                     yield w
 
@@ -161,39 +170,45 @@
         cls = self.getRealClass(class_)
 
         ## arguments management ##
-        _args = [self.host, target] + list(args) or [] # FIXME: check if it's really necessary to use optional args
+        _args = [self.host, target] + list(
+            args
+        ) or []  # FIXME: check if it's really necessary to use optional args
         _kwargs = kwargs or {}
-        if 'profiles' in _kwargs and 'profile' in _kwargs:
-            raise ValueError("You can't have 'profile' and 'profiles' keys at the same time")
+        if "profiles" in _kwargs and "profile" in _kwargs:
+            raise ValueError(
+                "You can't have 'profile' and 'profiles' keys at the same time"
+            )
         try:
-            _kwargs['profiles'] = [_kwargs.pop('profile')]
+            _kwargs["profiles"] = [_kwargs.pop("profile")]
         except KeyError:
-            if not 'profiles' in _kwargs:
-                _kwargs['profiles'] = None
+            if not "profiles" in _kwargs:
+                _kwargs["profiles"] = None
 
-        #on_new_widget tell what to do for the new widget creation
+        # on_new_widget tell what to do for the new widget creation
         try:
-            on_new_widget = _kwargs.pop('on_new_widget')
+            on_new_widget = _kwargs.pop("on_new_widget")
         except KeyError:
             on_new_widget = C.WIDGET_NEW
 
-        #on_existing_widget tell what to do when the widget already exists
+        # on_existing_widget tell what to do when the widget already exists
         try:
-            on_existing_widget = _kwargs.pop('on_existing_widget')
+            on_existing_widget = _kwargs.pop("on_existing_widget")
         except KeyError:
             on_existing_widget = C.WIDGET_KEEP
 
         ## we get the hash ##
         try:
-            hash_ = _kwargs.pop('force_hash')
+            hash_ = _kwargs.pop("force_hash")
         except KeyError:
-            hash_ = cls.getWidgetHash(target, _kwargs['profiles'])
+            hash_ = cls.getWidgetHash(target, _kwargs["profiles"])
 
         ## widget creation or retrieval ##
 
-        widgets_map = self._widgets.setdefault(cls.__name__, {}) # we sorts widgets by classes
+        widgets_map = self._widgets.setdefault(
+            cls.__name__, {}
+        )  # we sorts widgets by classes
         if not cls.SINGLE:
-            widget = None # if the class is not SINGLE, we always create a new widget
+            widget = None  # if the class is not SINGLE, we always create a new widget
         else:
             try:
                 widget = widgets_map[hash_]
@@ -225,16 +240,20 @@
                 #     we need to get rid of kwargs special options
                 new_kwargs = kwargs.copy()
                 try:
-                    new_kwargs.pop('force_hash')  # FIXME: we use pop instead of del here because pyjamas doesn't raise error on del
+                    new_kwargs.pop(
+                        "force_hash"
+                    )  # FIXME: we use pop instead of del here because pyjamas doesn't raise error on del
                 except KeyError:
                     pass
                 else:
-                    raise ValueError("force_hash option can't be used with on_existing_widget=RECREATE")
+                    raise ValueError(
+                        "force_hash option can't be used with on_existing_widget=RECREATE"
+                    )
 
-                new_kwargs['on_new_widget'] = on_new_widget
+                new_kwargs["on_new_widget"] = on_new_widget
 
                 # XXX: keep up-to-date if new special kwargs are added (i.e.: delete these keys here)
-                new_kwargs['on_existing_widget'] = C.WIDGET_RAISE
+                new_kwargs["on_existing_widget"] = C.WIDGET_RAISE
                 try:
                     recreateArgs = widget.recreateArgs
                 except AttributeError:
@@ -243,18 +262,28 @@
                     recreateArgs(args, new_kwargs)
                 hash_idx = 1
                 while True:
-                    new_kwargs['force_hash'] = "{}{}{}".format(hash_, NEW_INSTANCE_SUFF, hash_idx)
+                    new_kwargs["force_hash"] = "{}{}{}".format(
+                        hash_, NEW_INSTANCE_SUFF, hash_idx
+                    )
                     try:
-                        widget = self.getOrCreateWidget(class_, target, *args, **new_kwargs)
+                        widget = self.getOrCreateWidget(
+                            class_, target, *args, **new_kwargs
+                        )
                     except WidgetAlreadyExistsError:
                         hash_idx += 1
                     else:
-                        log.debug(u"Widget already exists, a new one has been recreated with hash {}".format(new_kwargs['force_hash']))
+                        log.debug(
+                            u"Widget already exists, a new one has been recreated with hash {}".format(
+                                new_kwargs["force_hash"]
+                            )
+                        )
                         break
             elif callable(on_existing_widget):
                 on_existing_widget(widget)
             else:
-                raise exceptions.InternalError("Unexpected on_existing_widget value ({})".format(on_existing_widget))
+                raise exceptions.InternalError(
+                    "Unexpected on_existing_widget value ({})".format(on_existing_widget)
+                )
 
         return widget
 
@@ -286,9 +315,10 @@
 
 class QuickWidget(object):
     """generic widget base"""
-    SINGLE=True # if True, there can be only one widget per target(s)
-    PROFILES_MULTIPLE=False # If True, this widget can handle several profiles at once
-    PROFILES_ALLOW_NONE=False # If True, this widget can be used without profile
+
+    SINGLE = True  # if True, there can be only one widget per target(s)
+    PROFILES_MULTIPLE = False  # If True, this widget can handle several profiles at once
+    PROFILES_ALLOW_NONE = False  # If True, this widget can be used without profile
 
     def __init__(self, host, target, profiles=None):
         """
@@ -317,7 +347,11 @@
 
     @property
     def profile(self):
-        assert len(self.profiles) == 1 and not self.PROFILES_MULTIPLE and not self.PROFILES_ALLOW_NONE
+        assert (
+            len(self.profiles) == 1
+            and not self.PROFILES_MULTIPLE
+            and not self.PROFILES_ALLOW_NONE
+        )
         return list(self.profiles)[0]
 
     def addTarget(self, target):
@@ -350,7 +384,7 @@
         @param profiles: profile(s) associated to target, see __init__ docstring
         @return: a hash (can correspond to one or many targets or profiles, depending of widget class)
         """
-        return unicode(target) # by defaut, there is one hash for one target
+        return unicode(target)  # by defaut, there is one hash for one target
 
     def onDelete(self, *args, **kwargs):
         """Called when a widget is being deleted
@@ -360,4 +394,3 @@
         """
         log.debug(u"widget {} deleted".format(self))
         return True
-