[mod_python] Re: Photo Album Viewer and req.path_into.

Shawn Harrison harrison at tbc.net
Wed Feb 9 10:28:45 EST 2005


Graham Dumpleton wrote [02/04/05 9:51 PM]:
> 
> On 05/02/2005, at 1:27 AM, Shawn Harrison wrote:
>
> [...]
>
>> I do have a question about Vampire. I want to have a Python script
>>
>>     /web/websitename/www/photos/index.py
>>
>> where I've appended /web to sys.path, so that
>>
>>     websitename.www.photos.index
>>
>> is a module name. Okay. I want to publish photos with urls like
>>
>>     http://websitename/photos/2004/10/01/137
>>
>> Can Vampire send all requests for paths under /photos/ to the photos
> 
> 
> Not knowing for sure what you are wanting to do, I am going to assume that
> you want to wrap image files in a HTML page automatically.

Yes, that's the idea, although I only gave photos as an example: What I 
really mean is this:
* If my document root is /web/examplecom/www/, and
* I have a request handler at /web/example/www/photos/index.py, and
* the photos are in /web/example/www/photos/
* Vampire is the PythonHandler, and
* req.uri is /photos/2004/10/01/137, then
? will the handler in /photos/index.py pick up the request?

I've been using my own homebrewed handler to publish objects dynamically 
in this fashion (using req.path_info, etc.). But, it's not ideal.

I am asking this basic question about Vampire because the answer with 
mod_python.publisher appears to be, no, it won't (which is why I made ye 
olde homebrewe a few months ago). The answer with Quixote is, yes it 
could if I set it up right. The answer for Vampire is not clear in its 
documentation.

Based on the rest of your reply, I am inferring that Vampire can handle 
dynamic object publishing from a database or filesystem, but that the 
set up would have to be different from what I've been using.

> Firstly, Vampire is structured so as to avoid altogether use of sys.path
> and all the complications that can arise because of it. Thus, you would
> not add "/web" to sys.path. 

Having '/web' in sys.path is fairly basic to the architecture that I've 
been developing. Every "web service" is a folder under /web, and each 
includes both request handlers and regular Python modules, including 
database access modules. I also want to be able to use Apache as one 
front end among many, not as the only one; so I need to be able to 
import modules in a more-or-less Python-native fashion.

> Thus, to configure things, one would use:
> 
>   Options -Indexes -MultiViews
> 
>   SetHandler python-program
>   PythonHandler vampire
>   PythonPath 'sys.path'
>   PythonDebug On
> 
> Indexes are turned off because we wouldn't want it, and MultiViews is
>  turned off because auto negotiation of file types screws with how
> Vampire works.

That makes sense.

> PythonPath is set to "sys.path" in order to stop mod_python from
> adding that directory into "sys.path" as we don't want that either.
> This is so nothing tries to attempt to load any Python code files in
> the document tree as Python modules using "import".
> 
> Assuming that the "photos" directory is where the images actually
> live, am going to change things around a little and rename "photos"
> to "library".
> [...]
> Note that the reason I had to rename the directory containing the images is
> that the way Apache resolves paths, it will give precedence to subdirectory
> search matching and thus will not hand off control to Vampire properly if I
> had "photos.py" handler and "photos" directory.

I don't understand why this is so, but I also don't understand the 
innards of the Vampire. In my crufty homebrew handler, this kind of 
situation resolves to using the handler in 
/web/examplecom/www/photos/index.py.

But I see that you achieve essentially the same results. There is 
something to be said for keeping the data files (which you put in 
/library) separate from the application code.

> [...]
> Hope this is of interest. This might actually be a good example for me of
> showing off Vampire. Although, I could see it being extended further than
> this simple bit of code to become a nice photo album viewer.

Yes, it's an interesting example, and it provides a lot of information 
about how Vampire would deal with various usage scenarios. You should 
add some of these details to your documentation.
> 
> Graham
> 
> 
> # Now for the code.

I enjoyed reading it.
> 
> from mod_python import apache
> 
> import os
> import posixpath
> 
> _PAGE = """
> <html>
> <body>
> <h1>Photo: %(name)s</h1>
> <img src="%(url)s" />
> </body>
> </html>
> """
> 
> # Handler for virtual directory.
> 
> def handler(req):
> 
>   # Determine stub of image file to view.
> 
>   if req.path_info == "":
>     return apache.HTTP_BAD_REQUEST
>   elif req.path_info == "/":
>     return apache.HTTP_BAD_REQUEST
> 
>   name = os.path.splitext(req.path_info[1:])[0]
> 
>   # Determine what type of image file it is.
> 
>   subdir = "library"
>   # subdir = "."  # use this if wanting to make it "browse.py" in 
> "photos" subdir
> 
>   root = os.path.join(os.path.dirname(req.filename),subdir)
> 
>   path = None
>   extn = None
> 
>   allowed = [ ".jpg", ".gif" ]

should be

     allowed = ['.jpg', '.png']

;-)
> 
>   for suffix in allowed:
>     file = os.path.join(root,name) + suffix
> 
>     if os.path.exists(file):
>       path = file
>       extn = suffix
> 
>   if path == None:
>     return apache.HTTP_NOT_FOUND
> 
>   # Determine the URL to the actual image.
> 
>   baseurl = os.path.dirname(req.uri[:-len(req.path_info)])
> 
>   url = posixpath.join(baseurl,subdir,name+extn)
> 
>   content = _PAGE % { "name": name, "url": url }
> 
>   # Return the rendered page content.
> 
>   req.content_type = "text/html"
>   req.send_http_header()
> 
>   req.write(content)
> 
>   return apache.OK

Thanks again,

-- 
________________
harrison at tbc.net


More information about the Mod_python mailing list