2005/5/19, Graham Dumpleton <[EMAIL PROTECTED]>:
> Nicolas Lehuen (JIRA) wrote ..
> >      [ 
> > http://issues.apache.org/jira/browse/MODPYTHON-54?page=comments#action_65702
> > ]
> >
> > Nicolas Lehuen commented on MODPYTHON-54:
> > -----------------------------------------
> >
> > I have added two function to mod_python.publisher :
> >
> > def import_page(relative_path, auto_reload=True):
> >     """
> >         This imports a published page. The relative_path argument is a
> > path
> >         relative to the directory of the page where import_page() is called.
> >         Hence, if we have index.py and foobar.py in the same directory,
> > index.py
> >         can simply do something like :
> >
> >         import mod_python.publisher
> >         foobar = mod_python.publisher.import_page('foobar.py')
> >
> >         If auto_reload is True (the default), the returned object is not
> > really
> >         the module itself, but a placeholder object which allows the real
> > module
> >         to be automatically reloaded whenever its source file changes.
> >     """
> >
> > and
> >
> > def get_page(req, relative_path):
> >     """
> >         This imports a published page. The relative_path argument is a
> > path
> >         relative to the published page where the request is really handled
> > (not
> >         relative to the path given in the URL).
> >
> >         Warning : in order to maintain consistency in case of module 
> > reloading,
> >         do not store the resulting module in a place that outlives the
> > request
> >         duration.
> >     """
> >
> > Now we need a bit of documentation.
> 
> Don't understand why it has to be relative for import_page(). Should
> be able to specify any path. The code can do an os.path.isabs() call
> and if it is absolute use it as is and if relative only then consider
> trying to automagically turn it into an absolute path somehow.

I totally agree. I think the relative path feature is handy, since it
gives you a bit of independence wrt the place where the files are
physically stored. But we should not prevent people from giving an
absolute path, even if it should be discouraged. I'll add your
suggestion of using isabs() to the code.
 
> Overall I feel that these functions might need a bit more thought.
> 
> The handle around the module worries me a bit as it would appear to
> work in read mode only. Ie., not sure it would work if someone tried to
> actually set a global variable within the module from outside of it.
> The lack of __setattr__() means it will probably set the variable in the
> handle and not the cached module.

Yeah, I've thought about that, but setting an attribute to a module
which can be reloaded at any time does not seem a good idea to me. I
think the get_page() function (which return a module instance valid
for the duration of the request) is much more robust, since it reckons
that a published module is a volatile object.

There is another way which would not rely on wrapper objects : it is
simply to add the imported file to the dependencies of the importing
file, so that whenever the imported file changes, its page is rebuilt
and the importing page is also rebuilt. This is not really difficult
to do and could be cleaner and faster.

> There are other things about it which means it will fail as appearing
> to be a module. For example, dir() on the handle returns handle
> stuff and not module stuff. Granted that for most this might not make
> a difference but for a small number it might.
>
> I'll think some more about the code and see if I can come up with any
> suggestions.
> 
> BTW, do like the idea of looking back up the stack frame to find the
> context. Am not sure that it will work all the time, although cases
> that it will not probably work wouldn't come up when using publisher.
> I'll probably look at falling back to this stack frame peek for my
> Vampire importing system, so if I come up with additional checks
> required to handle those cases where it may not work, will let you
> know about that as well.
>
> Graham
> 

I've seen this pattern in the Python cookbook ;  you can also check
the code for traceback.extract_stack (which I was originally relying
upon, but dropped since I do not need all the linecache stuff).

Reply via email to