1 2 package org.python.modules; 3 4 import org.python.core.Py; 5 import org.python.core.PyFile; 6 import org.python.core.PyList; 7 import org.python.core.PyModule; 8 import org.python.core.PyObject; 9 import org.python.core.PyString; 10 import org.python.core.PyTuple; 11 import org.python.core.PyInteger; 12 13 import java.io.File ; 14 import java.io.FileInputStream ; 15 import java.io.IOException ; 16 import java.io.InputStream ; 17 18 24 25 public class imp { 26 public static PyString __doc__ = new PyString( 27 "This module provides the components needed to build your own\n"+ 28 "__import__ function. Undocumented functions are obsolete.\n" 29 ); 30 31 public static final int PY_SOURCE = 1; 32 public static final int PY_COMPILED = 2; 33 public static final int PKG_DIRECTORY = 5; 34 public static final int PY_FROZEN = 7; 35 public static final int IMP_HOOK = 9; 36 37 private static class ModuleInfo { 38 PyObject file; 39 String filename; 40 String suffix; 41 String mode; 42 int type; 43 ModuleInfo(PyObject file, String filename, String suffix, String mode, int type) { 44 this.file = file; 45 this.filename = filename; 46 this.suffix = suffix; 47 this.mode = mode; 48 this.type = type; 49 } 50 } 51 52 private static PyObject newFile(File file) { 53 try { 54 return new PyFile(new FileInputStream (file)); 55 } catch (IOException ioe) { 56 throw Py.IOError(ioe); 57 } 58 } 59 60 private static boolean caseok(File file, String filename, int namelen) { 61 return org.python.core.imp.caseok(file, filename, namelen); 62 } 63 64 72 static ModuleInfo findFromSource(String name, PyObject entry, boolean findingPackage) { 73 int nlen = name.length(); 74 String sourceName = "__init__.py"; 75 String compiledName = "__init__$py.class"; 76 String directoryName = entry.toString(); 77 78 if (directoryName.length() == 0) { 83 directoryName = null; 84 } 85 86 File dir = findingPackage ? new File(directoryName) : new File(directoryName, name); 88 File sourceFile = new File(dir, sourceName); 89 File compiledFile = new File(dir, compiledName); 90 91 boolean pkg = (dir.isDirectory() && caseok(dir, name, nlen) 92 && (sourceFile.isFile() || compiledFile.isFile())); 93 94 if(!findingPackage) { 95 if(pkg) { 96 return new ModuleInfo(Py.None, dir.getPath(), "", "", PKG_DIRECTORY); 97 } else { 98 Py.writeDebug("import", "trying source " + dir.getPath()); 99 sourceName = name + ".py"; 100 compiledName = name + "$py.class"; 101 sourceFile = new File(directoryName, sourceName); 102 compiledFile = new File(directoryName, compiledName); 103 } 104 } 105 106 if (sourceFile.isFile() && caseok(sourceFile, sourceName, nlen)) { 107 if (compiledFile.isFile() && caseok(compiledFile, compiledName, nlen)) { 108 Py.writeDebug("import", "trying precompiled " + compiledFile.getPath()); 109 long pyTime = sourceFile.lastModified(); 110 long classTime = compiledFile.lastModified(); 111 if (classTime >= pyTime) { 112 return new ModuleInfo(newFile(compiledFile), 113 compiledFile.getPath(), ".class", "rb", PY_COMPILED); 114 } 115 } 116 return new ModuleInfo(newFile(sourceFile), 117 sourceFile.getPath(), ".py", "r", PY_SOURCE); 118 } 119 120 Py.writeDebug("import", "trying " + compiledFile.getPath()); 122 if (compiledFile.isFile() && caseok(compiledFile, compiledName, nlen)) { 123 return new ModuleInfo(newFile(compiledFile), 124 compiledFile.getPath(), ".class", "rb", PY_COMPILED); 125 } 126 return null; 127 } 128 129 public static PyObject find_module(String name) { 130 return find_module(name, null); 131 } 132 133 public static PyObject find_module(String name, PyObject path) { 134 if (path == null || path == Py.None) { 135 path = Py.getSystemState().path; 136 } 137 138 PyObject iter = path.__iter__(); 139 for (PyObject p = null; (p = iter.__iternext__()) != null; ) { 140 ModuleInfo mi = findFromSource(name, p, false); 141 if(mi == null) { 142 continue; 143 } 144 return new PyTuple(new PyObject[] { 145 mi.file, 146 new PyString(mi.filename), 147 new PyTuple(new PyObject[] { 148 new PyString(mi.suffix), 149 new PyString(mi.mode), 150 Py.newInteger(mi.type) 151 }), 152 }); 153 } 154 throw Py.ImportError("No module named " + name); 155 } 156 157 public static PyObject load_module(String name, PyObject file, PyObject filename, PyTuple data) { 158 PyObject mod = Py.None; 159 int type = ((PyInteger)data.__getitem__(2).__int__()).getValue(); 160 while(mod == Py.None) { 161 Object o = file.__tojava__(InputStream .class); 162 if (o == Py.NoConversion) { 163 throw Py.TypeError("must be a file-like object"); 164 } 165 switch (type) { 166 case PY_SOURCE: 167 mod = org.python.core.imp.loadFromSource( 168 name.intern(), (InputStream )o, filename.toString()); 169 break; 170 case PY_COMPILED: 171 mod = org.python.core.imp.loadFromCompiled( 172 name.intern(), (InputStream )o, filename.toString()); 173 break; 174 case PKG_DIRECTORY: 175 PyModule m = org.python.core.imp.addModule(name); 176 m.__dict__.__setitem__("__path__", 177 new PyList(new PyObject[] { filename })); 178 m.__dict__.__setitem__("__file__", filename); 179 ModuleInfo mi = findFromSource(name, filename, true); 180 type = mi.type; 181 file = mi.file; 182 filename = new PyString(mi.filename); 183 break; 184 default: 185 throw Py.ImportError("No module named " + name); 186 } 187 } 188 PyObject modules = Py.getSystemState().modules; 189 modules.__setitem__(name.intern(), mod); 190 return mod; 191 } 192 193 public static PyObject get_suffixes() { 194 return new PyList(new PyObject[] { 195 new PyTuple(new PyObject[] { 196 new PyString(".py"), 197 new PyString("r"), 198 Py.newInteger(PY_SOURCE), 199 }), 200 new PyTuple(new PyObject[] { 201 new PyString(".class"), 202 new PyString("rb"), 203 Py.newInteger(PY_COMPILED), 204 }), 205 }); 206 } 207 208 public static PyModule new_module(String name) { 209 return new PyModule(name, null); 210 } 211 } 212 | Popular Tags |