{ "info": { "author": "Alice Bevan-McGregor", "author_email": "alice@gothcandy.com", "bugtrack_url": null, "classifiers": [ "Development Status :: 5 - Production/Stable", "Environment :: Console", "Environment :: Web Environment", "Intended Audience :: Developers", "License :: OSI Approved :: MIT License", "Operating System :: OS Independent", "Programming Language :: Python", "Programming Language :: Python :: 2", "Programming Language :: Python :: 2.7", "Programming Language :: Python :: 3", "Programming Language :: Python :: 3.3", "Programming Language :: Python :: 3.4", "Programming Language :: Python :: 3.5", "Programming Language :: Python :: Implementation :: CPython", "Programming Language :: Python :: Implementation :: PyPy", "Topic :: Internet :: WWW/HTTP :: WSGI", "Topic :: Software Development :: Libraries :: Python Modules" ], "description": "==================\nweb.dispatch.route\n==================\n\n \u00a9 2009-2016 Alice Bevan-McGregor and contributors.\n\n..\n\n https://github.com/marrow/web.dispatch.route\n\n..\n\n |latestversion| |ghtag| |downloads| |masterstatus| |mastercover| |masterreq| |ghwatch| |ghstar|\n\n\n\nIntroduction\n============\n\nDispatch is the process of taking some starting point and a path, then resolving the object that path refers to. This\nprocess is common to almost every web application framework (transforming URLs into controllers), RPC system, and even\nfilesystem shell. Other terms for this process include: \"traversal\", \"routing\", or \"lookup\".\n\nRoute-based dispatch is the variant of dispatch that uses handlers for explicitly registered paths, optionally with\nregular expression (regex)-based path elements. This implementation exposes an API that particularly benefits from the\nuse of mix-ins as traits. This gives a clean flexability to routes that are difficult to beat.\n\nMost implementations of regex-based routing do so in a na\u00efve way, often iterating lists of all routes at O(n)\nworst-case. Others allow you to manually partition the space with sub-routers, or optimize by declaration or\nmanual lexicographical order. Some produce monolithic regular expressions that can cause instability when an\napplication grows beyond a certain size. Some even iterate the whole list even after finding an endpoint.\n\nThis dispatcher does not. It builds a tree, and descends the tree preferring static elements to dynamic ones,\nwith a controllalbe presedence at declaration. It optionally handles binding matched dynamic elements to arguments on\nthe resulting endpoint. Performance is O(depth) worst-case.\n\nThis package speaks a standardized `dispatch protocol `_ and\nis not entirely intended for direct use by most developers. The target audience is instead the authors of frameworks\nthat may require such modular dispatch for use by their own users.\n\n\nInstallation\n============\n\nInstalling ``web.dispatch.route`` is easy, just execute the following in a terminal::\n\n pip install web.dispatch.route\n\n**Note:** We *strongly* recommend always using a container, virtualization, or sandboxing environment of some kind when\ndeveloping using Python; installing things system-wide is yucky (for a variety of reasons) nine times out of ten. We\nprefer light-weight `virtualenv `_, others prefer solutions as\nrobust as `Vagrant `_.\n\nIf you add ``web.dispatch.route`` to the ``install_requires`` argument of the call to ``setup()`` in your\napplication's ``setup.py`` file, this dispatcher will be automatically installed and made available when your own\napplication or library is installed. We recommend using \"less than\" version numbers to ensure there are no\nunintentional side-effects when updating. Use ``web.dispatch.route<1.1`` to get all bugfixes for the current release,\nand ``web.dispatch.route<2.0`` to get bugfixes and feature updates while ensuring that large breaking changes are not\ninstalled.\n\n\nDevelopment Version\n-------------------\n\n |developstatus| |developcover| |ghsince| |issuecount| |ghfork|\n\nDevelopment takes place on `GitHub `_ in the \n`web.dispatch.route `_ project. Issue tracking, documentation, and\ndownloads are provided there.\n\nInstalling the current development version requires `Git `_, a distributed source code management\nsystem. If you have Git you can run the following to download and *link* the development version into your Python\nruntime::\n\n git clone https://github.com/marrow/web.dispatch.route.git\n (cd web.dispatch.route; python setup.py develop)\n\nYou can then upgrade to the latest version at any time::\n\n (cd web.dispatch.route; git pull; python setup.py develop)\n\nIf you would like to make changes and contribute them back to the project, fork the GitHub project, make your changes,\nand submit a pull request. This process is beyond the scope of this documentation; for more information see\n`GitHub's documentation `_.\n\n\nUsage\n=====\n\nThis section is split between framework authors who will be integrating the overall protocol into their systems, and\nthe \"producers\" using the system to register routes according to the API.\n\nFramework Use\n-------------\n\nTo begin resolving paths against routes registered in objects, first instantiate the dispatcher::\n\n from web.dispatch.route import RouteDispatch\n \n dispatch = RouteDispatch()\n\nCurrently the route dispatcher has no configuration options. With a prepared dispatcher, and supposing you have some\nobject to dispatch against, you'll need to prepare the path according to the protocol::\n\n path = \"/foo/bar/baz\" # Initial path, i.e. an HTTP request's PATH_INFO.\n path = path.split('/') # Find the path components.\n path = path[1:] # Skip the singular leading slash; see the API specification.\n path = deque(path) # Provide the path as a deque instance, allowing popleft.\n\nOf course, the above is rarely split apart like that. We split apart the invidiual steps of path processing here to\nmore clearly illustrate. In a web framework the above would happen once per request that uses dispatch. This, of\ncourse, frees your framework to use whatever internal or public representation of path you want: choices of\nseparators, and the ability for deque to consume arbitrary iterables. An RPC system might ``split`` on a period and\nsimply not have the possibility of leading separators. Etc.\n\nYou can now call the dispatcher and iterate the dispatch events::\n\n for segment, handler, endpoint, *meta in dispatch(None, some_object, path):\n print(segment, handler, endpoint) # Do something with this information.\n\nThe initial ``None`` value there represents the \"context\" to pass along to initializers of classes encountered during\ndispatch. If the value ``None`` is provided, classes won't be instantiated with any arguments. If a context is\nprovided it will be passed as the first positional argument to instantiation.\n\nAfter completing iteration, check the final ``endpoint``. If it is ``True`` then the path was successfully mapped to\nthe object referenced by the ``handler`` variable. If dispatch is unsuccessful, a ``LookupError`` is raised with an\nexplanation referencing the path element that caused the erorr.\n\nYou can always just skip straight to the answer if you so choose::\n\n try:\n segment, handler, endpoint, *meta = list(dispatch(None, some_object, path))[-1]\n except LookupError:\n ... # Dispatch failed.\n\nHowever, providing some mechanism for callbacks or notifications of dispatch is often far more generally useful.\n\n**Note:** It is entirely permissable for dispatchers to return ``None`` as a processed path segment. Route-based\ndispatch will do this to announce the starting point of dispatch. This is especially useful if you need to know if the\ninitial object was a class that was instantiated. (In that event ``handler`` will be an instance of ``some_object``\nduring the first iteration instead of being literally ``some_object``.) Other dispatchers may return ``None`` at\nother times, such as to indicate multiple steps of intermediate processing.\n\nPython 2 & 3 Compatibility\n~~~~~~~~~~~~~~~~~~~~~~~~~~\n\nThe dispatch protocol is designed to be extendable in the future by using ``namedtuple`` subclasses, however this has\nan impact on usage as you may have noticed the ``*meta`` in there. This syntax, introduced in Python 3, will gather\nany extraneous tuple elements into a separate list. If you actually care about the metadata do not unpack the tuple\nthis way. Instead::\n\n for meta in dispatch(None, some_object, path):\n segment, handler, endpoint = step[:3] # Unpack, but preserve.\n print(segment, handler, endpoint, meta) # Do something with this information.\n\nThis document is written from the perspective of modern Python 3, and throwing away the metadata within the ``for``\nstatement itself provides more compact examples. The above method of unpacking the first three values is the truly\nportable way to do this across versions.\n\n\nBasic Routable Objects\n----------------------\n\nThe simplest routable object is one that has some attribute with a ``__route__`` attribute of its own::\n\n class Root:\n def hello(self, name):\n return \"Hello \" + name\n \n hello.__route__ = '/{name}'\n\nThis defines a method capable of handling any single path element. Because this is a common pattern, and having such\nannotations after the method body, divorced from the method's definition, is ugly, a decorator is provided::\n\n from web.dispatch.route import route\n\n class Root:\n @route('/{name}')\n def hello(self, name):\n return \"Hello \" + name\n\nNow an attempt to access a path such as ``/world`` will result in version of the method with that argument already\nbound to it. The syntax allows for customization of the default expression, which is simply \"any single path element\".\nTo do so, after the name add a colon (``:``) followed by the custom expression. Be careful not to use any forward\nslashes within your expression::\n\n class Root:\n @route('/{name:[a-zA-Z ]+}/{age:[1-9][0-9]*}')\n def hello(self, name, age):\n return name + \" is \" + age + \" years old\"\n\nNow access to ``/dad/27`` is valid, returning a callable that when executed will return ``dad is 27 years old``, but\n``/42/dad`` is invalid, and won't match any routes. When using the ``route`` decorator declaration order is preserved\nvia the ``__index__`` annotation.\n\n\nVersion History\n===============\n\nVersion 1.0\n-----------\n\n* Initial extract from WebCore 2.\n\n\nLicense\n=======\n\nweb.dispatch.route has been released under the MIT Open Source license.\n\nThe MIT License\n---------------\n\nCopyright \u00a9 2009-2016 Alice Bevan-McGregor and contributors.\n\nPermission is hereby granted, free of charge, to any person obtaining a copy of this software and associated\ndocumentation files (the \u201cSoftware\u201d), to deal in the Software without restriction, including without limitation the\nrights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit\npersons to whom the Software is furnished to do so, subject to the following conditions:\n\nThe above copyright notice and this permission notice shall be included in all copies or substantial portions of the\nSoftware.\n\nTHE SOFTWARE IS PROVIDED \u201cAS IS\u201d, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE\nWARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR\nCOPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR\nOTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\n\n\n.. |ghwatch| image:: https://img.shields.io/github/watchers/marrow/web.dispatch.route.svg?style=social&label=Watch\n :target: https://github.com/marrow/web.dispatch.route/subscription\n :alt: Subscribe to project activity on Github.\n\n.. |ghstar| image:: https://img.shields.io/github/stars/marrow/web.dispatch.route.svg?style=social&label=Star\n :target: https://github.com/marrow/web.dispatch.route/subscription\n :alt: Star this project on Github.\n\n.. |ghfork| image:: https://img.shields.io/github/forks/marrow/web.dispatch.route.svg?style=social&label=Fork\n :target: https://github.com/marrow/web.dispatch.route/fork\n :alt: Fork this project on Github.\n\n.. |masterstatus| image:: http://img.shields.io/travis/marrow/web.dispatch.route/master.svg?style=flat\n :target: https://travis-ci.org/marrow/web.dispatch.route/branches\n :alt: Release build status.\n\n.. |mastercover| image:: http://img.shields.io/codecov/c/github/marrow/web.dispatch.route/master.svg?style=flat\n :target: https://codecov.io/github/marrow/web.dispatch.route?branch=master\n :alt: Release test coverage.\n\n.. |masterreq| image:: https://img.shields.io/requires/github/marrow/web.dispatch.route.svg\n :target: https://requires.io/github/marrow/web.dispatch.route/requirements/?branch=master\n :alt: Status of release dependencies.\n\n.. |developstatus| image:: http://img.shields.io/travis/marrow/web.dispatch.route/develop.svg?style=flat\n :target: https://travis-ci.org/marrow/web.dispatch.route/branches\n :alt: Development build status.\n\n.. |developcover| image:: http://img.shields.io/codecov/c/github/marrow/web.dispatch.route/develop.svg?style=flat\n :target: https://codecov.io/github/marrow/web.dispatch.route?branch=develop\n :alt: Development test coverage.\n\n.. |developreq| image:: https://img.shields.io/requires/github/marrow/web.dispatch.route.svg\n :target: https://requires.io/github/marrow/web.dispatch.route/requirements/?branch=develop\n :alt: Status of development dependencies.\n\n.. |issuecount| image:: http://img.shields.io/github/issues-raw/marrow/web.dispatch.route.svg?style=flat\n :target: https://github.com/marrow/web.dispatch.route/issues\n :alt: Github Issues\n\n.. |ghsince| image:: https://img.shields.io/github/commits-since/marrow/web.dispatch.route/1.0.0.svg\n :target: https://github.com/marrow/web.dispatch.route/commits/develop\n :alt: Changes since last release.\n\n.. |ghtag| image:: https://img.shields.io/github/tag/marrow/web.dispatch.route.svg\n :target: https://github.com/marrow/web.dispatch.route/tree/1.0.0\n :alt: Latest Github tagged release.\n\n.. |latestversion| image:: http://img.shields.io/pypi/v/web.dispatch.route.svg?style=flat\n :target: https://pypi.python.org/pypi/web.dispatch.route\n :alt: Latest released version.\n\n.. |downloads| image:: http://img.shields.io/pypi/dw/web.dispatch.route.svg?style=flat\n :target: https://pypi.python.org/pypi/web.dispatch.route\n :alt: Downloads per week.\n\n.. |cake| image:: http://img.shields.io/badge/cake-lie-1b87fb.svg?style=flat", "description_content_type": null, "docs_url": null, "download_url": "UNKNOWN", "downloads": { "last_day": -1, "last_month": -1, "last_week": -1 }, "home_page": "https://github.com/marrow/web.dispatch.route", "keywords": "", "license": "MIT", "maintainer": null, "maintainer_email": null, "name": "web.dispatch.route", "package_url": "https://pypi.org/project/web.dispatch.route/", "platform": "UNKNOWN", "project_url": "https://pypi.org/project/web.dispatch.route/", "project_urls": { "Download": "UNKNOWN", "Homepage": "https://github.com/marrow/web.dispatch.route" }, "release_url": "https://pypi.org/project/web.dispatch.route/1.0.0/", "requires_dist": null, "requires_python": null, "summary": "Route-based dispatch; highly optimized tree-based routes for WebCore, with support for regular expression components.", "version": "1.0.0" }, "last_serial": 1956749, "releases": { "1.0.0": [ { "comment_text": "", "digests": { "md5": "16c66463cc185112f2c0f4fd71af5b2b", "sha256": "6cbb2768a9056221444e787a0d7d62c9657da520bb7b946338ddfe5bf39219f3" }, "downloads": -1, "filename": "web.dispatch.route-1.0.0-py2.py3-none-any.whl", "has_sig": false, "md5_digest": "16c66463cc185112f2c0f4fd71af5b2b", "packagetype": "bdist_wheel", "python_version": "3.5", "requires_python": null, "size": 17676, "upload_time": "2016-02-15T08:00:42", "url": "https://files.pythonhosted.org/packages/53/2c/3d8dce2018a28a98ffcfd86ce5f31ea3f311ce1be0fc7a0b48f9aca066c9/web.dispatch.route-1.0.0-py2.py3-none-any.whl" }, { "comment_text": "", "digests": { "md5": "352f089ff3eee48276830759e587a260", "sha256": "3de7515a9c1888a62c1cbc4526133c390bf84feb8a6f803fb4712fee7e319e6e" }, "downloads": -1, "filename": "web.dispatch.route-1.0.0-py3.5.egg", "has_sig": false, "md5_digest": "352f089ff3eee48276830759e587a260", "packagetype": "bdist_egg", "python_version": "3.5", "requires_python": null, "size": 16207, "upload_time": "2016-02-15T08:00:34", "url": "https://files.pythonhosted.org/packages/a9/ad/00b099bab1562704f710043c56e2edc091840be9b40c1e11af672baf1c72/web.dispatch.route-1.0.0-py3.5.egg" }, { "comment_text": "", "digests": { "md5": "3c30de4c7e706621fe1ffcb9291043f2", "sha256": "b03b156d7e1e93ff790b78d415524ab7ab585fad9c38df75b0c1cd512743873c" }, "downloads": -1, "filename": "web.dispatch.route-1.0.0.tar.gz", "has_sig": false, "md5_digest": "3c30de4c7e706621fe1ffcb9291043f2", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 17217, "upload_time": "2016-02-15T08:00:27", "url": "https://files.pythonhosted.org/packages/9a/69/e3de5a9bfbfc023665fbe2ad672281350324e27db68eb108eda6abf3b006/web.dispatch.route-1.0.0.tar.gz" } ] }, "urls": [ { "comment_text": "", "digests": { "md5": "16c66463cc185112f2c0f4fd71af5b2b", "sha256": "6cbb2768a9056221444e787a0d7d62c9657da520bb7b946338ddfe5bf39219f3" }, "downloads": -1, "filename": "web.dispatch.route-1.0.0-py2.py3-none-any.whl", "has_sig": false, "md5_digest": "16c66463cc185112f2c0f4fd71af5b2b", "packagetype": "bdist_wheel", "python_version": "3.5", "requires_python": null, "size": 17676, "upload_time": "2016-02-15T08:00:42", "url": "https://files.pythonhosted.org/packages/53/2c/3d8dce2018a28a98ffcfd86ce5f31ea3f311ce1be0fc7a0b48f9aca066c9/web.dispatch.route-1.0.0-py2.py3-none-any.whl" }, { "comment_text": "", "digests": { "md5": "352f089ff3eee48276830759e587a260", "sha256": "3de7515a9c1888a62c1cbc4526133c390bf84feb8a6f803fb4712fee7e319e6e" }, "downloads": -1, "filename": "web.dispatch.route-1.0.0-py3.5.egg", "has_sig": false, "md5_digest": "352f089ff3eee48276830759e587a260", "packagetype": "bdist_egg", "python_version": "3.5", "requires_python": null, "size": 16207, "upload_time": "2016-02-15T08:00:34", "url": "https://files.pythonhosted.org/packages/a9/ad/00b099bab1562704f710043c56e2edc091840be9b40c1e11af672baf1c72/web.dispatch.route-1.0.0-py3.5.egg" }, { "comment_text": "", "digests": { "md5": "3c30de4c7e706621fe1ffcb9291043f2", "sha256": "b03b156d7e1e93ff790b78d415524ab7ab585fad9c38df75b0c1cd512743873c" }, "downloads": -1, "filename": "web.dispatch.route-1.0.0.tar.gz", "has_sig": false, "md5_digest": "3c30de4c7e706621fe1ffcb9291043f2", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 17217, "upload_time": "2016-02-15T08:00:27", "url": "https://files.pythonhosted.org/packages/9a/69/e3de5a9bfbfc023665fbe2ad672281350324e27db68eb108eda6abf3b006/web.dispatch.route-1.0.0.tar.gz" } ] }