Rewrite routing using new MGRoute class
authorElrond <elrond+mediagoblin.org@samba-tng.org>
Sat, 15 Dec 2012 23:45:57 +0000 (00:45 +0100)
committerElrond <elrond+mediagoblin.org@samba-tng.org>
Sun, 23 Dec 2012 11:26:34 +0000 (12:26 +0100)
MGRoute subclasses Rule():
Rule doesn't have a way to tag extra data, like the
controller function, we need. So MGRoute has a new
attribute .gmg_controller, which holds this.

Rewrite everything to use this new Rule variant and drop
all the other stuff that mapped endpoints to controller
functions, mostly.

mediagoblin/app.py
mediagoblin/tools/routing.py

index ff64f65c9e3c967aa5b320d6e65c707f6d3a8034..936a354f13acf77c4a4cd25c939ddc40209baee9 100644 (file)
@@ -184,7 +184,7 @@ class MediaGoblinApp(object):
         mg_request.setup_user_in_request(request)
 
         try:
-            endpoint, url_values = map_adapter.match()
+            found_rule, url_values = map_adapter.match(return_rule=True)
             request.matchdict = url_values
         except HTTPException as exc:
             # Stop and render exception
@@ -192,7 +192,7 @@ class MediaGoblinApp(object):
                 request, exc,
                 exc.get_description(environ))(environ, start_response)
 
-        controller = endpoint_to_controller(endpoint)
+        controller = endpoint_to_controller(found_rule)
 
         # pass the request through our meddleware classes
         try:
index 6c5acbec40a168b6046557bf6b3c0f717117e18d..791cd1e6bc18f0fdfbb08cd5eedb7e8e74fbfa84 100644 (file)
@@ -24,18 +24,28 @@ _log = logging.getLogger(__name__)
 
 url_map = Map()
 
-view_functions = {}
 
+class MGRoute(Rule):
+    def __init__(self, endpoint, url, controller):
+        Rule.__init__(self, url, endpoint=endpoint)
+        self.gmg_controller = controller
 
-def endpoint_to_controller(endpoint):
-    view_func = view_functions[endpoint]
+    def empty(self):
+        new_rule = Rule.empty(self)
+        new_rule.gmg_controller = self.gmg_controller
+        return new_rule
+
+
+def endpoint_to_controller(rule):
+    endpoint = rule.endpoint
+    view_func = rule.gmg_controller
 
     _log.debug('endpoint: {0} view_func: {1}'.format(endpoint, view_func))
 
     # import the endpoint, or if it's already a callable, call that
     if isinstance(view_func, basestring):
         view_func = import_component(view_func)
-        view_functions[endpoint] = view_func
+        rule.gmg_controller = view_func
 
     return view_func
 
@@ -44,14 +54,7 @@ def add_route(endpoint, url, controller):
     """
     Add a route to the url mapping
     """
-    # XXX: We cannot use this, since running tests means that the plugin
-    # routes will be populated over and over over the same session.
-    #
-    # assert endpoint not in view_functions.keys(), 'Trying to overwrite a rule'
-
-    view_functions.update({endpoint: controller})
-
-    url_map.add(Rule(url, endpoint=endpoint))
+    url_map.add(MGRoute(endpoint, url, controller))
 
 
 def mount(mountpoint, routes):