Completed
Push — master ( aa9a3d...5e72be )
by Daniel
01:05
created

ContextManagerApplication   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 17
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 17
rs 10
wmc 7

3 Methods

Rating   Name   Duplication   Size   Complexity  
A get() 0 2 1
B register() 0 8 5
A __init__() 0 4 1
1
from groundwork.util import gw_get
2
3
4
class ContextManagerPlugin:
5
6
    def __init__(self, plugin):
7
        self.plugin = plugin
8
        self.log = plugin.log
9
        self.app = plugin.app
10
11
    def register(self, name, template_folder, static_folder, url_prefix, description):
12
        return self.app.web.contexts.register(name, template_folder, static_folder,
13
                                              url_prefix, description, self.plugin)
14
15
16
class ContextManagerApplication:
17
    def __init__(self, app):
18
        self._contexts = {}
19
        self.app = app
20
        self.default_context = None
21
22
    def register(self, name, template_folder, static_folder, url_prefix, description, plugin):
23
        if name not in self._contexts.keys():
24
            self._contexts[name] = Context(name, template_folder, static_folder, url_prefix, description, plugin)
25
            if name == self.app.config.get("DEFAULT_CONTEXT", None) or self.default_context is None:
26
                self.default_context = self._contexts[name]
27
28
        for name, provider in self.app.web.providers.get().items():
29
            provider.register_context()
30
31
    def get(self, name=None, plugin=None):
32
        return gw_get(self._contexts, name, plugin)
33
34
35
class Context:
36
    """
37
    Contexts are used to collect common objects in a single place and make it easy for new web routes to reuse
38
    these objects.
39
40
    Common objects are:
41
42
     * A template folder
43
     * A static folder
44
     * A url_prefix
45
     * A static folder web route, which gets automatically calculated based on given context name
46
47
    They are similar to flask blueprint concept.
48
    """
49
    def __init__(self, name, template_folder, static_folder, url_prefix, description, plugin):
50
        self.name = name
51
        self.template_folder = template_folder
52
        self.static_folder = static_folder
53
        self.url_prefix = url_prefix
54
        self.description = description
55
        self.plugin = plugin
56