diff options
| author | Michele Simionato <michele.simionato@gmail.com> | 2018-04-15 14:35:10 +0200 |
|---|---|---|
| committer | Michele Simionato <michele.simionato@gmail.com> | 2018-04-15 14:35:10 +0200 |
| commit | 9e5235962d8d7e67f41a1ef7b7d70b1cc7e55d0b (patch) | |
| tree | 2207a5150cdd7d4b8c5d4a832a238cfa91be39cf /src | |
| parent | c7aa9cb24de431c73ddb5a5fa024a0e68c0eb3c9 (diff) | |
| download | python-decorator-git-9e5235962d8d7e67f41a1ef7b7d70b1cc7e55d0b.tar.gz | |
Updated docs
Diffstat (limited to 'src')
| -rw-r--r-- | src/decorator.py | 2 | ||||
| -rw-r--r-- | src/tests/documentation.py | 36 |
2 files changed, 20 insertions, 18 deletions
diff --git a/src/decorator.py b/src/decorator.py index b31356a..44303ee 100644 --- a/src/decorator.py +++ b/src/decorator.py @@ -40,7 +40,7 @@ import operator import itertools import collections -__version__ = '4.2.1' +__version__ = '4.3.0' if sys.version >= '3': from inspect import getfullargspec diff --git a/src/tests/documentation.py b/src/tests/documentation.py index 2588768..0d8ebf5 100644 --- a/src/tests/documentation.py +++ b/src/tests/documentation.py @@ -395,7 +395,7 @@ https://www.python.org/dev/peps/pep-0557/) I finally gave up. The example below will show how it works in practice. -``blocking`` +Decorator factories ------------------------------------------- Sometimes one has to deal with blocking resources, such as ``stdin``. @@ -432,6 +432,12 @@ available. For instance: >>> print(read_data()) some data +Decorator factories are most useful to framework builders. Here is an example +that gives an idea of how you could manage permissions in a Web framework: + +$$restricted +$$Action + ``decorator(cls)`` -------------------------------------------- @@ -1495,35 +1501,31 @@ class Admin(PowerUser): "Will be able to delete pages too" -def get_userclass(): - return User - - class PermissionError(Exception): - pass + """ + >>> a = Action() + >>> a.user = User() + >>> a.view() # ok + >>> a.insert() # doctest: +IGNORE_EXCEPTION_DETAIL + Traceback (most recent call last): + ... + PermissionError: User does not have the permission to run insert! + """ @decorator def restricted(func, user_class=User, *args, **kw): "Restrict access to a given class of users" - userclass = get_userclass() - if issubclass(userclass, user_class): + self = args[0] + if isinstance(self.user, user_class): return func(*args, **kw) else: raise PermissionError( '%s does not have the permission to run %s!' - % (userclass.__name__, func.__name__)) + % (self.user, func.__name__)) class Action(object): - """ - >>> a = Action() - >>> a.view() # ok - >>> a.insert() # doctest: +IGNORE_EXCEPTION_DETAIL - Traceback (most recent call last): - ... - PermissionError: User does not have the permission to run insert! - """ @restricted(User) def view(self): pass |
