001: package org.python.core;
002:
003: /**
004: * An importer for classes pre-compiled with JythonC.
005: *
006: */
007: public class PrecompiledImporter extends PyObject {
008:
009: public PrecompiledImporter() {
010: super ();
011: }
012:
013: /**
014: * Find the module for the fully qualified name.
015: *
016: * @param name the fully qualified name of the module
017: * @return a loader instance if this importer can load the module, None
018: * otherwise
019: */
020: public PyObject find_module(String name) {
021: return find_module(name, Py.None);
022: }
023:
024: /**
025: * Find the module for the fully qualified name.
026: *
027: * @param name the fully qualified name of the module
028: * @param path if installed on the meta-path None or a module path
029: * @return a loader instance if this importer can load the module, None
030: * otherwise
031: */
032: public PyObject find_module(String name, PyObject path) {
033: if (Py.frozenModules != null) {
034: // System.out.println("precomp: "+name+", "+name);
035: Class c = null;
036: if (Py.frozenModules.get(name + ".__init__") != null) {
037: // System.err.println("trying: "+name+".__init__$_PyInner");
038: Py.writeDebug("import", "trying " + name
039: + " as precompiled package");
040: c = findPyClass(name + ".__init__");
041: if (c == null) {
042: return Py.None;
043: }
044: // System.err.println("found: "+name+".__init__$_PyInner");
045: return new PrecompiledLoader(c, true);
046: } else if (Py.frozenModules.get(name) != null) {
047: Py.writeDebug("import", "trying " + name
048: + " as precompiled module");
049: c = findPyClass(name);
050: if (c == null) {
051: return Py.None;
052: }
053: return new PrecompiledLoader(c, false);
054: }
055: }
056: return Py.None;
057: }
058:
059: /**
060: * Returns a string representation of the object.
061: *
062: * @return a string representation of the object.
063: */
064: public String toString() {
065: return this .getType().toString();
066: }
067:
068: public class PrecompiledLoader extends PyObject {
069:
070: private Class _class;
071:
072: private boolean _package;
073:
074: public PrecompiledLoader(Class class_, boolean package_) {
075: this ._class = class_;
076: this ._package = package_;
077: }
078:
079: public PyObject load_module(String name) {
080: if (this ._package) {
081: PyModule m = imp.addModule(name);
082: m.__dict__.__setitem__("__path__", new PyList());
083: m.__dict__.__setitem__("__loader__", this );
084: }
085: Py.writeComment("import", "'" + name + "' as precompiled "
086: + (this ._package ? "package" : "module"));
087: return imp.createFromClass(name, this ._class);
088: }
089:
090: /**
091: * Returns a string representation of the object.
092: *
093: * @return a string representation of the object.
094: */
095: public String toString() {
096: return this .getType().toString();
097: }
098: }
099:
100: private Class findPyClass(String name) {
101: if (Py.frozenPackage != null) {
102: name = Py.frozenPackage + "." + name;
103: }
104: return Py.findClassEx(name + "$_PyInner", "precompiled");
105: }
106: }
|