Graham Dumpleton
grahamd at dscpl.com.au
Thu Feb 10 04:02:32 EST 2005
On 10/02/2005, at 10:56 AM, Graham Dumpleton wrote: > Shawn Harrison wrote .. >>> 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? > > If you expect the files/paths/URLs to be named exactly as you state, > then for the currently available version of Vampire, the short answer > is "No". Having thought more about this. The particular case where there is actually a physical resource, but where you might want to represent it in a different format, is much better handled in other ways in Vampire. Take for example where physical file "/photos/2004/10/01/137.jpg" exists, but where you want to be able to access it as "/photos/2004/10/01/137.html" and thus have the image automatically framed in in appropriate HTML page. One would take a modified version of handler I already sent you. What you call it or where it is stored is irrelevant, but I'll save it as the file "/photos/index.py" to match what you want. Next thing is to put the ".htaccess" file in "photos" subdirectory with it containing: Options -Indexes -MultiViews SetHandler python-program PythonHandler vampire PythonPath 'sys.path' PythonDebug On PythonOption VampireDefaultHandlers On Ie., have added "VampireDefaultHandlers" option to enabled default handler lookup in Vampire. Then add into the "photos" directory the file ".vampire" which contains: [Handlers] handler_html = %(__config_root__)s/index.py I know this doesn't follow the REST style of URLs that you were using, ie., no extension, but the actual image is still accessed with an extension so using a ".html" extension makes more sense to me than a naked URL. If you really wanted naked URLs, instead of "handler_html" in .vampire file and in name of method of "index.py", use just "handler". The code will need to be tweaked a bit as it strips off the ".html" extension in places, but it will yield exactly what you wanted. Anyway, for the ".html" case, setting this up this way means that Vampire will if it cannot find an appropriate content handler in the directory hierarchy itself to URL, fallback to using the default handler specified by the ".vampire" configuration file. The content handler can then determine if the URL equates to a virtual resource matched to a physical image file. If it does, it returns a HTML page which within itself loads the image. The content handler doesn't even need to play with req.path_info as req.filename and req.uri are going to be correct for the target already, one just needs to fiddle with the extension to identify the associated physical image file. When it comes down to accessing resources which are completely virtual, ie., no physical equivalent in the document hierarchy, then that is where the script for a virtual directory is used as described previously. Include below is the code for the "index.py" content handler as described. from mod_python import apache import os import posixpath import vampire _PAGE = """ <html> <body> <h1>Photo: %(name)s</h1> <img src="%(url)s" /> </body> </html> """ # Handler for virtual directory. def handler_html(req): # Determine what type of image file it is. path = None extn = None allowed = [ ".jpg", ".gif" ] stub = os.path.splitext(req.filename)[0] for suffix in allowed: file = stub + 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. config = vampire.loadConfig(req,".vampire") defaults = config.defaults() url = os.path.splitext(req.uri)[0] + extn name = os.path.splitext(req.uri[len(defaults["__baseurl_abs__"]):][1:])[0] 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
|