-
Notifications
You must be signed in to change notification settings - Fork 226
Expand file tree
/
Copy pathClasspathPyImporter.java
More file actions
249 lines (217 loc) · 7.59 KB
/
ClasspathPyImporter.java
File metadata and controls
249 lines (217 loc) · 7.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
/* Copyright (c) Jython Developers */
package org.python.core;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.Map;
import java.util.logging.Level;
import org.python.core.util.FileUtil;
import org.python.core.util.StringUtil;
import org.python.core.util.importer;
import org.python.expose.ExposedMethod;
import org.python.expose.ExposedNew;
import org.python.expose.ExposedType;
import org.python.util.Generic;
@Untraversable
@ExposedType(name="ClasspathPyImporter")
public class ClasspathPyImporter extends importer<String> {
public static final String PYCLASSPATH_PREFIX = "__pyclasspath__/";
public static final PyType TYPE = PyType.fromClass(ClasspathPyImporter.class);
public ClasspathPyImporter(PyType subType) {
super(subType);
}
public ClasspathPyImporter() {
super();
}
@ExposedNew
@ExposedMethod
final void ClasspathPyImporter___init__(PyObject[] args, String[] kwds) {
ArgParser ap = new ArgParser("__init__", args, kwds, new String[] {"path"});
String path = ap.getString(0);
if (path == null || !path.startsWith(PYCLASSPATH_PREFIX)) {
throw Py.ImportError("path isn't for classpath importer");
}
if (!path.endsWith("/")) {
path += "/";
}
this.path = path;
}
/**
* Return the contents of the jarred file at the specified path
* as bytes.
*
* @param path a String path name within the archive
* @return a String of data in binary mode (no CRLF)
*/
@Override
public String get_data(String path) {
return ClasspathPyImporter_get_data(path);
}
@ExposedMethod
final String ClasspathPyImporter_get_data(String path) {
// Strip any leading occurrence of the hook string
int len = PYCLASSPATH_PREFIX.length();
if (len < path.length() && path.startsWith(PYCLASSPATH_PREFIX)) {
path = path.substring(len);
}
// Bundle wraps the stream together with a close operation
try (Bundle bundle = makeBundle(path, makeEntry(path))) {
byte[] data = FileUtil.readBytes(bundle.inputStream);
return StringUtil.fromBytes(data);
} catch (IOException ioe) {
throw Py.IOError(ioe);
}
}
/**
* Return the source code for the module as a string (using
* newline characters for line endings)
*
* @param fullname the fully qualified name of the module
* @return a String of the module's source code or null
*/
public String get_source(String fullname) {
return ClasspathPyImporter_get_source(fullname);
}
@ExposedMethod
final String ClasspathPyImporter_get_source(String fullname) {
ModuleInfo moduleInfo = getModuleInfo(fullname);
if (moduleInfo == ModuleInfo.ERROR) {
return null;
} else if (moduleInfo == ModuleInfo.NOT_FOUND) {
throw Py.ImportError(String.format("can't find module '%s'", fullname));
} else {
// Turn the module name into a source file name
String path = makeFilename(fullname);
if (moduleInfo == ModuleInfo.PACKAGE) {
path += File.separator + "__init__.py";
} else {
path += ".py";
}
// Bundle wraps the stream together with a close operation
try (Bundle bundle = makeBundle(path, makeEntry(path))) {
InputStream is = bundle.inputStream;
if (is != null) {
byte[] data = FileUtil.readBytes(is);
return StringUtil.fromBytes(data);
} else {
// we have the module, but no source
return null;
}
} catch (IOException ioe) {
throw Py.IOError(ioe);
}
}
}
/**
* Find the module for the fully qualified name.
*
* @param fullname the fully qualified name of the module
* @param path if not installed on the meta-path None or a module path
* @return a loader instance if this importer can load the module, None
* otherwise
*/
@ExposedMethod(defaults = "null")
final PyObject ClasspathPyImporter_find_module(String fullname, String path) {
return importer_find_module(fullname, path);
}
/**
* Determine whether a module is a package.
*
* @param fullname the fully qualified name of the module
* @return whether the module is a package
*/
@ExposedMethod
final boolean ClasspathPyImporter_is_package(String fullname) {
return importer_is_package(fullname);
}
/**
* Return the code object associated with the module.
*
* @param fullname the fully qualified name of the module
* @return the module's PyCode object or None
*/
@ExposedMethod
final PyObject ClasspathPyImporter_get_code(String fullname) {
ModuleCodeData moduleCodeData = getModuleCode(fullname);
if (moduleCodeData != null) {
return moduleCodeData.code;
}
return Py.None;
}
/**
* Load a module for the fully qualified name.
*
* @param fullname the fully qualified name of the module
* @return a loaded PyModule
*/
@ExposedMethod
final PyObject ClasspathPyImporter_load_module(String fullname) {
return importer_load_module(fullname);
}
@Override
protected long getSourceMtime(String path) {
// Can't determine this easily
return -1;
}
@Override
protected Bundle makeBundle(String fullFilename, String entry) {
InputStream is = entries.remove(entry);
return new Bundle(is) {
@Override
public void close() {
try {
inputStream.close();
} catch (IOException e) {
throw Py.JavaError(e);
}
}
};
}
@Override
protected String makeEntry(String filename) {
// In some contexts, the resource string arrives as from os.path.join(*parts)
if (!getSeparator().equals(File.separator)) {
filename = filename.replace(File.separator, getSeparator());
}
if (entries.containsKey(filename)) {
return filename;
}
InputStream is;
if (Py.getSystemState().getClassLoader() != null) {
is = tryClassLoader(filename, Py.getSystemState().getClassLoader(), "sys");
} else {
is = tryClassLoader(filename, imp.getParentClassLoader(), "parent");
}
if (is != null) {
entries.put(filename, is);
return filename;
}
return null;
}
private InputStream tryClassLoader(String fullFilename, ClassLoader loader, String place) {
if (loader != null) {
logger.log(Level.FINE, "# trying {0} in {1} class loader",
new Object[] {fullFilename, place});
return loader.getResourceAsStream(fullFilename);
}
return null;
}
@Override
protected String makeFilename(String fullname) {
return path.replace(PYCLASSPATH_PREFIX, "") + fullname.replace('.', '/');
}
@Override
protected String makeFilePath(String fullname) {
return path + fullname.replace('.', '/');
}
@Override
protected String makePackagePath(String fullname) {
return path;
}
@Override
protected String getSeparator() {
return "/";
}
private Map<String, InputStream> entries = Generic.map();
private String path;
}