GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( 194382...d1a5fb )
by Lambda
01:38
created

PermissionHandlerRegistry.__init__()   A

Complexity

Conditions 1

Size

Total Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
c 1
b 0
f 0
dl 0
loc 2
rs 10
1
# coding=utf-8
2
"""
3
A utilities of permission handler
4
"""
5
import inspect
6
from django.core.exceptions import ImproperlyConfigured
7
from permission.conf import settings
8
from permission.compat import isstr
9
from permission.compat import import_string
10
11
12
class PermissionHandlerRegistry(object):
13
    """
14
    A registry class of permission handler
15
    """
16
    def __init__(self):
17
        self._registry = {}
18
19
    def register(self, model, handler=None):
20
        """
21
        Register a permission handler to the model
22
23
        Parameters
24
        ----------
25
        model : django model class
26
            A django model class
27
        handler : permission handler class, string, or None
28
            A permission handler class or a dotted path
29
30
        Raises
31
        ------
32
        ImproperlyConfigured
33
            Raise when the model is abstract model
34
        KeyError
35
            Raise when the model is already registered in registry
36
            The model cannot have more than one handler.
37
        """
38
        from permission.handlers import PermissionHandler
39
        if model._meta.abstract:
40
            raise ImproperlyConfigured(
41
                    'The model %s is abstract, so it cannot be registered '
42
                    'with permission.' % model)
43
        if model in self._registry:
44
            raise KeyError("A permission handler class is already "
45
                            "registered for '%s'" % model)
46
        if handler is None:
47
            handler = settings.PERMISSION_DEFAULT_PERMISSION_HANDLER
48
        if isstr(handler):
49
            handler = import_string(handler)
50
        if not inspect.isclass(handler):
51
            raise AttributeError(
52
                    "`handler` attribute must be a class. "
53
                    "An instance was specified.")
54
        if not issubclass(handler, PermissionHandler):
55
            raise AttributeError(
56
                    "`handler` attribute must be a subclass of "
57
                    "`permission.handlers.PermissionHandler`")
58
59
        # Instantiate the handler to save in the registry
60
        instance = handler(model)
61
        self._registry[model] = instance
62
63
    def unregister(self, model):
64
        """
65
        Unregister a permission handler from the model
66
67
        Parameters
68
        ----------
69
        model : django model class
70
            A django model class
71
72
        Raises
73
        ------
74
        KeyError
75
            Raise when the model have not registered in registry yet.
76
        """
77
        if model not in self._registry:
78
            raise KeyError("A permission handler class have not been "
79
                           "registered for '%s' yet" % model)
80
        # remove from registry
81
        del self._registry[model]
82
83
    def get_handlers(self):
84
        """
85
        Get registered handler instances
86
87
        Returns
88
        -------
89
        tuple
90
            permission handler tuple
91
        """
92
        return tuple(self._registry.values())
93
94
registry = PermissionHandlerRegistry()
95
"""Permission handler registry instance"""
96