[mod_python] mod_python.Importer.path; set dynamically

Martijn Moeling martijn at xs4us.nu
Fri Feb 22 07:42:39 EST 2008


Graham,
 
Sorry for the delay I have been extremely busy on some other stuff.
 
>                 modules=filesInUserDirectory(req.session.user)
>            for module in modules:
>                         module.append( apache.import_module(module)
>                 return module.index(req,*args,**kwargs) # return apache.OK?
>    ......... (some code)
>         return apache.OK
>

As you can see the line "modules=filesInUserDirectory(req.session.user)" loads a tuple with modules in the userdirectory to import over the stable versions.
 
Then I have to iterate to see which ones I nee to import, please note that this is explanational code not actual code, I'm designing not implementing.
 
what does the line: "return module.index(req,*args,**kwargs) # return apache.OK?" do, and what if I return Apache.OK, I do not understand (or find explanation of the "return module.index(req,*args,**kwargs)" part.

It seems to me that in this way I cannot load more than one module and I should (re)load all modules in the userdir.....

 

Martijn


 

________________________________

Van: Graham Dumpleton [mailto:graham.dumpleton at gmail.com]
Verzonden: wo 13.02.2008 0:40
Aan: Martijn Moeling
CC: mod_python at modpython.org
Onderwerp: Re: [mod_python] mod_python.Importer.path; set dynamically



On 12/02/2008, Martijn Moeling <martijn at xs4us.nu> wrote:
> Graham,
>
> Thank you, I did not mess around with it yet, I am planning a
> versioning system to be added to my webbased development platform.
> I am still having some trouble with my webdav module, I have to taccle
> that one first.
>
> Your help is usefull one question though:
>
> say the inporterpath is set to:
>
> ".../python_modules/me:.../python_modules"
>
> in the apacheconfig the importerpath is set to .../python_modules
>
> in both directories stays:
>
> modulex.py ( in the me directory is the lates version)
>
>
> in my handler module:
>
> import modulex
>
> de handler(req):
>         ....... (some code)
>         if req.debug and req.session.loggedIn:
>                 req.options['mod_python.path'] = ".../me:../"
>                 importedModules=[]
>                 modules=filesInUserDirectory(req.session.user)
>            for module in modules:
>                         module.append( apache.import_module(module)
>                 return module.index(req,*args,**kwargs) # return apache.OK?
>    ......... (some code)
>         return apache.OK
>
> what happens to the interpreter for the next requests? which "modulex"
> is used ? The are supposed to get the "stable" one from .../
> python_modules/

Yes. Should always get the one from path specified in Apache
configuration. The dynamic change is only going to apply to those
imports done from handler or as a side effect of running the handler.

> I don't understand what the return line does, what happens if I return
> apache.OK in this case?

Huh.

At least I am a bit confused by your question as the code:

          for module in modules:
                       module.append( apache.import_module(module)
               return module.index(req,*args,**kwargs) # return apache.OK?
  ......... (some code)

doesn't make much sense to me. Don't understand why you are trying to
iterate over a list of modules to import.

Graham

> Martijn
>
> On Feb 5, 2008, at 11:12 AM, Graham Dumpleton wrote:
>
> > Sorry for the delay.
> >
> > To make things work transparently across a lot of modules, for both
> > import_module() and 'import' where possible, what you can do is modify
> > the value of option 'mod_python.importer.path' dynamically from code.
> > This can be done from memory using:
> >
> > req.options['mod_python.importer.path'] = "...."
> >
> > You obviously can query this first if you need to somehow manipulate
> > it to add stuff.
> >
> > All you then need is a level of indirection. Has to be from inside of
> > handler, can't be at global scope. So,
> > /var/www/python_modules/index.py might do:
> >
> > def index(req, *args, **kwargs):
> > req.options['mod_python.importer.path'] = \
> >  '/var/www/python_modules/%s:/var/www/python_modules' % req.user
> > module = apache.import_module('index-core')
> > return module.index(req, *args, **kwargs)
> >
> > If there is only one level of module imports, you could also do just:
> >
> > def index(req, *args, **kwargs):
> > module = apache.import_module('index-core', \
> >  path=['/var/www/python_modules/%s:/var/www/python_modules' %
> > req.user]))
> > return module.index(req, *args, **kwargs)
> >
> > but think that if subdirectory uses 'import', then it might not look
> > in all the directories. Can't remember how it works now.
> >
> > Anyway the suggestion is look at changing req.options if you haven't
> > already.
> >
> > Graham
> >
> > On 01/02/2008, Martijn Moeling <martijn at xs4us.nu> wrote:
> >>
> >>
> >>
> >> Graham,
> >>
> >> As you might remember I added a MySQLdb section to session.
> >> (to be able to save sessions across computers)
> >>
> >> In my system a lot of additional data is added to the req object,
> >> which is
> >> passed
> >> around. in my case it is req.Session.user.
> >>
> >> (I do not use http based auth, since this is in conflict with my
> >> web based
> >> desktop)
> >>
> >> nevertheless, i can change your "example code" to my needs, consider
> >> req.user as the distinguisher for ease of thinking.
> >>
> >> Martijn
> >>
> >>
> >>
> >>
> >> -----Original Message-----
> >> From: Graham Dumpleton [mailto:graham.dumpleton at gmail.com]
> >> Sent: Thu 31.01.2008 23:03
> >> To: Martijn Moeling
> >> Cc: mod_python at modpython.org
> >> Subject: Re: [mod_python] mod_python.Importer.path; set dynamically
> >>
> >> On 01/02/2008, Martijn Moeling <martijn at xs4us.nu> wrote:
> >>> Hi!
> >>>
> >>> I am an extensive mod_python user and my system needs something
> >>> special.
> >>>
> >>> Consider the following:
> >>>
> >>> in httpd.conf I have :
> >>>
> >>> PythonOption mod_python.importer.path ["/var/www/python_modules"]
> >>>
> >>> /var/www/html/index.py is linked to ../python_modules/index.py
> >>>
> >>> the python_modules directory has subdirectories, like; developer1,
> >>> developer2
> >>>
> >>> the python_modules contains stable modules and developerX holds
> >> development
> >>> versions.
> >>>
> >>> I have load balancing with multiple frond-ends (apache/MP) and one
> >> backend
> >>> (MySQL).
> >>>
> >>> I would like to dynamically change the mod_python.importer.path so
> >>> that
> >>> developer1 runs code in the corresponding subdir if the module
> >>> exists,
> >>> else fall back to the python_modules directory.
> >>>
> >>> Any tips, suggestions, remarks?
> >>
> >> What is the distinguisher used to know which developer it is? Are you
> >> using something like Basic authentication and so could distinguish
> >> based on req.user?
> >>
> >> If you tell me what the distinguisher is, probably have an idea.
> >>
> >> Graham
> >>
> >>
>
>


-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mm_cfg_has_not_been_edited_to_set_host_domains/pipermail/mod_python/attachments/20080222/e02452ae/attachment.html


More information about the Mod_python mailing list