Limit   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 27
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 27
rs 10
wmc 4

3 Methods

Rating   Name   Duplication   Size   Complexity  
A scope() 0 5 2
A is_exempt() 0 4 1
A __init__() 0 11 1
1
from flask import request
2
from limits import parse_many
3
4
5
class Limit(object):
6
    """
7
    simple wrapper to encapsulate limits and their context
8
    """
9
10
    def __init__(
11
        self, limit, key_func, scope, per_method, methods, error_message,
12
        exempt_when
13
    ):
14
        self.limit = limit
15
        self.key_func = key_func
16
        self.__scope = scope
17
        self.per_method = per_method
18
        self.methods = methods
19
        self.error_message = error_message
20
        self.exempt_when = exempt_when
21
22
    @property
23
    def is_exempt(self):
24
        """Check if the limit is exempt."""
25
        return self.exempt_when and self.exempt_when()
26
27
    @property
28
    def scope(self):
29
        return self.__scope(request.endpoint) if callable(
30
            self.__scope
31
        ) else self.__scope
32
33
34
class LimitGroup(object):
35
    """
36
    represents a group of related limits either from a string or a callable that returns one
37
    """
38
39
    def __init__(
40
        self, limit_provider, key_function, scope, per_method, methods,
41
        error_message, exempt_when
42
    ):
43
        self.__limit_provider = limit_provider
44
        self.__scope = scope
45
        self.key_function = key_function
46
        self.per_method = per_method
47
        self.methods = methods and [m.lower() for m in methods] or methods
48
        self.error_message = error_message
49
        self.exempt_when = exempt_when
50
51
    def __iter__(self):
52
        limit_items = parse_many(
53
            self.__limit_provider()
54
            if callable(self.__limit_provider) else self.__limit_provider
55
        )
56
        for limit in limit_items:
57
            yield Limit(
58
                limit, self.key_function, self.__scope, self.per_method,
59
                self.methods, self.error_message, self.exempt_when
60
            )
61