- http://localhost:8080/
- http://localhost:8080/mystuff
- http://localhost:8080/yourstuff
It's actually fairly straight-forward. All we need to do is the following:
- Create a tuple of patterns.
- Override the locateChild() method in our "root" class.
urlPatterns = (Then, in SiteRoot, we could do something like the following:
(r'/mystuff(.*)', MyStuffResource),
(r'/yourstuff/blog(.*)', BlogResource),
(r'/yourstuff(.*)', YourStuffResource),
)
def locateChild(self, context, segments):What we're doing here is interrupting the "natural" flow of Nevow's path processing. If there are more segments once we've found a match, we pass the additional segments on to the child resource's locateChild() method. If not, we have a final destination and can return the resource itself.
path = '/'.join(('',) + segments)
for regex, resource in urlPatterns:
match = re.match(regex, path)
if match:
newPath = match.groups()[0]
r = resource()
if newPath in ['', '/']:
return r, ()
else:
newSegments = newPath.split('/')[1:]
return r.locateChild(context, newSegments)
return super(SiteRoot, self).locateChild(context, segments)
Here are some screenshots of this in action:
You can browse the code for this at the following links:
- The example as a single .tac file
- The same example, split up into separate files
3 comments:
Have you tried routes as well?
http://routes.groovie.org/integration.html
That's actually the third planned object publishing article in this series. It's still "planned", though (i.e., not definite), as routes really doesn't fit my brain and may, in fact, not be a good match for Nevow. I will give it a shot, and if I can get it working well, I'll write that up too :-)
Thanks!
Thanks!
Post a Comment