|
@@ -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
|
|
|
+
|