Making Resources aware of their location in the resource tree

[email protected] Wed, 25 Nov 2009 04:34:26 -0000
Newsgroups gmane.comp.python.twisted.bugs
Message-ID <[email protected]>
New submission from jpsimons <[email protected]>:

I'd like to be able to generate full paths to resources, for linking to them. Currently this isn't possible because `putChild` doesn't set any context information in the putted child such as the parent resource or even the child's own path component. I am able to monkey patch it like so:

{{{
# Patch `putChild` to keep track of hierarchy in both directions
def putChild(self, path, child):
    self.children[path] = child
    child.server = self.server
    child.parent = self # Addition
    child.path_component = path # Addition
from twisted.web.resource import Resource
Resource.__dict__['putChild'] = putChild
}}}

This enables full path generation like so:

{{{
parts = []
while rsrc and hasattr(rsrc, "path_component"):
    parts.insert(0, getattr(rsrc, "path_component"))
    rsrc = getattr(rsrc, "parent", None)
return "/" + "/".join(parts)
}}}

However the drawback is this monkey patching has to run before any children are added.

More information about my use case:

I'm building some blogging software that lets you drop in pieces wherever you like in the resource tree, such as:

{{{
someResource.putChild("blog_admin", BlogAdmin())
someOtherResource.putChild("blog_files", BlogFiles())
}}}

BlogFiles() serves up blog posting attachments like photos. In order for a blog posting to link to its resources, it needs to know where the BlogFiles resource sits in the overall resource tree.

The change required is small (only two lines) so it seems low-risk to me. Any chance of getting this in twisted.web?



----------
Type     : enhancement
Component: web
Keywords : web resource putChild
Priority : normal
Nosy     : 
----------
http://twistedmatrix.com/trac/ticket/4133