+

:4736 Function create_function() is deprecated [8192]

Last commit for mycoserver/router.py: 7004713412b51ec58cf01149ba736fadf769acaf

Initial commit

Benjamin Renard [2014-01-12 00:33:07]
Initial commit
  1. # -*- coding: utf-8 -*-
  2.  
  3.  
  4. """Helpers for URLs"""
  5.  
  6.  
  7. import logging
  8. import re
  9.  
  10. from webob.dec import wsgify
  11.  
  12. from . import wsgi_helpers
  13.  
  14.  
  15. log = logging.getLogger(__name__)
  16.  
  17.  
  18. def make_router(*routings):
  19. """Return a WSGI application that dispatches requests to controllers."""
  20. routes = []
  21. for routing in routings:
  22. methods, regex, app = routing[:3]
  23. if isinstance(methods, basestring):
  24. methods = (methods,)
  25. vars = routing[3] if len(routing) >= 4 else {}
  26. routes.append((methods, re.compile(regex), app, vars))
  27.  
  28. @wsgify
  29. def router(req):
  30. """Dispatch request to controllers."""
  31. split_path_info = req.path_info.split('/')
  32. assert not split_path_info[0], split_path_info
  33. for methods, regex, app, vars in routes:
  34. if methods is None or req.method in methods:
  35. match = regex.match(req.path_info)
  36. if match is not None:
  37. log.debug(u'URL path = {path} matched controller {controller}'.format(
  38. controller=app, path=req.path_info))
  39. if getattr(req, 'urlvars', None) is None:
  40. req.urlvars = {}
  41. req.urlvars.update(dict(
  42. (name, value.decode('utf-8') if value is not None else None)
  43. for name, value in match.groupdict().iteritems()
  44. ))
  45. req.urlvars.update(vars)
  46. req.script_name += req.path_info[:match.end()]
  47. req.path_info = req.path_info[match.end():]
  48. return req.get_response(app)
  49. return wsgi_helpers.not_found(req.ctx)
  50. return router