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
|
|
|
class Context: |
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.description = description |
61
|
|
|
self.plugin = plugin |
62
|
|
|
self.app = app |
63
|
|
|
|
64
|
|
|
self.blueprint = Blueprint(name, __name__, |
65
|
|
|
url_prefix=url_prefix, |
66
|
|
|
subdomain=None, |
67
|
|
|
template_folder=template_folder, |
68
|
|
|
static_folder=static_folder, |
69
|
|
|
static_url_path="/static/" + name) |
70
|
|
|
self.app.web.flask.register_blueprint(self.blueprint) |
71
|
|
|
|