[mod_python] patch for default functions in the publisher handler (fwd)

Matt Hughes mhughe at ucalgary.ca
Wed Jul 11 22:41:49 EST 2001


This is sort of a follow up to this message:
http://www.modpython.org/pipermail/mod_python/2001-June/001421.html, which
discussed how to call a default function from a file using the publisher
handler. 

I wanted similar functionality, but didn't want to resort to the redirect
directive suggested in the last message of that thread. This patch looks
for the PATH_INFO as before, but instead of raising an
apache.HTTP_NOT_FOUND, it puts "default" in func_path as the default
function to call. 

The default function could be made a configuration directive, but I
haven't investigated that. 

This patch also solves some problems I was having when importing modules
in sub-directories. I had a structure like this:

/
/subdir1
/subdir1/subdir2
/subdir1/subdir2/subdir3

and so on, with each directory containing a index.py file which is
configured as the index with the DirectoryIndex directive. The problem was
that a URL refering to /subdir1/subdir2/subdir3 or whatever always found
the index.py file in /. 

This patch fixes this by treating directories off the root as packages.
It's a compelete hack, but it works for me. 


//
// Matt Hughes
// http://www.ucalgary.ca/~mhughe
//

-------------- next part --------------
--- orig-pub.py	Mon Jul  9 20:43:40 2001
+++ publisher.py	Mon Jul  9 20:53:26 2001
@@ -65,6 +65,8 @@
 
 import new
 
+import sys
+
 # list of suffixes - .py, .pyc, etc.
 suffixes = map(lambda x: x[0], imp.get_suffixes())
 
@@ -81,10 +83,11 @@
 
     # get the path PATH_INFO (everthing after script)
     if not _req.subprocess_env.has_key("PATH_INFO"):
-        raise apache.SERVER_RETURN, apache.HTTP_NOT_FOUND
-    
-    func_path = _req.subprocess_env["PATH_INFO"][1:] # skip fist /
-    func_path = string.replace(func_path, "/", ".")
+        func_path = "default"  # Call for a default method
+    else:
+        func_path = _req.subprocess_env["PATH_INFO"][1:] # skip fist /
+        func_path = string.replace(func_path, "/", ".")
+        
     if func_path[-1] == ".":
         func_path = func_path[:-1] 
 
@@ -118,11 +121,19 @@
     # add req
     args["req"] = req
 
-    # import the script
-    path, module_name =  os.path.split(_req.filename)
+    l = len(sys.path[0])
+    module_name = _req.filename[l:]
 
-    # get rid of the suffix
+    # get rid of the suffix... 
     module_name = suff_matcher.sub("", module_name)
+
+    # An alternate way to get rid of the suffix
+    #pos = string.rfind(module_name, '.')
+    #module_name = module_name[:pos]      
+
+    module_name = string.replace(module_name, '/', '.')
+
+    path = _req.filename[0:l]
 
     # import module (or reload if needed)
     module = apache.import_module(module_name, _req, [path])


More information about the Mod_python mailing list