From 2f8854627811d71210df2945fd19f9d381e874ff Mon Sep 17 00:00:00 2001 From: Andres Riancho Date: Wed, 28 Jan 2015 15:45:25 -0300 Subject: [PATCH] * Fixing the whole thing to work with python 2.7 * Fixed syntax errors due to the usage of unicode * Fixed tests * Fixed documentation --- README.md | 155 +++++++++----------- aop/aspectbase.py | 1 + aop/aspecttype.py | 1 + setup.py | 1 + tests/{aspectbase.py => test_aspectbase.py} | 0 tests/{aspecttype.py => test_aspecttype.py} | 8 +- 6 files changed, 77 insertions(+), 89 deletions(-) rename tests/{aspectbase.py => test_aspectbase.py} (100%) rename tests/{aspecttype.py => test_aspecttype.py} (82%) diff --git a/README.md b/README.md index 2d557cb..a3e4125 100644 --- a/README.md +++ b/README.md @@ -1,101 +1,84 @@ -Python AOP Framework -==================== +# Python AOP Framework Python AOP Framework is a minimal Framework to use Aspect-Oriented Programming on your Python applications -Examples --------- +## Examples -### Definition of Aspects ### +### Definition of Aspects - >>> import logging - >>> from aop import Aspect - >>> - >>> class InvocationLoggerAspect(Aspect): - ... def __init__(self, *args, **kwargs): - ... super().__init__(*args, **kwargs) - ... self.logger = logging.getLogger('python-aop') - ... self.logger.setLevel(logging.DEBUG) - ... - ... fh = logging.FileHandler('auth.log') - ... fh.setLevel(logging.DEBUG) - ... - ... self.logger.addHandler(fh) - ... - ... def before(self, *args, **kwargs): - ... self.logger.info( - ... 'Invocation: name=%s, args=%s, kwargs=%s' % ( - ... self.function, - ... args, - ... kwargs - ... ) - ... ) - ... - >>> +```python +import logging +from aop import Aspect + +class InvocationLoggerAspect(Aspect): + def __init__(self, *args, **kwargs): + super(InvocationLoggerAspect, self).__init__(*args, **kwargs) + self.logger = logging.getLogger('python-aop') + self.logger.setLevel(logging.DEBUG) + + fh = logging.FileHandler('auth.log') + fh.setLevel(logging.DEBUG) + + self.logger.addHandler(fh) + + def before(self, *args, **kwargs): + self.logger.info( + 'Invocation: name=%s, args=%s, kwargs=%s' % ( + self.function, + args, + kwargs + ) + ) +``` - -### Use of Aspect over functions ### +### Use of Aspect over functions If we want to log each invocation attempt we can apply the aspect directly to a function - >>> def auth(user, password): - ... if user == 'aop' and password == '#$%3&23%#$(%': - ... return True - ... return False - ... - >>> - >>> auth = InvocationLoggerAspect(auth) - >>> - >>> auth('aop', '#$%3&23%#$(%') - True - >>> auth('aop', '') - False - >>> +```python +>>> def auth(user, password): +... if user == 'aop' and password == '#$%3&23%#$(%': +... return True +... return False +... +>>> +>>> auth = InvocationLoggerAspect(auth) +>>> +>>> auth('aop', '#$%3&23%#$(%') +True +>>> auth('aop', '') +False +>>> +``` -The content of **auth.log**: +The content of `auth.log`: - Invocation: name=, args=('aop', '#$%3&23%#$(%'), kwargs={} - Invocation: name=, args=('aop', ''), kwargs={} +```text +Invocation: name=, args=('aop', '#$%3&23%#$(%'), kwargs={} +Invocation: name=, args=('aop', ''), kwargs={} +``` +### Use of AspectType -### Use of AspectType ### +```python +>>> from aop import Aspect, AspectType +>>> class AuthenticationBackend(object): +... __metaclass__ = AspectType +... +... def auth(self, user, password): +... if user == 'aop' and password == '#$%3&23%#$(%': +... return True +... return False +... +>>> AuthenticationBackend.pointcut('auth', InvocationLoggerAspect) +>>> +>>> backend = AuthenticationBackend() +>>> backend.auth('aop', '#$%3&23%#$(%') +True +>>> backend.auth('aop', '') +False +>>> +``` - >>> import logging - >>> from aop import Aspect, AspectType - >>> - >>> class InvocationLoggerAspect(Aspect): - ... def __init__(self, *args, **kwargs): - ... super().__init__(*args, **kwargs) - ... self.logger = logging.getLogger('python-aop') - ... self.logger.setLevel(logging.DEBUG) - ... - ... fh = logging.FileHandler('auth.log') - ... fh.setLevel(logging.DEBUG) - ... - ... self.logger.addHandler(fh) - ... - ... def before(self, *args, **kwargs): - ... self.logger.info( - ... 'Invocation: name=%s, args=%s, kwargs=%s' % ( - ... self.function, - ... args, - ... kwargs - ... ) - ... ) - ... - >>> - >>> class AuthenticationBackend(metaclass=AspectType): - ... def auth(self, user, password): - ... if user == 'aop' and password == '#$%3&23%#$(%': - ... return True - ... return False - ... - >>> AuthenticationBackend.pointcut('auth', InvocationLoggerAspect) - >>> - >>> backend = AuthenticationBackend() - >>> backend.auth('aop', '#$%3&23%#$(%') - True - >>> backend.auth('aop', '') - False - >>> +The same `auth.log` is generated. diff --git a/aop/aspectbase.py b/aop/aspectbase.py index 5268cd9..69f1ef8 100644 --- a/aop/aspectbase.py +++ b/aop/aspectbase.py @@ -1,3 +1,4 @@ +# -*- coding: utf-8 -*- """ python-aop is part of LemonFramework. diff --git a/aop/aspecttype.py b/aop/aspecttype.py index a9250f8..e118dd3 100644 --- a/aop/aspecttype.py +++ b/aop/aspecttype.py @@ -1,3 +1,4 @@ +# -*- coding: utf-8 -*- """ python-aop is part of LemonFramework. diff --git a/setup.py b/setup.py index e3f3db7..5f22b0a 100644 --- a/setup.py +++ b/setup.py @@ -1,4 +1,5 @@ #!/usr/bin/env python +# -*- coding: utf-8 -*- """ python-aop is part of LemonFramework. diff --git a/tests/aspectbase.py b/tests/test_aspectbase.py similarity index 100% rename from tests/aspectbase.py rename to tests/test_aspectbase.py diff --git a/tests/aspecttype.py b/tests/test_aspecttype.py similarity index 82% rename from tests/aspecttype.py rename to tests/test_aspecttype.py index 9ed5f20..e343e51 100644 --- a/tests/aspecttype.py +++ b/tests/test_aspecttype.py @@ -4,13 +4,15 @@ class TestAspectType(unittest.TestCase): def test_pointcut_attr(self): - class TestClass(metaclass=AspectType): - pass + class TestClass(object): + __metaclass__ = AspectType self.assertTrue(hasattr(TestClass, 'pointcut')) def test_pointcut(self): - class TestClass(metaclass=AspectType): + class TestClass(object): + __metaclass__ = AspectType + def method(self): return True