Browse Source

added py2 compat version of fluid and strict

gwillz 3 years ago
parent
commit
551ea170d3
5 changed files with 58 additions and 3 deletions
  1. 7 2
      avent/__init__.py
  2. 0 0
      avent/compat/__init__.py
  3. 17 0
      avent/compat/fluid.py
  4. 33 0
      avent/compat/strict.py
  5. 1 1
      avent/utils.py

+ 7 - 2
avent/__init__.py

@@ -1,6 +1,11 @@
 from avent.abject import Abject
 from avent.avent_impl import Avent
 from avent.event import Event
-from avent.strict import Strict
-from avent.fluid import Fluid
 from avent.utils import wait_for, ArgsError
+
+try:
+    from avent.strict import Strict
+    from avent.fluid import Fluid
+except (ImportError, SyntaxError):
+    from avent.compat.strict import Strict
+    from avent.compat.fluid import Fluid

+ 0 - 0
avent/compat/__init__.py


+ 17 - 0
avent/compat/fluid.py

@@ -0,0 +1,17 @@
+from avent.event import Event
+from avent.utils import ensure_fire, fire_me
+
+class Fluid(Event):
+    """
+    The 'Fluid Event' will accept any number of arguments when being fired.
+    It also accepts handlers with varying arguments without error. Excessive
+    args are filled with `None`.
+    """
+    
+    def fire(self, *args):
+        results = []
+        for result in ensure_fire(self.handlers, args, wrap=fire_me):
+            results.append(result)
+        return results
+    
+    __call__ = fire

+ 33 - 0
avent/compat/strict.py

@@ -0,0 +1,33 @@
+from avent.event import Event
+from avent.utils import ensure_fire, check_missing, ArgsError
+
+class Strict(Event):
+    """
+    The 'Strict Event' requires definition of the args on init. It will then
+    raise an `ArgsError` whenever these requirements are not adhered to. This
+    includes checking of `fire()` parameters and handler function args.
+    """
+    
+    def __init__(self, *arg_names):
+        if len(set(arg_names)) != len(arg_names):
+            raise ArgsError("Cannot accept args of same name")
+        
+        self.args = arg_names
+        Event.__init__(self)
+    
+    def fire(self, *args):
+        check_missing(self.args, args)
+        
+        results = []
+        for result in ensure_fire(self.handlers, args):
+            results.append(result)
+        
+        return results
+    
+    def add(self, handler):
+        check_missing(self.args, handler.__code__.co_varnames)
+        return Event.add(self, handler)
+    
+    __iadd__ = add
+    __call__ = fire
+    

+ 1 - 1
avent/utils.py

@@ -52,7 +52,7 @@ def ensure_fire(handlers, args, wrap=None):
             else:
                 yield h(*args)
         
-        except Exception as e:
+        except Exception as e: # pylint: disable=broad-except
             errors.append(e)
     
     for e in errors: