1 | import os |
||
2 | import logging |
||
3 | |||
4 | from groundwork.util import gw_get |
||
5 | |||
6 | |||
7 | class RouteManagerPlugin: |
||
8 | def __init__(self, plugin): |
||
9 | self.plugin = plugin |
||
10 | self.log = plugin.log |
||
11 | self.app = plugin.app |
||
12 | |||
13 | def register(self, url, methods, endpoint, context=None, name=None, description=None): |
||
14 | return self.app.web.routes.register(url, methods, endpoint, self.plugin, context, name, description) |
||
15 | |||
16 | |||
17 | class RouteManagerApplication: |
||
18 | def __init__(self, app): |
||
19 | self._routes = {} |
||
20 | self.app = app |
||
21 | self.log = logging.getLogger(__name__) |
||
22 | self.blueprints = {} |
||
23 | |||
24 | def register(self, url, methods, endpoint, plugin, context=None, name=None, description=None, ): |
||
25 | |||
26 | if context is None and self.app.web.contexts.default_context is None: |
||
27 | self.log.warning("Context not given and no default context is available. Basic context will be created") |
||
28 | basic_context = self.app.web.contexts.register("basic", |
||
29 | os.path.join(self.app.path, "template"), |
||
30 | os.path.join(self.app.path, "static"), |
||
31 | "/", |
||
32 | "basic context, which was created automatically due the " |
||
33 | "miss of an available context during first route " |
||
34 | "registration.", |
||
35 | plugin) |
||
36 | context = basic_context.name |
||
37 | context_obj = basic_context |
||
38 | |||
39 | if context is None: |
||
40 | context_obj = self.app.web.contexts.default_context |
||
41 | else: |
||
42 | context_obj = self.app.web.contexts.get(context) |
||
43 | |||
44 | if name not in self._routes.keys(): |
||
45 | self._routes[name] = Route(url, methods, endpoint, context_obj, name, description, plugin, self.app) |
||
46 | |||
47 | def get(self, name=None, plugin=None): |
||
48 | return gw_get(self._routes, name, plugin) |
||
49 | |||
50 | |||
51 | View Code Duplication | class Route: |
|
0 ignored issues
–
show
Duplication
introduced
by
Loading history...
|
|||
52 | """ |
||
53 | """ |
||
54 | |||
55 | def __init__(self, url, methods, endpoint, context, name, description, plugin, app): |
||
56 | self.url = url |
||
57 | self.methods = methods |
||
58 | self.endpoint = endpoint |
||
59 | self.context = context |
||
60 | self.name = name |
||
61 | self.description = description |
||
62 | self.plugin = plugin |
||
63 | self.app = app |
||
64 | self.log = logging.getLogger(__name__) |
||
65 | |||
66 | blueprint = self.context.blueprint |
||
67 | blueprint.add_url_rule(url, methods=methods, endpoint=endpoint.__name__, view_func=endpoint) |
||
68 | # We have to (re-)register our blueprint to activate the route |
||
69 | self.app.web.flask.register_blueprint(blueprint) |
||
70 | |||
71 | self.log.info("Route registered: %s for context %s (%s)" % (self.url, self.context.name, |
||
72 | self.context.url_prefix)) |
||
73 |