ContextManagerPlugin   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 9
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 9
rs 10
wmc 2

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __init__() 0 4 1
A register() 0 3 1
1
import logging
2
from flask import Blueprint
3
4
from groundwork.util import gw_get
5
6
7
class ContextManagerPlugin:
8
    def __init__(self, plugin):
9
        self.plugin = plugin
10
        self.log = plugin.log
11
        self.app = plugin.app
12
13
    def register(self, name, template_folder, static_folder, url_prefix, description):
14
        return self.app.web.contexts.register(name, template_folder, static_folder,
15
                                              url_prefix, description, self.plugin)
16
17
18
class ContextManagerApplication:
19
    def __init__(self, app):
20
        self._contexts = {}
21
        self.app = app
22
        self.log = logging.getLogger(__name__)
23
        self.default_context = None
24
25
    def register(self, name, template_folder, static_folder, url_prefix, description, plugin):
26
        if name not in self._contexts.keys():
27
            self._contexts[name] = Context(name, template_folder, static_folder, url_prefix, description, plugin,
28
                                           self.app)
29
            if name == self.app.config.get("DEFAULT_CONTEXT", None) or self.default_context is None:
30
                self.default_context = self._contexts[name]
31
        else:
32
            self.log.warning("Context %s already registered by %s" % (name, self._contexts[name].plugin.name))
33
34
        return self._contexts[name]
35
36
    def get(self, name=None, plugin=None):
37
        return gw_get(self._contexts, name, plugin)
38
39
40 View Code Duplication
class Context:
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
41
    """
42
    Contexts are used to collect common objects in a single place and make it easy for new web routes to reuse
43
    these objects.
44
45
    Common objects are:
46
47
     * A template folder
48
     * A static folder
49
     * A url_prefix
50
     * A static folder web route, which gets automatically calculated based on given context name
51
52
    They are similar to flask blueprint concept.
53
    """
54
55
    def __init__(self, name, template_folder, static_folder, url_prefix, description, plugin, app):
56
        self.name = name
57
        self.template_folder = template_folder
58
        self.static_folder = static_folder
59
        self.url_prefix = url_prefix
60
        self.static_url_path = "/static"
61
        self.description = description
62
        self.plugin = plugin
63
        self.app = app
64
        self.log = logging.getLogger(__name__)
65
66
        self.blueprint = Blueprint(name, __name__,
67
                                   url_prefix=url_prefix,
68
                                   subdomain=None,
69
                                   template_folder=template_folder,
70
                                   static_folder=static_folder,
71
                                   static_url_path=self.static_url_path)
72
        self.app.web.flask.register_blueprint(self.blueprint)
73
74
        self.log.debug("Context registered: %s (%s) for plugin %s" % (self.name, self.url_prefix, self.plugin.name))
75