websauna.utils.autoevent module

Automatically fire events on function enter and exit.

Use @event_source() to mark methods which fire events:

class MyClass:

    # Target function
    @event_source
    def my_func(self):
        print("In myfunc")

    def run(self):
        self.my_func()

Create a class which can bind handlers to be called before and after event_source methods:

class MyHandlers:

    # Aspect function
    @before(MyClass.my_func)
    def extra_logging(self):
        print("Entering my_func")

    # Aspect function
    @after(MyClass.my_func)
    def extra_logging(self):
        print("After my_func")


# Nothing happens yet
instance_a = MyClass()
instance_a.run()
# ... In myfunc

# Aspects run on instiated targetters only
my_handler = MyHandlers()

instance_b = MyClass()
bind_events(instance_b, my_handler)
instance_b.run()
# ...
# Entering myfunc
# In myfunc
# After myfunc

This implementation

  • It is not designed to be a general solution, but targets a very narrow use case only. But you are free to expand.
  • Works only on class methods.
  • Event sources are named. By default they get name from function name, but you can override this to avoid namespace conflict
  • Event handlers are not called if the subclass of MyClass overrides the parent method and doesn’t call super(). In this case you need to call fire_advisor_event() manually from the overriden method.
class websauna.utils.autoevent.AdvisorRole[source]

Bases: enum.Enum

Mark the role when storing references to advisor methods in our internal registry.

after = 2

This advisor is to be called after the function

before = 1

This advisor is to be called before the function

websauna.utils.autoevent.after(target_event_source)[source]

Call decorated function before target function.

Parameters:target_event_source (Callable) – Target method decorated with @event_source
websauna.utils.autoevent.before(target_event_source)[source]

Call decorated function before target function.

Parameters:target_event_source (Callable) – Target method decorated with @event_source
websauna.utils.autoevent.bind_events(source, target)[source]

Making the advisor functions of instance active.

To correctly handle application flow, before and after advices are not bind their target functions until the instance of a target class is contructed. This avoids import time side effects that advices would be always unconditionally bound.

Parameters:
  • source (object) – Target instance providing @event_source methods
  • target (object) – Provider of advisor methods
websauna.utils.autoevent.event_source(method, name=None)[source]

A decorator which makes the function act as a source of before and after call events.

You can later subscribe to these event with before() and :py:func`after` decorators.

Parameters:method (Callable) – Target class method
Param:Name of for the join point. If not given use the function name.
websauna.utils.autoevent.fire_advisor_event(source, event_source_name, role, *args, **kwargs)[source]

Call bound advisors for a join point.