From 9e5235962d8d7e67f41a1ef7b7d70b1cc7e55d0b Mon Sep 17 00:00:00 2001 From: Michele Simionato Date: Sun, 15 Apr 2018 14:35:10 +0200 Subject: Updated docs --- src/decorator.py | 2 +- src/tests/documentation.py | 36 +++++++++++++++++++----------------- 2 files changed, 20 insertions(+), 18 deletions(-) (limited to 'src') 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 -- cgit v1.2.1