home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2010 November / maximum-cd-2010-11.iso / DiscContents / calibre-0.7.13.msi / file_2389 (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2010-08-06  |  3.9 KB  |  121 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. import re
  5. import logging
  6. from webob import Request
  7. from routes.base import request_config
  8. from routes.util import URLGenerator, url_for
  9. log = logging.getLogger('routes.middleware')
  10.  
  11. class RoutesMiddleware(object):
  12.     
  13.     def __init__(self, wsgi_app, mapper, use_method_override = True, path_info = True, singleton = True):
  14.         self.app = wsgi_app
  15.         self.mapper = mapper
  16.         self.singleton = singleton
  17.         self.use_method_override = use_method_override
  18.         self.path_info = path_info
  19.         log_debug = self.log_debug = logging.DEBUG >= log.getEffectiveLevel()
  20.         if self.log_debug:
  21.             log.debug('Initialized with method overriding = %s, and path info altering = %s', use_method_override, path_info)
  22.         
  23.  
  24.     
  25.     def __call__(self, environ, start_response):
  26.         old_method = None
  27.         if self.use_method_override:
  28.             req = None
  29.             
  30.             try:
  31.                 qs = environ['QUERY_STRING']
  32.             except KeyError:
  33.                 qs = ''
  34.  
  35.             if '_method' in qs:
  36.                 req = Request(environ)
  37.                 req.errors = 'ignore'
  38.                 if '_method' in req.GET:
  39.                     old_method = environ['REQUEST_METHOD']
  40.                     environ['REQUEST_METHOD'] = req.GET['_method'].upper()
  41.                     if self.log_debug:
  42.                         log.debug('_method found in QUERY_STRING, altering request method to %s', environ['REQUEST_METHOD'])
  43.                     
  44.                 
  45.             elif environ['REQUEST_METHOD'] == 'POST' and is_form_post(environ):
  46.                 if req is None:
  47.                     req = Request(environ)
  48.                     req.errors = 'ignore'
  49.                 
  50.                 if '_method' in req.POST:
  51.                     old_method = environ['REQUEST_METHOD']
  52.                     environ['REQUEST_METHOD'] = req.POST['_method'].upper()
  53.                     if self.log_debug:
  54.                         log.debug('_method found in POST data, altering request method to %s', environ['REQUEST_METHOD'])
  55.                     
  56.                 
  57.             
  58.         
  59.         if self.singleton:
  60.             config = request_config()
  61.             config.mapper = self.mapper
  62.             config.environ = environ
  63.             match = config.mapper_dict
  64.             route = config.route
  65.         else:
  66.             results = self.mapper.routematch(environ = environ)
  67.             if results:
  68.                 match = results[0]
  69.                 route = results[1]
  70.             else:
  71.                 match = None
  72.                 route = None
  73.         if old_method:
  74.             environ['REQUEST_METHOD'] = old_method
  75.         
  76.         if not match:
  77.             match = { }
  78.             if self.log_debug:
  79.                 urlinfo = '%s %s' % (environ['REQUEST_METHOD'], environ['PATH_INFO'])
  80.                 log.debug('No route matched for %s', urlinfo)
  81.             
  82.         elif self.log_debug:
  83.             urlinfo = '%s %s' % (environ['REQUEST_METHOD'], environ['PATH_INFO'])
  84.             log.debug('Matched %s', urlinfo)
  85.             log.debug("Route path: '%s', defaults: %s", route.routepath, route.defaults)
  86.             log.debug('Match dict: %s', match)
  87.         
  88.         url = URLGenerator(self.mapper, environ)
  89.         environ['wsgiorg.routing_args'] = (url, match)
  90.         environ['routes.route'] = route
  91.         environ['routes.url'] = url
  92.         if route and route.redirect:
  93.             route_name = '_redirect_%s' % id(route)
  94.             location = url(route_name, **match)
  95.             log.debug("Using redirect route, redirect to '%s' with statuscode: %s", location, route.redirect_status)
  96.             start_response(route.redirect_status, [
  97.                 ('Content-Type', 'text/plain; charset=utf8'),
  98.                 ('Location', location)])
  99.             return []
  100.         response = self.app(environ, start_response)
  101.         
  102.         try:
  103.             del self.mapper.environ
  104.         except AttributeError:
  105.             None if self.path_info and 'path_info' in match else route.redirect
  106.             None if self.path_info and 'path_info' in match else route.redirect
  107.         except:
  108.             None if self.path_info and 'path_info' in match else route.redirect
  109.  
  110.         return response
  111.  
  112.  
  113.  
  114. def is_form_post(environ):
  115.     content_type = environ.get('CONTENT_TYPE', '').lower()
  116.     if ';' in content_type:
  117.         content_type = content_type.split(';', 1)[0]
  118.     
  119.     return content_type in ('application/x-www-form-urlencoded', 'multipart/form-data')
  120.  
  121.