[mod_python] Getting the script name with publisher

Bart Whiteley bwhiteley at novell.com
Wed Mar 1 16:18:32 EST 2006


On Wed, 2006-03-01 at 22:01 +0100, d wrote:
> On Mar 1, 2006, at 9:19 PM, Bart Whiteley wrote:
> 
> > In order to generate hyperlinks, I need a reliable way to determine  
> > the
> > script name while using the publisher handler.  I hunted around for a
> > bit, and only found this from years ago that wasn't answered:
> > http://www.modpython.org/pipermail/mod_python/2003-September/ 
> > 014133.html
> >
> > I was doing this for a while:
> >   req.uri[:-len(req.path_info)]
> >
> > This worked until I renamed my script to index.py, then it fell apart.
> > So, I guess what I really need is not the script name, but the portion
> > of the URI prior to the method name.  In some cases this might be the
> > script name (possibly without the '.py').  In the case of index.py, it
> > might be the folder containing index.py.
> >
> > As an example, I've set up a script loosely based on hello.py:
> > http://modpython.org/live/current/doc-html/hand-pub-intro.html
> >
> > I place it in /srv/www/htdocs/mptest/index.py (docroot
> > is /srv/www/htdocs).
> > When I access it like this: http://www/mptest/say/hello,
> > I see the following vars:
> > SCRIPT_FILENAME		/srv/www/htdocs/mptest/say
> > PATH_INFO		/hello
> > SCRIPT_NAME		/mptest/say
> >
> > In this example, I'd like to isolate "/mptest".
> >
> > Does anyone have a way to reliable isolate the script name, or the
> > folder containing index.py?
> 
> You mean `os.path.basename (__file__)` ?
> 

Thanks.  I didn't know about __file__.  I how have this which seems to
work in all cases: 

def _baseScript(req):
    drlen = len(req.subprocess_env['DOCUMENT_ROOT'])
    if os.path.basename(__file__) == 'index.py':
        return os.path.dirname(__file__)[drlen:]
    else:
        return __file__[drlen:-3]




More information about the Mod_python mailing list