Fwd: [mod_python] Physical paths ?

Graham Dumpleton graham.dumpleton at gmail.com
Sat Feb 2 16:04:20 EST 2008

Please keep your replies on the list.

FYI. That code was setting req.application_root so you could use it as
a reference, not trying to use it.


  PythonHandler somemodulenameinrootofpspfilesdirectory .psp
  PythonHandler mod_python.psp .psp

In somemodulenameinrootofpspfilesdirectory.py have:

  import os

  def handler(req):
    req.application_root = os.path,dirname(__file__)
    return apache.OK

Read the documentation about stacked mod_python handlers to see how this works.

Have no time to read all this new mail now, maybe someone else will help.


---------- Forwarded message ----------
From: S.R.Pardá <linux at qbox.es>
Date: 2 Feb 2008 21:26
Subject: Re: [mod_python] Physical paths ?
To: Graham Dumpleton <graham.dumpleton at gmail.com>

Thank You.

I'm using: PythonHandler mod_python.psp and req.application_root doesn't
But __file__ is '/usr/lib/python2.5/site-packages/mod_python/psp.pyc' in
the psp page, so it doen't seem related to the root of my app pages
where I wnt to save.

Anyway, I will try to explain it better, so it can help any other person
in the future looking for info like this.

Basically, I want the mod_python psp application, allow content upload
from the user client:

That's done in:         http://SERVER/MyApp/config/upload.psp
(and that page is in /home/user/MyApp/config/index.psp)

And I want to save the user file to a directory accesible later at:
(that page would be in /home/user/MyApp/images/users)

So the upload.psp save the file received from form at that images
directory (instead of the form directory).

I wanted that the code from upload psp will pass a relative path from
the root of the App (/MyApp) to the module that saves the file, that is
something like:
        save_file(USER_FILE,"images/users/", req)

I needed the save_file to obtain the physical path to the URL:
        '//SERVER' + '/MyApp/' + 'images/users/'
        (server + application_root + destiny_folder)
that is, the physical path to save the file:

Of course, I didn't wanted the function to use a constant to the root of
the application '/home/user/MyApp' (at code time) to allow to the
application to be independent from the installation directory.

I know req.filename give me the physical path to
that is:

so I could save to '/home/user/MyApp/config/' without problem. And the
same in some subdirectory like '/home/user/MyApp/config/images/users/'.
I know.

But, I had an images directory in '/home/user/MyApp/images/', and maybe
I like the things in the hard way. And decide to save to that directory
of images (don't ask me why).
I know that ... application root is at '..' relative to the physical
directory of the page ('/home/user/MyApp/config/').
But I really like the things in the hard way and I wanted the save
function to be usable from any other pages in other directories, and I
don't wanted to send a constant relative path to the app root (remember
'/home/user/MyApp/') from every page to the save function (because, I
want app to be independent of the installation directory, and I don't
want to worry about the constant relative paths if I move the pages).

So, the code I'm using now, take the physical path of the page, and the
uri of the page , and the application root (uri), to obtain the physical
path of the application.
And from that , I can take the pyshical path of any subdirectory
relative to root (only if no subdirectory with alias are involved, of

def physicalAppRoot(req):

  rootApp = req.get_options()['mod_python.session.application_path']
  pageDir, pageName = os.path.split(req.parsed_uri[6])

  if pageDir.startswith(rootApp):
     appPageDir = pageDir[len(rootApp):]
     appPageDir = pageDir

  physPath = os.path.dirname(req.filename)

  i = physPath.rfind(appPageDir)
  physAppRoot = physPath[:i]+os.path.sep

  return physAppRoot

The application path is an apache's directory alias, so option
application_path is indicated in the config (like you said me Graham, to
set different paths for cookies from various applications, and that
worked great for me).

That's work for me but, really all I wanted was some function like:

        physicalPath(uri = 'images/users/')
        physicalPath(uri = '/MyApp/images/users/')

that give me:  '/home/user/MyApp/images/users/'
for an apache mod_python application installed in /home/user/MyApp,
accessible at 'http://server/MyApp' (because an alias would be set at
apache 'alias /MyApp/ "/home/user/MyApp"').

Sorry, if I can explain it better. Maybe I'm doing a silly thing,
because an apache function available at mod_python does exctly this, but
I didn't found it.

Thank You,


El sáb, 02-02-2008 a las 08:36 +1100, Graham Dumpleton escribió:
> I think I only partly understand what you want. Anyway, work out what
> the following would do if done in your mod_python handler.
>   import os
>   req.application_root = os.path.dirname(__file__)
> Graham
> On 01/02/2008, S.R.Pardá <linux at qbox.es> wrote:
> > Hi,
> >
> > Now, thank you to your help,  my applicattion is capable of receive
> > image files from users using forms.
> > And app store them into a physical path of the server.
> >
> >
> > It's possible to save from the app to an uri folder ?
> >
> > Can I know the physical path of one uri ?
> >
> >
> > I can save easily to a subfolder of the pyhsical path obtained by
> > req.filename relative to the page visited, and that can work without
> > problem.
> >
> >
> > But I would like the module (the module that implements the form
> > proccesing) receive the relative route relative to the root of the
> > application path, so module will not need to know the physical structure
> > of directories, and can save to a route not descendent of the directory
> > of my own page.
> >
> > ... And in my app, the root of the app is a directory alias. outside of
> > the apache document_root '/var/www'
> >
> >
> > That's it
> >
> >         DocumentRoot    /             /var/www
> >
> >         Alias           MyApp         /home/user/MyApp/
> >
> >         /MyApp/index.psp   .......... /home/user/MyApp/index.psp
> >
> >         /MyApp/config/upload.psp  ... /home/user/MyApp/config/index.psp
> >
> >         /MyApp/images/  ............. /home/user/MyApp/images/
> >         /MyApp/images/users/    ..... /home/user/MyApp/images/users/
> >
> >
> > And I want to obtain from code executed in
> >
> >         http:/MyApp/config/upload.psp
> >
> > the physical path of root of the application:
> >
> >
> >
> > so I can't sent to a function in form.py:
> >
> >         app_root = ???    # function that returns '/home/user/MyApp/'
> >         imgs_folder = 'images/users/'
> >
> >         process(form, directory_to_save = app_root+imgs_folder)
> >
> >
> > So, the code is independent of the installation directory and the app
> > path.
> >
> > Another thing ... if images would be an alias, ¿ could I know the path ?
> >
> >
> >
> >
> > Thank You again.
> >
> >         S.R.Parda
> >
> > _______________________________________________
> > Mod_python mailing list
> > Mod_python at modpython.org
> > http://mailman.modpython.org/mailman/listinfo/mod_python
> >

More information about the Mod_python mailing list