Package: web.dispatch.object
© 2009-2019 Alice Bevan-McGregor and contributors.https://github.com/marrow/web.dispatch.objectIntroductionDispatch is the process of taking some starting point and a path, then resolving the object that path refers to. This
process is common to almost every web application framework (transforming URLs into controllers), RPC system, and even
filesystem shell. Other terms for this process include: “traversal”, “routing”, or “lookup”.Object dispatch is a particular flavour of dispatch that attempts to resolve path elements as a chain of object
attributes.  This is similar to how Python’simportmachinery would work, with modules being just another object.
A simplified analogy for object dispatch would be that of a filesystem, with “objects” as directories, and
“attributes” as files.This package speaks a standardizeddispatch protocoland
is not entirely intended for direct use by most developers. The target audience is instead the authors of frameworks
that may require such modular dispatch for use by their own users.InstallationInstallingweb.dispatch.objectis easy, just execute the following in a terminal:pip install web.dispatch.objectNote:Westronglyrecommend always using a container, virtualization, or sandboxing environment of some kind when
developing using Python; installing things system-wide is yucky (for a variety of reasons) nine times out of ten.  We
prefer light-weightvirtualenv, others prefer solutions as
robust asVagrant.If you addweb.dispatch.objectto theinstall_requiresargument of the call tosetup()in your
application’ssetup.pyfile, this dispatcher will be automatically installed and made available when your own
application or library is installed.  We recommend using “less than” version numbers to ensure there are no
unintentional side-effects when updating.  Useweb.dispatch.object<3.1to get all bugfixes for the current release,
andweb.dispatch.object<4.0to get bugfixes and feature updates while ensuring that large breaking changes are not
installed.Development VersionDevelopment takes place onGitHubin theweb.dispatch.objectproject.  Issue tracking, documentation, and
downloads are provided there.Installing the current development version requiresGit, a distributed source code management
system.  If you have Git you can run the following to download andlinkthe development version into your Python
runtime:git clone https://github.com/marrow/web.dispatch.object.git
pip install -e 'web.dispatch.object[development]'You can then upgrade to the latest version at any time:(cd web.dispatch.object; git pull; pip install -e .)If you would like to make changes and contribute them back to the project, fork the GitHub project, make your changes,
and submit a pull request.  This process is beyond the scope of this documentation; for more information seeGitHub’s documentation.UsageThis section is split to cover framework authors who will need to integrate the overall protocol into their systems,
and the object interactions this form of dispatch provides for end users.Framework UseAt the most basic level, in order to resolve paths against a tree of objects one must first insantiate the dispatcher:from web.dispatch.object import ObjectDispatch

dispatch = ObjectDispatch()  # Opportunity to pass configuration options here.The object dispatcher currently only has one configurable option:protect. This defaults toTrue, and will
prematurely end dispatch in the event it encounters a path element beginning with an underscore. This protects Python
magic attributes (such as__name__), mangled “private” methods (such as__foo), and protected-by-convention
single underscore prefixed attributes (such as_foo). Python ordinarily does not enforce such protections,
excepting the “mangling” feature which is onlysecurity through obscurity.Now that you have a prepared dispatcher, and presuming you have some “base object” to start dispatch from, you’ll need
to prepare the path according to the protocol:path = "/foo/bar/baz"  # Initial path, i.e. an HTTP request's PATH_INFO.
path = path.split('/')  # Find the path components.
path = path[1:]  # Skip the singular leading slash; see the API specification.
path = deque(path)  # Provide the path as a deque instance, allowing popleft.Of course, the above is rarely split apart like that. We split apart the invidiual steps of path processing here to
more clearly illustrate. In a web framework the above would happen once per request that uses dispatch. This, of
course, frees your framework to use whatever internal or public representation of path you want: choices of
separators, and the ability for deque to consume arbitrary iterables. An RPC system mightspliton a period and
simply not have the possibility of leading separators. Etc.You can now call the dispatcher and iterate the dispatch events:for segment, handler, endpoint, *meta in dispatch(None, some_object, path):
    print(segment, handler, endpoint)  # Do something with this information.The initialNonevalue there represents the “context” to pass along to initializers of classes encountered during
