b3j0f.aop package

Module contents

Aspect Oriented Programming Library for Python.

Provides tools to (un)weave and get advices, and check joinpoint status.

b3j0f.aop.weave(target, advices, pointcut=None, ctx=None, depth=1, public=False, pointcut_application=None, ttl=None)[source]

Weave advices on target with input pointcut.

Parameters:
  • target (callable) – target from where checking pointcut and weaving advices.
  • advices – advices to weave on target.
  • ctx – target ctx (class or instance).
  • pointcut – condition for weaving advices on joinpointe. The condition depends on its type.
  • depth (int) – class weaving depthing.
  • public (bool) – (default True) weave only on public members.
  • pointcut_application (routine) – routine which applies a pointcut when required. _Joinpoint().apply_pointcut by default. Such routine has to take in parameters a routine called target and its related function called function. Its result is the interception function.
  • ttl (float) – time to leave for weaved advices.
Returns:

the intercepted functions created from input target or a tuple with intercepted functions and ttl timer.

Return type:

list

Raises:

AdviceError if pointcut is not None, not callable neither a str.

b3j0f.aop.unweave(target, advices=None, pointcut=None, ctx=None, depth=1, public=False)[source]

Unweave advices on target with input pointcut.

Parameters:
  • target (callable) – target from where checking pointcut and weaving advices.
  • pointcut – condition for weaving advices on joinpointe. The condition depends on its type.
  • ctx – target ctx (class or instance).
  • depth (int) – class weaving depthing.
  • public (bool) – (default True) weave only on public members
Returns:

the intercepted functions created from input target.

b3j0f.aop.get_advices(target, ctx=None, local=False)[source]

Get element advices.

Parameters:
  • target – target from where get advices.
  • ctx – ctx from where get target.
  • local (bool) – If ctx is not None or target is a method, if True (False by default) get only target advices without resolving super target advices in a super ctx.
Returns:

list of advices.

Return type:

list

b3j0f.aop.weave_on(advices, pointcut=None, ctx=None, depth=1, ttl=None)[source]

Decorator for weaving advices on a callable target.

Parameters:
  • pointcut – condition for weaving advices on joinpointe. The condition depends on its type.
  • ctx – target ctx (instance or class).
  • depth (int) – class weaving depthing
  • public (bool) – (default True) weave only on public members
class b3j0f.aop.Joinpoint(target=None, args=None, kwargs=None, advices=None, ctx=None, exec_ctx=None)[source]

Bases: object

Manage joinpoint execution with Advices.

Advices are callable objects which take in parameter a Joinpoint.

Joinpoint provides to advices:
  • the joinpoint,
  • joinpoint call arguments as args and kwargs property,
  • a shared context during interception such as a dictionary.
ARGS = 'args'
CTX = 'ctx'
EXEC_CTX = 'exec_ctx'
KWARGS = 'kwargs'
TARGET = 'target'
apply_pointcut(target, function=None, ctx=None)[source]

Apply pointcut on input target and returns final interception.

The poincut respects all meta properties such as: - function signature, - module path, - file path, - __dict__ reference.

args
ctx
exec_ctx
get_advices(target)[source]

Get target advices.

Parameters:target – target from where getting advices.
kwargs
proceed()[source]

Proceed this Joinpoint in calling all advices with this joinpoint as the only one parameter, and call at the end the target.

set_target(target, ctx=None)[source]

Set target.

Parameters:
  • target – new target to use.
  • ctx (target) – target ctx if target is an class/instance attribute.
start(target=None, args=None, kwargs=None, advices=None, exec_ctx=None, ctx=None)[source]

Start to proceed this Joinpoint in initializing target, its arguments and advices. Call self.proceed at the end.

Parameters:
  • target (callable) – new target to use in proceeding. self.target by default.
  • args (tuple) – new target args to use in proceeding. self.args by default.
  • kwargs (dict) – new target kwargs to use in proceeding. self.kwargs by default.
  • advices (list) – advices to use in proceeding. self advices by default.
  • exec_ctx (dict) – execution context.
  • target_ctx – target ctx to use in proceeding.
Returns:

self.proceed()

target
exception b3j0f.aop.JoinpointError[source]

Bases: exceptions.Exception

Handle Joinpoint errors