Merge branch '512_bump_video_js'
[mediagoblin.git] / mediagoblin / tools / routing.py
CommitLineData
3d914332
E
1# GNU MediaGoblin -- federated, autonomous media hosting
2# Copyright (C) 2011, 2012 MediaGoblin contributors. See AUTHORS.
3#
4# This program is free software: you can redistribute it and/or modify
5# it under the terms of the GNU Affero General Public License as published by
6# the Free Software Foundation, either version 3 of the License, or
7# (at your option) any later version.
8#
9# This program is distributed in the hope that it will be useful,
10# but WITHOUT ANY WARRANTY; without even the implied warranty of
11# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12# GNU Affero General Public License for more details.
13#
14# You should have received a copy of the GNU Affero General Public License
15# along with this program. If not, see <http://www.gnu.org/licenses/>.
16
17import logging
18
19from werkzeug.routing import Map, Rule
20from mediagoblin.tools.common import import_component
21
22
23_log = logging.getLogger(__name__)
24
25url_map = Map()
26
3d914332 27
05501c57
E
28class MGRoute(Rule):
29 def __init__(self, endpoint, url, controller):
30 Rule.__init__(self, url, endpoint=endpoint)
31 self.gmg_controller = controller
3d914332 32
05501c57
E
33 def empty(self):
34 new_rule = Rule.empty(self)
35 new_rule.gmg_controller = self.gmg_controller
36 return new_rule
37
38
39def endpoint_to_controller(rule):
40 endpoint = rule.endpoint
41 view_func = rule.gmg_controller
3d914332
E
42
43 _log.debug('endpoint: {0} view_func: {1}'.format(endpoint, view_func))
44
45 # import the endpoint, or if it's already a callable, call that
46 if isinstance(view_func, basestring):
47 view_func = import_component(view_func)
05501c57 48 rule.gmg_controller = view_func
3d914332
E
49
50 return view_func
51
52
53def add_route(endpoint, url, controller):
54 """
55 Add a route to the url mapping
56 """
05501c57 57 url_map.add(MGRoute(endpoint, url, controller))
3d914332
E
58
59
60def mount(mountpoint, routes):
61 """
62 Mount a bunch of routes to this mountpoint
63 """
64 for endpoint, url, controller in routes:
65 url = "%s/%s" % (mountpoint.rstrip('/'), url.lstrip('/'))
66 add_route(endpoint, url, controller)