shiroyuki / Imagination

Java Beans-inspired Dependency Injection Framework for Python
https://imagination.readthedocs.io/en/latest/
4 stars 2 forks source link

Implement the decorators to set up interceptable methods and define the interceptor without the configuration. #14

Open shiroyuki opened 11 years ago

shiroyuki commented 11 years ago

Suppose we have two methods where one is interceptable and the another is an interceptor.

from imagination.action import EventType
from imagination.interception import InterceptableMethod, CallableInterceptor

@InterceptableMethod('interceptable_name_a')
def method_a(x):
    print(x)

@CallableInterceptor(
    EventType.PRE_CONDITION,
    'interceptable_name_a',
    options=[CallableInterceptor.RAISE_UNDEFINED_TARGET_EXCEPTION]
)
def method_b(x):
    print(
        'It is greater than 10.'\
            if x > 10
            else 'It is less than 10.'
    )

If method_a is executed, method_b should be executed before method_a while using the same parameters.

It also should work with any registered imagination.entity.Entity where the name of the interceptable point is the composition of the entity ID and the name of the method in the following pattern

entity_id.method_name

By default, CallableInterceptor will do nothing if the reference point of interception doesn't exist.