KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > python > core > PrecompiledImporter


1 package org.python.core;
2
3 /**
4  * An importer for classes pre-compiled with JythonC.
5  *
6  */

7 public class PrecompiledImporter extends PyObject {
8
9     public PrecompiledImporter() {
10         super();
11     }
12
13     /**
14      * Find the module for the fully qualified name.
15      * @param name the fully qualified name of the module
16      * @return a loader instance if this importer can load the module, None otherwise
17      */

18     public PyObject find_module(String JavaDoc name) {
19         return find_module(name, Py.None);
20     }
21
22     /**
23      * Find the module for the fully qualified name.
24      * @param name the fully qualified name of the module
25      * @param path if installed on the meta-path None or a module path
26      * @return a loader instance if this importer can load the module, None otherwise
27      */

28     public PyObject find_module(String JavaDoc name, PyObject path) {
29         if (Py.frozenModules != null) {
30             //System.out.println("precomp: "+name+", "+name);
31
Class JavaDoc c = null;
32             if (Py.frozenModules.get(name+".__init__") != null) {
33                 //System.err.println("trying: "+name+".__init__$_PyInner");
34
Py.writeDebug("import", "trying " + name + " as precompiled package");
35                 c = findPyClass(name+".__init__");
36                 if (c == null) {
37                     return Py.None;
38                 }
39                 //System.err.println("found: "+name+".__init__$_PyInner");
40
return new PrecompiledLoader(c, true);
41             } else if (Py.frozenModules.get(name) != null) {
42                 Py.writeDebug("import", "trying " + name + " as precompiled module");
43                 c = findPyClass(name);
44                 if (c == null) {
45                     return Py.None;
46                 }
47                 return new PrecompiledLoader(c, false);
48             }
49         }
50         return Py.None;
51     }
52
53     /**
54      * Returns a string representation of the object.
55      *
56      * @return a string representation of the object.
57      */

58     public String JavaDoc toString() {
59         return this.getType().toString();
60     }
61
62     public class PrecompiledLoader extends PyObject {
63
64         private Class JavaDoc _class;
65         private boolean _package;
66
67         public PrecompiledLoader(Class JavaDoc class_, boolean package_) {
68             this._class = class_;
69             this._package = package_;
70         }
71
72         public PyObject load_module(String JavaDoc name) {
73             if(this._package) {
74                 PyModule m = imp.addModule(name);
75                 m.__dict__.__setitem__("__path__", new PyList());
76                 m.__dict__.__setitem__("__loader__", this);
77             }
78             Py.writeComment("import", "'" + name + "' as precompiled "
79               + (this._package ? "package" : "module"));
80             return imp.createFromClass(name, this._class);
81         }
82
83         /**
84          * Returns a string representation of the object.
85          *
86          * @return a string representation of the object.
87          */

88         public String JavaDoc toString() {
89             return this.getType().toString();
90         }
91     }
92
93     private Class JavaDoc findPyClass(String JavaDoc name) {
94         if (Py.frozenPackage != null) {
95             name = Py.frozenPackage+"."+name;
96         }
97         return Py.findClassEx(name+"$_PyInner", "precompiled");
98     }
99 }
100
Popular Tags