Commits

Andriy Kornatskyy  committed 8edaf6a

Moved bootstrap_default middleware factory to bootstrap module; imported middleware factories in package.

  • Participants
  • Parent commits ec34833

Comments (0)

Files changed (4)

File src/wheezy/web/middleware/__init__.py

 """ ``middleware`` package.
 """
 
-from wheezy.core.collections import defaultdict
-from wheezy.core.i18n import TranslationsManager
-from wheezy.http.config import bootstrap_http_defaults
-from wheezy.routing import PathRouter
-from wheezy.security.crypto import Ticket
-from wheezy.web.middleware.errors import HTTPErrorMiddleware
-from wheezy.web.middleware.routing import PathRoutingMiddleware
-from wheezy.web.templates import MakoTemplate
-
-
-def bootstrap_defaults(url_mapping=None):
-    """ Defaults bootstrap.
-    """
-    def load(options):
-        bootstrap_http_defaults(options)
-        if 'path_router' not in options:
-            options['path_router'] = path_router = PathRouter()
-        else:
-            path_router = options['path_router']
-        if 'path_for' not in options:
-            options['path_for'] = path_router.path_for
-        if url_mapping:
-            path_router.add_routes(url_mapping)
-        if 'render_template' not in options:
-            options['render_template'] = MakoTemplate()
-        if 'translations_manager' not in options:
-            options['translations_manager'] = TranslationsManager()
-        if 'ticket' not in options:
-            options['ticket'] = Ticket()
-
-        options.setdefault('ENCODING', 'UTF-8')
-        options.setdefault('CONTENT_TYPE', 'text/html; charset=' +
-                options['ENCODING'])
-        options.setdefault('AUTH_COOKIE', '_a')
-        options.setdefault('AUTH_COOKIE_DOMAIN', None)
-        options.setdefault('AUTH_COOKIE_PATH', '')
-        options.setdefault('AUTH_COOKIE_SECURE', False)
-
-        options.setdefault('XSRF_NAME', '_x')
-        options.setdefault('RESUBMISSION_NAME', '_c')
-        return None
-    return load
-
-
-def http_error_middleware_factory(options):
-    """ HTTP error middleware factory.
-    """
-    path_for = options['path_for']
-    try:
-        error_mapping = options['http_errors']
-        assert isinstance(error_mapping, defaultdict)
-        assert path_for(error_mapping.default_factory())
-        for route_name in error_mapping.values():
-            assert path_for(route_name) is not None
-    except KeyError:
-        error_mapping = defaultdict(str)
-    return HTTPErrorMiddleware(
-            error_mapping=error_mapping)
-
-
-def path_routing_middleware_factory(options):
-    """ PathRouting middleware factory.
-    """
-    path_router = options['path_router']
-    return PathRoutingMiddleware(
-            path_router=path_router)
+from wheezy.web.middleware.bootstrap import bootstrap_defaults
+from wheezy.web.middleware.errors import http_error_middleware_factory
+from wheezy.web.middleware.routing import path_routing_middleware_factory

File src/wheezy/web/middleware/bootstrap.py

+
+""" ``bootstrap`` module.
+"""
+from wheezy.http import bootstrap_http_defaults
+
+
+def bootstrap_defaults(url_mapping=None):
+    """ Defaults bootstrap.
+    """
+    def load(options):
+        bootstrap_http_defaults(options)
+        if 'path_router' not in options:
+            from wheezy.routing import PathRouter
+            options['path_router'] = path_router = PathRouter()
+        else:
+            path_router = options['path_router']
+        if 'path_for' not in options:
+            options['path_for'] = path_router.path_for
+        if url_mapping:
+            path_router.add_routes(url_mapping)
+        if 'render_template' not in options:
+            from wheezy.web.templates import MakoTemplate
+            options['render_template'] = MakoTemplate()
+        if 'translations_manager' not in options:
+            from wheezy.core.i18n import TranslationsManager
+            options['translations_manager'] = TranslationsManager()
+        if 'ticket' not in options:
+            from wheezy.security.crypto import Ticket
+            options['ticket'] = Ticket()
+
+        options.setdefault('ENCODING', 'UTF-8')
+        options.setdefault('CONTENT_TYPE', 'text/html; charset=' +
+                options['ENCODING'])
+        options.setdefault('AUTH_COOKIE', '_a')
+        options.setdefault('AUTH_COOKIE_DOMAIN', None)
+        options.setdefault('AUTH_COOKIE_PATH', '')
+        options.setdefault('AUTH_COOKIE_SECURE', False)
+
+        options.setdefault('XSRF_NAME', '_x')
+        options.setdefault('RESUBMISSION_NAME', '_c')
+        return None
+    return load

File src/wheezy/web/middleware/errors.py

 
 from traceback import print_exc
 
+from wheezy.core.collections import defaultdict
 from wheezy.http.response import internal_error
 from wheezy.http.response import not_found
 from wheezy.web.handlers.base import RedirectRouteHandler
             if error_route_name != route_name:
                 response = RedirectRouteHandler(request, error_route_name)
         return response
+
+
+def http_error_middleware_factory(options):
+    """ HTTP error middleware factory.
+    """
+    path_for = options['path_for']
+    if 'http_errors' in options:
+        error_mapping = options['http_errors']
+        assert isinstance(error_mapping, defaultdict)
+        assert path_for(error_mapping.default_factory())
+        for route_name in error_mapping.values():
+            assert path_for(route_name) is not None
+    else:
+        error_mapping = defaultdict(str)
+    return HTTPErrorMiddleware(
+            error_mapping=error_mapping)

File src/wheezy/web/middleware/routing.py

                 return following(request)
             return None
         return handler(request)
+
+
+def path_routing_middleware_factory(options):
+    """ PathRouting middleware factory.
+    """
+    path_router = options['path_router']
+    return PathRoutingMiddleware(
+            path_router=path_router)