107 if (!userDirInternalPython.exists()) {
108 userDirInternalPython.mkdirs();
111 if (installInternalPython.exists()) {
113 FileUtils.copyDirectory(installInternalPython, userDirInternalPython);
114 }
catch (IOException ex) {
115 logger.log(Level.WARNING, MessageFormat.format(
"There was an error copying internal python modules from {0} to {1}.",
116 installInternalPython, userDirInternalPython), ex);
127 List<T> objects =
new ArrayList<>();
128 Set<File> pythonModuleDirs =
new HashSet<>();
129 PythonInterpreter interpreter =
null;
132 interpreter =
new PythonInterpreter();
133 }
catch (Exception ex) {
134 logger.log(Level.SEVERE,
"Failed to load python Intepreter. Cannot load python modules", ex);
149 for (File file : pythonModuleDirs) {
150 if (file.isDirectory()) {
152 for (File script : pythonScripts) {
153 try (Scanner fileScanner =
new Scanner(
new BufferedReader(
new FileReader(script)))) {
154 while (fileScanner.hasNextLine()) {
155 String line = fileScanner.nextLine();
156 if (line.startsWith(
"class ") && filter.
accept(line)) {
157 String className = line.substring(6, line.indexOf(
"("));
160 }
catch (Exception ex) {
161 logger.log(Level.SEVERE, String.format(
"Failed to load %s from %s", className, script.getAbsolutePath()), ex);
163 DialogDisplayer.getDefault().notify(
new NotifyDescriptor.Message(
164 NbBundle.getMessage(
JythonModuleLoader.class,
"JythonModuleLoader.errorMessages.failedToLoadModule", className, ex.toString()),
165 NotifyDescriptor.ERROR_MESSAGE));
169 }
catch (FileNotFoundException ex) {
170 logger.log(Level.SEVERE, String.format(
"Failed to open %s", script.getAbsolutePath()), ex);
171 DialogDisplayer.getDefault().notify(
new NotifyDescriptor.Message(
172 NbBundle.getMessage(
JythonModuleLoader.class,
"JythonModuleLoader.errorMessages.failedToOpenModule", script.getAbsolutePath()),
173 NotifyDescriptor.ERROR_MESSAGE));
179 Collections.sort(objects, Comparator.comparing((T obj) -> obj.getClass().getSimpleName(), (s1, s2) -> s1.compareToIgnoreCase(s2)));
183 private static <T> T
createObjectFromScript(PythonInterpreter interpreter, File script, String className, Class<T> interfaceClass) {
187 interpreter.exec(
"import sys");
188 String path = Matcher.quoteReplacement(script.getParent());
189 interpreter.exec(
"sys.path.append('" + path +
"')");
190 String moduleName = script.getName().replaceAll(
"\\.py$",
"");
193 interpreter.exec(
"import " + moduleName);
194 interpreter.exec(
"reload(" + moduleName +
")");
197 interpreter.exec(
"from " + moduleName +
" import " + className);
198 interpreter.exec(
"obj = " + className +
"()");
200 T obj = interpreter.get(
"obj", interfaceClass);
204 interpreter.exec(
"sys.path.remove('" + path +
"')");