pythonurlmod-pythonfriendly-url

mod_python publisher and pretty URLs


I am new to Python (I am getting out of PHP because of how increasingly broken it is), and I am racing through porting my old code. One thing:

I have a file /foo.py with functions index() and bar(), so, with the publisher I can access http://domain/foo/bar and http://domain/foo as the documentation suggests.

How can I have it such that I can do:

http://domain/foo/bar/a1/a2/a3/an/...

Such that the publisher launches bar() and then I can access the URL to obtain /a1/a2/... All I get is Forbidden :) (I don't want to use mod_rewrite on everything)

Oh, im on 2.5.2 Thanks in advance

UPDATE: The ideal solution would be for the publisher to launch the furthest-right resolution in the URL it can, and simply make a1/a2/a3.. accessible through the apache module. Maybe a combination of an apache directive and publisher?

SOLVED (ish): The answer of the magic call() method and so on is juicy! Although I think I will modify the publisher or write my own to inspect objects in a similar way using furthest-right matching, then allowing the furthest-right to access the URL using apache module. Thanks all!


Solution

  • You would have to have an object bar.a1.a2.a3.an defined within your foo.py module. Basically, the publisher handler replaces the slashes in the URL with dots, and tries to find some Python object with that name.

    Here's something wacky you could try: in foo.py:

    class _barclass(object):
        def __init__(self, parent, name):
            if parent and name:
                self.path = parent.path + '/' + name
                setattr(parent, name, self)
            else:
                self.path = ''
        def __getattr__(self, name):
            return _barclass(self, name)
        def __call__(self):
            # do your processing here
            # url path is contained in self.path
    
    bar = _barclass(None, None)
    

    Although this is kind of straining the bounds of what the publisher is meant to do - you might be better off writing your own handlers from scratch. (Or using something like Django.)