[mod_python] Physical paths ?

Graham Dumpleton graham.dumpleton at gmail.com
Tue Feb 5 15:00:01 EST 2008


On 06/02/2008, S.R.Pardá <linux at qbox.es> wrote:
>
> El mar, 05-02-2008 a las 21:20 +1100, Graham Dumpleton escribió:
> > Sorry for the delay.
> >
>
> Not at all.
>
> > Since you are using .psp files, then you must be using AddHandler. As
> > a result the following should work to give you the name of the
> > directory corresponding to the Directory container in Apache
> > configuration, or .htaccess file, that you defined PythonHandler in.
> >
> >         root = req.hlist.directory
> >         parent = req.hlist.parent
> >         while parent is not None:
> >             root = parent.directory
> >             parent = parent.parent
> >
>
>
> Thank you,
>
> That's a good answer to the question of the Pyshical Root Path of an
> Application, because my app is an Apache Directory.
>
> In my case, parent is None. Because not stacked handlers ¿isn't it?.
> So req.hlist.directory has the root.
> But I understand I should use the complete code.

Not stacked handlers, but handlers added with req.add_handler().

BTW, you can probably also just use:

  apache.get_handler_root()

It works of a cached copy of that calculation from memory.

Graham

> > In other words, if in main Apache configuration you have:
> >
> >   Alias /MyApp/ /home/user/MyApp/
> >
> >   <Directory /home/user/MyApp>
> >   AddHandler mod_python .psp
> >   PythonHandler mod_python.psp | .psp
> >   ...
> >   </Directory>
> >
> > The 'root' should end up being '/home/user/MyApp'.
> >
> > This only works where PythonHandler in a Directory container or .htaccess file.
> >
> > You must be using mod_python 3.3.1.
> >
> > Graham
> >
> > On 03/02/2008, Graham Dumpleton <graham.dumpleton at gmail.com> wrote:
> > > 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.
> > >
> > > Do:
> > >
> > >   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.
> > >
> > > Graham
> > >
> > > ---------- 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
> > > exist.
> > > 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:
> > >         http://SERVER/MyApp/images/users/USER_FILE
> > > (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:
> > >         '/home/user/MyApp/config/index.psp'
> > >
> > > 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
> > > http://SERVER/MyApp/config/upload.psp
> > > that is:
> > > /home/user/MyApp/config/upload.psp
> > >
> > > 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
> > > course).
> > >
> > > ######
> > > 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):]
> > >   else:
> > >      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/')
> > > or
> > >         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,
> > >
> > >
> > >         S.R.Pardá
> > >
> > >
> > >
> > > 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