[mod_python] mod_python.publisher and req.path_info.

Graham Dumpleton grahamd at dscpl.com.au
Tue Mar 1 18:39:27 EST 2005


Graham Dumpleton wrote ..
> Now I haven't looked too deeply into new style classes, but from some
> where I have got it in my head that one should be able to using new
> style classes create new methods and dynamically setup the parameters
> they accept. If this is true then _extra_path_info could be replaced
> with a magic method which would create a wrapper around a function
> and automatically fill into the __call__() method all the parameters
> which the wrapped method was expecting. Used in conjunction with
> decorators in Python 2.4, I am speculating that one could simple write:
> 
> @extra_path_info
> def method(req):
>   return req.path_info,req.extra_path_info
> 
> The only problem with this even if it can be done, is that the object which
> wraps the function most likely is an instance of a new style class and
> presently, mod_python.publisher cannot traverse into new style classes.
> Luckily the vampire::publisher code I have and will release at some point
> will, so I can try it out and see how it goes.

Actually, in vampire::publisher I don't need new style classes as I can
reuse one of my internal methods for parameter matching and write:

from vampire.lookup import _params

class _extra_path_info_kw:
  def __init__(self,callback,path=""):
    self.__callback = callback
    self.__path = path
  def __call__(self,**args):
    if args.has_key("req"):
      args["req"].extra_path_info = self.__path
    status,kwargs,expected = _params(self.__callback)
    if status != apache.OK:
      raise apache.SERVER_RETURN, status
    if kwargs == False:
      for name in args.keys():
        if name not in expected:
          del args[name]
    return self.__callback(**args)
  def __getattr__(self,name):
    if name[0] != '_':
      return _extra_path_info_kw(self.__callback,'/'.join([self.__path,name]))
    raise AttributeError()

class A:
  def __init__(self):
    self.method = _extra_path_info_kw(self.method)
  def method(self,req,arg1="1",arg2="2"):
    return req.path_info,req.extra_path_info,arg1,arg2

a = A()

This gives me with one method a way of wrapping methods with
arbitrary form parameters.

In mod_python.publisher there is parameter matching code as well in
util.apply_fs_data() and as a result one can write:

from mod_python import util 

class _extra_path_info_kw:
  def __init__(self,callback,path=""):
    self.__callback = callback
    self.__path = path
  def __call__(self,req):
    req.extra_path_info = self.__path
    return util.apply_fs_data(self.__callback,req.form,req=req)
  def __getattr__(self,name):
    if name[0] != '_':
      return _extra_path_info_kw(self.__callback,'/'.join([self.__path,name]))
    raise AttributeError()

class A:
  def __init__(self):
    self.method = _extra_path_info_kw(self.method)
  def method(self,req,arg1="1",arg2="2"):
    return req.path_info,req.extra_path_info,arg1,arg2

a = A()

This is though going to result in a second processing of req.form in the
case of mod_python.publisher.

Anyway, time to get back to some real work. :-)


Graham


More information about the Mod_python mailing list