dispatch.  If the valueNoneis provided, classes won’t be instantiated with any arguments. If a context is
provided it will be passed as the first positional argument to instantiation.After completing iteration, check the finalendpoint.  If it isTruethen the path was successfully mapped to
the object referenced by thehandlervariable, otherwise it represents the deepest object that was able to be
found. While some dispatchers might not support partial path resolution and may instead raiseLookupErroror a
subclass, such asAttriuteErrororKeyError, object dispatch does not do this. This is to allow the framework
making use of object dispatch to decide for itself how to proceed in the event of failed or partial lookup, in a
somewhat cleaner way than extensive exception handling within a loop.In the context of a web framework, dispatch being an iterable process makes a lot of sense. In the simplest use of
iteration, path elements would be moved fromPATH_INFOtoSCRIPT_NAMEas dispatch progresses, or to build up a
“bread crumb list” of accessible controllers.You can always just skip straight to the answer if you so choose:segment, handler, endpoint, *meta = list(dispatch(None, some_object, path))[-1]However, providing some mechanism for callbacks or notifications of dispatch is often far more generally useful.Note:It is entirely permissable for dispatchers to returnNoneas a processed path segment. Object dispatch
will do this to announce the starting point of dispatch. This is especially useful if you need to know if the initial
object was a class that was instantiated.  (In that eventhandlerwill be an instance ofsome_objectduring
the first iteration instead of being literallysome_object.)  Other dispatchers may returnNoneat other
times, such as to indicate multiple steps of intermediate processing.Python 2 & 3 CompatibilityThe dispatch protocol is designed to be extendable in the future by usingnamedtuplesubclasses, however this has
an impact on usage as you may have noticed the*metain there. This syntax, introduced in Python 3, will gather
any extraneous tuple elements into a separate list. If you actually care about the metadata do not unpack the tuple
this way.  Instead:for meta in dispatch(None, some_object, path):
    segment, handler, endpoint = step[:3]  # Unpack, but preserve.
    print(segment, handler, endpoint, meta)  # Do something with this information.This document is written from the perspective of modern Python 3, and throwing away the metadata within theforstatement itself provides more compact examples. The above method of unpacking the first three values is the truly
portable way to do this across versions.Dispatchable ObjectsEvery object, of every built-in or third-party class in Python, supports object dispatch. This is because this form
of dispatch is implemented as a series of basicgetattr()calls happening in a loop. In theory, you can dispatch
against anything. In practice, there are certain expectations and protocols you will have to work within. The first of
these notes is extremely important to keep in mind:Bare classes are instantiated with zero or one positional argument, depending on the presence of a context.You can overridegetattr()by providing a__getattr__(self, name)method in your object’s class.Python has no particular distinction between a “real” attribute and one generated by__getattr__, so if
protection is enabled dispatch would stop and your__getattr__method would never be called when
encountering protected path elements.If a callable routine is encountered, it is considered the endpoint regardless of the presence of additional path
elements. This does not extend to classes with__call__methods, allowing mixed use in that situation.With those elements out of the way, we’ll work up from the simplest possible example, a single function:def hola():
    passAny path resolved against a plain function will resolve to that plain function. You can’t “descend” past any routine;
they are, by definition, endpoints. In this instance there will be only a single dispatch event.A slightly more complex example involves a class with callable instances:class Thing:
    def __call__(self):
        passSimilar to an isolated function, an instance of theThingclass will be the endpoint for all paths. As a note,
more specific attributes are preferred over the instance-level__call__, however an empty path (in this example)
will always use the instance as the endpoint, and missing attributes will also use the instance as the endpoint. It is
up to the framework you are using to determine if this is a problem or not, i.e. to allow unprocessed path elements.In the following example:class Thing:
    class foo:
        def bar(self):
            passOnly dispatch to the paths/,/foo, and/foo/barwill resolve, and only/foo/barfinds a recognizable
endpoint. For a somewhat real-world example, the following would successfully represent a database-backed collection
of things, each with their own set of endpoints:class Thing:
    def __init__(self, identifier):
        self._thing = identifier  # This might look it up from the DB.

    def __call__(self):
        pass  # Handle direct access to an identified thing.

    def action(self):
        pass  # This will match any path in the form /<identifier>/action

class Things:
    def __call__(self):
        pass  # This will only handle the path /

    def __getattr__(self, identifier):
        return Thing(identifier)Because there is a__getattr__method and it does not raise anAttributeErrorall first path segments are
valid on theThingsclass, giving you such paths as:/ - Things.__call__
/foo - Thing.__call__
/foo/action - Thing.action
/bar - Thing.__call__
/bar/action - Thing.actionEt cetera.Version HistoryVersion 3.0Python 2 support removed and Python 2 specific code eliminated.Updated to utilize Python 3 namespace packaging.Critical Note: This is not compatible with any Marrow package
that is backwards compatible with Python 2.Version 2.1Massive simplification and conformance to common dispatch protocol.Version 2.0Extract of the object dispatch mechanism from WebCore.Version 1.xProcess fully integrated in the WebCore web framework.Licenseweb.dispatch.object has been released under the MIT Open Source license.The MIT LicenseCopyright © 2009-2019 Alice Bevan-McGregor and contributors.Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
documentation files (the “Software”), to deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit
persons to whom the Software is furnished to do so, subject to the following conditions:The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
Software.THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.