__init__.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. """When it comes to combining multiple controller or view functions
  2. (however you want to call them) you need a dispatcher. A simple way
  3. would be applying regular expression tests on the ``PATH_INFO`` and
  4. calling registered callback functions that return the value then.
  5. This module implements a much more powerful system than simple regular
  6. expression matching because it can also convert values in the URLs and
  7. build URLs.
  8. Here a simple example that creates a URL map for an application with
  9. two subdomains (www and kb) and some URL rules:
  10. .. code-block:: python
  11. m = Map([
  12. # Static URLs
  13. Rule('/', endpoint='static/index'),
  14. Rule('/about', endpoint='static/about'),
  15. Rule('/help', endpoint='static/help'),
  16. # Knowledge Base
  17. Subdomain('kb', [
  18. Rule('/', endpoint='kb/index'),
  19. Rule('/browse/', endpoint='kb/browse'),
  20. Rule('/browse/<int:id>/', endpoint='kb/browse'),
  21. Rule('/browse/<int:id>/<int:page>', endpoint='kb/browse')
  22. ])
  23. ], default_subdomain='www')
  24. If the application doesn't use subdomains it's perfectly fine to not set
  25. the default subdomain and not use the `Subdomain` rule factory. The
  26. endpoint in the rules can be anything, for example import paths or
  27. unique identifiers. The WSGI application can use those endpoints to get the
  28. handler for that URL. It doesn't have to be a string at all but it's
  29. recommended.
  30. Now it's possible to create a URL adapter for one of the subdomains and
  31. build URLs:
  32. .. code-block:: python
  33. c = m.bind('example.com')
  34. c.build("kb/browse", dict(id=42))
  35. 'http://kb.example.com/browse/42/'
  36. c.build("kb/browse", dict())
  37. 'http://kb.example.com/browse/'
  38. c.build("kb/browse", dict(id=42, page=3))
  39. 'http://kb.example.com/browse/42/3'
  40. c.build("static/about")
  41. '/about'
  42. c.build("static/index", force_external=True)
  43. 'http://www.example.com/'
  44. c = m.bind('example.com', subdomain='kb')
  45. c.build("static/about")
  46. 'http://www.example.com/about'
  47. The first argument to bind is the server name *without* the subdomain.
  48. Per default it will assume that the script is mounted on the root, but
  49. often that's not the case so you can provide the real mount point as
  50. second argument:
  51. .. code-block:: python
  52. c = m.bind('example.com', '/applications/example')
  53. The third argument can be the subdomain, if not given the default
  54. subdomain is used. For more details about binding have a look at the
  55. documentation of the `MapAdapter`.
  56. And here is how you can match URLs:
  57. .. code-block:: python
  58. c = m.bind('example.com')
  59. c.match("/")
  60. ('static/index', {})
  61. c.match("/about")
  62. ('static/about', {})
  63. c = m.bind('example.com', '/', 'kb')
  64. c.match("/")
  65. ('kb/index', {})
  66. c.match("/browse/42/23")
  67. ('kb/browse', {'id': 42, 'page': 23})
  68. If matching fails you get a ``NotFound`` exception, if the rule thinks
  69. it's a good idea to redirect (for example because the URL was defined
  70. to have a slash at the end but the request was missing that slash) it
  71. will raise a ``RequestRedirect`` exception. Both are subclasses of
  72. ``HTTPException`` so you can use those errors as responses in the
  73. application.
  74. If matching succeeded but the URL rule was incompatible to the given
  75. method (for example there were only rules for ``GET`` and ``HEAD`` but
  76. routing tried to match a ``POST`` request) a ``MethodNotAllowed``
  77. exception is raised.
  78. """
  79. from .converters import AnyConverter as AnyConverter
  80. from .converters import BaseConverter as BaseConverter
  81. from .converters import FloatConverter as FloatConverter
  82. from .converters import IntegerConverter as IntegerConverter
  83. from .converters import PathConverter as PathConverter
  84. from .converters import UnicodeConverter as UnicodeConverter
  85. from .converters import UUIDConverter as UUIDConverter
  86. from .converters import ValidationError as ValidationError
  87. from .exceptions import BuildError as BuildError
  88. from .exceptions import NoMatch as NoMatch
  89. from .exceptions import RequestAliasRedirect as RequestAliasRedirect
  90. from .exceptions import RequestPath as RequestPath
  91. from .exceptions import RequestRedirect as RequestRedirect
  92. from .exceptions import RoutingException as RoutingException
  93. from .exceptions import WebsocketMismatch as WebsocketMismatch
  94. from .map import Map as Map
  95. from .map import MapAdapter as MapAdapter
  96. from .matcher import StateMachineMatcher as StateMachineMatcher
  97. from .rules import EndpointPrefix as EndpointPrefix
  98. from .rules import parse_converter_args as parse_converter_args
  99. from .rules import Rule as Rule
  100. from .rules import RuleFactory as RuleFactory
  101. from .rules import RuleTemplate as RuleTemplate
  102. from .rules import RuleTemplateFactory as RuleTemplateFactory
  103. from .rules import Subdomain as Subdomain
  104. from .rules import Submount as Submount