1 # GNU MediaGoblin -- federated, autonomous media hosting
2 # Copyright (C) 2011, 2012 MediaGoblin contributors. See AUTHORS.
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.
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.
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/>.
20 from werkzeug
.routing
import Map
, Rule
21 from mediagoblin
.tools
.common
import import_component
24 _log
= logging
.getLogger(__name__
)
30 def __init__(self
, endpoint
, url
, controller
):
31 Rule
.__init
__(self
, url
, endpoint
=endpoint
)
32 self
.gmg_controller
= controller
35 new_rule
= Rule
.empty(self
)
36 new_rule
.gmg_controller
= self
.gmg_controller
40 def endpoint_to_controller(rule
):
41 endpoint
= rule
.endpoint
42 view_func
= rule
.gmg_controller
44 _log
.debug('endpoint: {0} view_func: {1}'.format(endpoint
, view_func
))
46 # import the endpoint, or if it's already a callable, call that
47 if isinstance(view_func
, six
.string_types
):
48 view_func
= import_component(view_func
)
49 rule
.gmg_controller
= view_func
54 def add_route(endpoint
, url
, controller
):
56 Add a route to the url mapping
58 url_map
.add(MGRoute(endpoint
, url
, controller
))
61 def mount(mountpoint
, routes
):
63 Mount a bunch of routes to this mountpoint
65 for endpoint
, url
, controller
in routes
:
66 url
= "%s/%s" % (mountpoint
.rstrip('/'), url
.lstrip('/'))
67 add_route(endpoint
, url
, controller
)