Graham Dumpleton
grahamd at dscpl.com.au
Mon May 30 19:28:53 EDT 2005
On 31/05/2005, at 6:51 AM, Nicolas Lehuen wrote: > Hi Lee, > > Unfortunately the answer is not as simple as it seems. > > Try doing this in xmlhandler.py : > > from mod_python import apache > > from os.path import dirname > this_directory = dirname(__file__) > site_setup = apache.import_module("site_setup",path=[this_directory]) > > check = str(dir()) > > def handler(req): > ... > req.write(req.interpreter + '<br />') > req.write(check) > ... > return apache.OK > > We use apache.import_module to import the site_setup module, looking Just to clarify Nicolas' answer a bit. Although PythonImport imports a module at startup, it is still a distinct module and thus why you don't see it within the globals of your handler. Even when you use apache.import_module() as above, you still need to refer to the module to get the data. Thus, to see your data, should change: check = str(dir()) to: check = str(dir(site_setup)) and reference data as: site_setup.magic_string as appropriate. In respect of Nicolas' comment in his other email, ie., > Plus, if > you have many site_setup.py in different directories, they will > collide with each other. True, at least where we are talking about the same interpreter. As you are talking about different interpreters for each virtual host, you could have more than one. You would though need to ensure that the files were in different directories and each interpreter set the Python path to only the one directory appropriate to that interpreter. If your intent is for this module only to be used for configuration, as Nicolas pointed out, you may want to have a look at Vampire, which includes a configuration mechanism based on use of Python ConfigParser module. In your case with multiple sites, you appear to be reserving a "config" directory for common stuff in which case with Vampire all you would need to do is create a file called something like "site_setup.cf" in the "config" directory. It may for example contain: [Settings] magic_string = Ooggaa-Booggaa! In your handler you could then say: from mod_python import apache import vampire def handler(req): config = vampire.loadConfig(req,"config/site_setup.cf") magic_string = config.get("Settings","magic_string") req.content_type = "text/plain" req.send_http_header() req.write("magic_string = %s"%magic_string) return apache.OK This will work whether or not you actually use Vampire for its mechanism for dispatching to different handlers based on resource name. Ie., it should work with a standard handler of your own creation. Further, this handler could be in subdirectory, ie., not in the root directory where "config" exists, as the configuration mechanism will search back up the hierarchy to the root of where PythonHandler is defined (as necessary), looking for "config/site_setup.cf". This means you do not need to hard code any paths to anything in either the Apache configuration or your handlers. Because the configuration file is looked up when required, you can change it whenever you want and do not need to restart Apache for changes to be reflected. There is a little bit of information on the configuration mechanism in Vampire in the overview for it: http://www.dscpl.com.au/projects/vampire/articles/vampire -001.html#configuration-mechanism Hope this is of interest. Graham
|