Import hooks available?

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Import hooks available?

Diez B. Roggisch
Hi all,

I'm on my way rewriting a so-far jythonc-dependend class that runs inside a
J2EE container. I want to get rid of the compilation step, as it complicates
our build-process.

But becauso of the J2EE-container environment, I don't have a directory
containing my .py-files for easy import.

So I was thinking of using getResourceAsStream from Java classloaders via an
import-hook mechanism.

Unfortunately, I'm not sure if such a thing really exists. Googling didn't
reveal anything enlightening, so maybe someone here can help me out?

Diez

-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your
opinions on IT & business topics through brief surveys-and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV
_______________________________________________
Jython-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/jython-users
Reply | Threaded
Open this post in threaded view
|

Re: Import hooks available?

Leo User
Hmmm, I faced a similiar situation this weekend: I had
to run jython with a naked jython environment.  I did
this,
Created a special importer:
   public static class JSLoader extends PyObject{
        Map<String, PyObject> loadedmods = new
HashMap<String, PyObject>();
        public PyObject find_module(PyObject[] args){
            PyObject rv = load_module(args[0]);
            if(rv != null && rv != Py.None){
                loadedmods.put(args[0].toString(),
rv);
                return this;
            }
            return Py.None;
        }
       
        public PyObject load_module(PyObject modname){
           
            String modstring = modname.toString();
            if(modstring.endsWith(".class"))
                return Py.None;
            if(modstring.startsWith("./"))
                modstring = modstring.substring(2);
            if(loadedmods.containsKey(modstring))
                return loadedmods.get(modstring);
            try{
                ClassLoader cl =
this.getClass().getClassLoader();
                String resource = "Lib/" +
modstring.toString() + ".py";
                java.net.URL url =
cl.getResource(resource);
                if(url != null){
                    return
imp.createFromSource(modname.toString(),
                                           
url.openStream(),
                                           
url.toString());        
                }
            } catch(java.io.IOException io){}
            return Py.None;
        }
    }

After the PySystemState was initialised I added the
importer to the metapath:
Py.getSystemState().meta_path.append(importer);

-------------
from doing these 2 things I was able to stick all the
code in one jar without any reference to an outside
jython resource and have it run.

leouser




--- "Diez B. Roggisch" <[hidden email]> wrote:

> Hi all,
>
> I'm on my way rewriting a so-far jythonc-dependend
> class that runs inside a
> J2EE container. I want to get rid of the compilation
> step, as it complicates
> our build-process.
>
> But becauso of the J2EE-container environment, I
> don't have a directory
> containing my .py-files for easy import.
>
> So I was thinking of using getResourceAsStream from
> Java classloaders via an
> import-hook mechanism.
>
> Unfortunately, I'm not sure if such a thing really
> exists. Googling didn't
> reveal anything enlightening, so maybe someone here
> can help me out?
>
> Diez
>
>
-------------------------------------------------------------------------
> Take Surveys. Earn Cash. Influence the Future of IT
> Join SourceForge.net's Techsay panel and you'll get
> the chance to share your
> opinions on IT & business topics through brief
> surveys-and earn cash
>
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV
> _______________________________________________
> Jython-users mailing list
> [hidden email]
>
https://lists.sourceforge.net/lists/listinfo/jython-users
>



 
____________________________________________________________________________________
Don't pick lemons.
See all the new 2007 cars at Yahoo! Autos.
http://autos.yahoo.com/new_cars.html 

-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your
opinions on IT & business topics through brief surveys-and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV
_______________________________________________
Jython-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/jython-users
Reply | Threaded
Open this post in threaded view
|

Re: Import hooks available?

Travis B. Hartwell
In reply to this post by Diez B. Roggisch
Diez,
On Wed, Mar 21, 2007 at 03:54:32PM +0100, Diez B. Roggisch wrote:

> Hi all,
>
> I'm on my way rewriting a so-far jythonc-dependend class that runs inside a
> J2EE container. I want to get rid of the compilation step, as it complicates
> our build-process.
>
> But becauso of the J2EE-container environment, I don't have a directory
> containing my .py-files for easy import.
>
> So I was thinking of using getResourceAsStream from Java classloaders via an
> import-hook mechanism.
>
> Unfortunately, I'm not sure if such a thing really exists. Googling didn't
> reveal anything enlightening, so maybe someone here can help me out?

I wrote something very similar just recently here at work.  I'm
intending on writing a little how-to on how to do so.  I might take a
minute this afternoon and jot down a few notes on what I did and send it
to the list.

Sorry I don't have time to help at the moment, but I'll share later.

Travis
 

-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your
opinions on IT & business topics through brief surveys-and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV
_______________________________________________
Jython-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/jython-users
Reply | Threaded
Open this post in threaded view
|

Re: Import hooks available?

Leo User
I used the metapath but there is the path_hooks
variable as well in PySystemState.  From looking at
imp it might work as well.

leouser

--- "Travis B. Hartwell" <[hidden email]>
wrote:

> Diez,
> On Wed, Mar 21, 2007 at 03:54:32PM +0100, Diez B.
> Roggisch wrote:
> > Hi all,
> >
> > I'm on my way rewriting a so-far jythonc-dependend
> class that runs inside a
> > J2EE container. I want to get rid of the
> compilation step, as it complicates
> > our build-process.
> >
> > But becauso of the J2EE-container environment, I
> don't have a directory
> > containing my .py-files for easy import.
> >
> > So I was thinking of using getResourceAsStream
> from Java classloaders via an
> > import-hook mechanism.
> >
> > Unfortunately, I'm not sure if such a thing really
> exists. Googling didn't
> > reveal anything enlightening, so maybe someone
> here can help me out?
>
> I wrote something very similar just recently here at
> work.  I'm
> intending on writing a little how-to on how to do
> so.  I might take a
> minute this afternoon and jot down a few notes on
> what I did and send it
> to the list.
>
> Sorry I don't have time to help at the moment, but
> I'll share later.
>
> Travis
>  
>
>
-------------------------------------------------------------------------
> Take Surveys. Earn Cash. Influence the Future of IT
> Join SourceForge.net's Techsay panel and you'll get
> the chance to share your
> opinions on IT & business topics through brief
> surveys-and earn cash
>
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV
> _______________________________________________
> Jython-users mailing list
> [hidden email]
>
https://lists.sourceforge.net/lists/listinfo/jython-users
>



 
____________________________________________________________________________________
Food fight? Enjoy some healthy debate
in the Yahoo! Answers Food & Drink Q&A.
http://answers.yahoo.com/dir/?link=list&sid=396545367

-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your
opinions on IT & business topics through brief surveys-and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV
_______________________________________________
Jython-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/jython-users