From: Steven Githens <sgithens@ca...> - 2007-10-29 16:45:21
This might be a goofy usecase.
I'm working on a j2ee/Spring project and we have a Content Repository
integrated with it (a legacy implementation moving to Apache Jackrabbit
or any other JCR-170/283 impl ).
I have an Ajax command line interface to the system which can run
scripts written in things like Jython, Groovy, Beanshell, etc from the
Content Repository. This is nice because the repository is cluster safe
and I can script/debug the system editing my scripts over WebDAV or a
simple HTML based text editor.
I am writing most of my system admin commands in Jython, but find that I
know want the ability to import other modules.
This means that 'import MyModule' would have to look for real physical
files in the python path AND in the folder(s) in my Content Repository.
I'm wondering where the best place to extend Jython is for something
like this. Basically I need to add functionality to python's 'import'.
Is there a standard way to do this? Any suggestions on the best and
cleanest way to proceed would be much appreciated.
Thanks! Especially for all the hardwork on the releases lately!.
From: Charlie Groves <charlie.groves@gm...> - 2007-11-12 03:17:03
On Oct 29, 2007 8:44 AM, Steven Githens <sgithens@...> wrote:
> I'm wondering where the best place to extend Jython is for something
> like this. Basically I need to add functionality to python's 'import'.
Jython picked up an implementation of PEP 302 as part of the 2.2
release, and using that is the best way to implement something like
this. PEP 302 allows you to write hooks into the import system that
get called for every item in sys.path. The hook returns an importer
object if it can handle importing from that sys.path entry, and if it
does, that importer is called with the actual module name that's being
imported. You'd just write a path hook to key of a particular
sys.path entry for your content repository, and an importer to handle
the actual loading of stuff from the repository.
Java importation is handled with a hook like this in Jython.
org.python.core.JavaImporter is a path hook and importer in one: it
keys off the '__classpath__' entry in sys.path, and when it gets that,
returns itself as an importer that looks up Java classes on the
classpath and hooks them into the Jython runtime. You can implement
the importer and hook in a single class as in JavaImporter, or in
separate classes. In either case, I think JavaImporter should be a
good starting point.
Feel free to ask further questions here, and if you do go down this
path, it'd be great if you wrote up what you did on the wiki. I'm
positive this isn't an isolated usecase.