1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
|
import logging
import cherrypy
from ..exceptions import ScopeNotValid
from ..security import Scope
from ._base_controller import BaseController
from ._helpers import generate_controller_routes
logger = logging.getLogger(__name__)
class Router(object):
def __init__(self, path, base_url=None, security_scope=None, secure=True):
if security_scope and not Scope.valid_scope(security_scope):
raise ScopeNotValid(security_scope)
self.path = path
self.base_url = base_url
self.security_scope = security_scope
self.secure = secure
if self.path and self.path[0] != "/":
self.path = "/" + self.path
if self.base_url is None:
self.base_url = ""
elif self.base_url == "/":
self.base_url = ""
if self.base_url == "" and self.path == "":
self.base_url = "/"
def __call__(self, cls):
cls._routed = True
cls._cp_path_ = "{}{}".format(self.base_url, self.path)
cls._security_scope = self.security_scope
config = {
'tools.dashboard_exception_handler.on': True,
'tools.authenticate.on': self.secure,
}
if not hasattr(cls, '_cp_config'):
cls._cp_config = {}
cls._cp_config.update(config)
return cls
@classmethod
def generate_routes(cls, url_prefix):
controllers = BaseController.load_controllers()
logger.debug("controllers=%r", controllers)
mapper = cherrypy.dispatch.RoutesDispatcher()
parent_urls = set()
endpoint_list = []
for ctrl in controllers:
inst = ctrl()
for endpoint in ctrl.endpoints():
endpoint.inst = inst
endpoint_list.append(endpoint)
endpoint_list = sorted(endpoint_list, key=lambda e: e.url)
for endpoint in endpoint_list:
parent_urls.add(generate_controller_routes(endpoint, mapper,
"{}".format(url_prefix)))
logger.debug("list of parent paths: %s", parent_urls)
return mapper, parent_urls
|