use of org.python.pydev.ast.interpreter_managers.InterpreterInfo in project Pydev by fabioz.
the class AbstractIOTestCase method createModuleAdapterFromDataSource.
/**
* @param version IPythonNature.PYTHON_VERSION_XXX
*/
protected ModuleAdapter createModuleAdapterFromDataSource(String version) throws Throwable {
codeCompletionTestsBase.restorePythonPath(FileUtils.getFileAbsolutePath(data.file.getParentFile()), true);
PythonModuleManager pythonModuleManager = new PythonModuleManager(CodeCompletionTestsBase.nature);
if (version != null) {
// As the files will be found in the system, we need to set the system modules manager info.
IModulesManager modulesManager = pythonModuleManager.getIModuleManager();
SystemModulesManager systemModulesManager = (SystemModulesManager) modulesManager.getSystemModulesManager();
systemModulesManager.setInfo(new InterpreterInfo(version, "", new ArrayList<String>()));
CodeCompletionTestsBase.nature.setVersion(version, null);
}
ModuleAdapter module = VisitorFactory.createModuleAdapter(pythonModuleManager, data.file, new Document(data.source), CodeCompletionTestsBase.nature, CodeCompletionTestsBase.nature);
return module;
}
use of org.python.pydev.ast.interpreter_managers.InterpreterInfo in project Pydev by fabioz.
the class AbstractInterpreterEditor method getTreeLibsControl.
/**
* @param parent
* @return
*/
private Tree getTreeLibsControl(Composite parent) {
if (treeWithLibs == null) {
treeWithLibs = new Tree(parent, SWT.BORDER | SWT.MULTI);
treeWithLibs.setFont(parent.getFont());
treeWithLibs.addDisposeListener(new DisposeListener() {
@Override
public void widgetDisposed(DisposeEvent event) {
treeWithLibs = null;
}
});
EnabledTreeDragReorder.enableDrag(treeWithLibs, false, false, new ICallback<Object, Object>() {
@Override
public Object call(Object arg) {
if (treeWithInterpreters.getSelectionCount() == 1) {
TreeItem[] selection = treeWithInterpreters.getSelection();
String nameFromTreeItem = getNameFromTreeItem(selection[0]);
InterpreterInfo info = (InterpreterInfo) nameToInfo.get(nameFromTreeItem);
exeOrJarOfInterpretersToRestore.add(info.getExecutableOrJar());
info.libs.clear();
TreeItem[] items = treeWithLibs.getItems();
if (items.length == 1) {
TreeItem treeItem = items[0];
TreeItem[] items2 = treeItem.getItems();
for (TreeItem treeItem2 : items2) {
info.libs.add(treeItem2.getText(0));
}
}
}
return null;
}
});
}
return treeWithLibs;
}
use of org.python.pydev.ast.interpreter_managers.InterpreterInfo in project Pydev by fabioz.
the class AbstractInterpreterEditor method doFillIntoGrid.
/**
* @see org.eclipse.jface.preference.ListEditor#doFillIntoGrid(org.eclipse.swt.widgets.Composite, int)
*/
@Override
protected void doFillIntoGrid(Composite parent, int numColumns) {
super.doFillIntoGrid(parent, numColumns);
GridData gd = new GridData();
tabFolder = new TabFolder(parent, SWT.None);
gd = new GridData();
gd.horizontalAlignment = SWT.FILL;
gd.verticalAlignment = SWT.FILL;
gd.grabExcessVerticalSpace = true;
gd.horizontalSpan = numColumns;
tabFolder.setLayoutData(gd);
try {
if (this.getShowPackageTab()) {
packageTab.createPackageControlTab(tabFolder, exeOrJarOfInterpretersToRestore, interpreterManager);
}
} catch (Exception e1) {
// Not really expected, just new code, so, let's protect until it matures.
Log.log(e1);
}
createTreeLibsControlTab();
// ----------------------- FORCED BUILTINS
forcedBuiltins = new AbstractListWithNewRemoveControl(this) {
@Override
protected List<String> getStringsFromInfo(InterpreterInfo info) {
ArrayList<String> ret = new ArrayList<String>();
for (Iterator<String> iter = info.forcedLibsIterator(); iter.hasNext(); ) {
ret.add(iter.next());
}
return ret;
}
@Override
protected void removeSelectedFrominfo(InterpreterInfo info, String[] builtins) {
for (String builtin : builtins) {
info.removeForcedLib(builtin);
}
exeOrJarOfInterpretersWithBuiltinsChanged.add(info.getExecutableOrJar());
}
@Override
protected String getInput() {
IInputValidator validator = new IInputValidator() {
@Override
public String isValid(String newText) {
for (char c : newText.toCharArray()) {
if (!Character.isJavaIdentifierPart(c) && c != ' ' && c != ',' && c != '.') {
return "Can only accept valid python module names (char: '" + c + "' not accepted)";
}
}
return null;
}
};
InputDialog d = new InputDialog(getShell(), "Builtin to add", "Builtin to add (comma separated)", "", validator);
int retCode = d.open();
String builtins = null;
if (retCode == InputDialog.OK) {
builtins = d.getValue();
}
return builtins;
}
@Override
protected void addInputToInfo(InterpreterInfo info, String builtins) {
java.util.List<String> split = StringUtils.splitAndRemoveEmptyTrimmed(builtins, ',');
for (String string : split) {
String trimmed = string.trim();
if (trimmed.length() > 0) {
info.addForcedLib(trimmed);
}
}
exeOrJarOfInterpretersWithBuiltinsChanged.add(info.getExecutableOrJar());
}
};
forcedBuiltins.createTab("Forced Builtins", "Forced Builtins (check <a>Manual</a> for more info).");
// ----------------------- PREDEFINED COMPLETIONS
predefinedCompletions = new AbstractListWithNewRemoveControl(this) {
private Button addAPIBt;
@Override
protected List<String> getStringsFromInfo(InterpreterInfo info) {
return info.getPredefinedCompletionsPath();
}
@Override
protected void removeSelectedFrominfo(InterpreterInfo info, String[] items) {
for (String item : items) {
info.removePredefinedCompletionPath(item);
}
exeOrJarOfInterpretersWithPredefinedChanged.add(info.getExecutableOrJar());
}
@Override
protected String getInput() {
DirectoryDialog dialog = new DirectoryDialog(getShell());
dialog.setFilterPath(lastDirectoryDialogPath);
String filePath = dialog.open();
if (filePath != null) {
lastDirectoryDialogPath = filePath;
}
return filePath;
}
@Override
protected void addInputToInfo(InterpreterInfo info, String item) {
info.addPredefinedCompletionsPath(item);
exeOrJarOfInterpretersWithPredefinedChanged.add(info.getExecutableOrJar());
}
@Override
protected void createButtons(AbstractInterpreterEditor interpreterEditor) {
super.createButtons(interpreterEditor);
// $NON-NLS-1$
addAPIBt = interpreterEditor.createBt(box, "Add from QScintilla api file", this);
}
@Override
public void widgetDisposed(DisposeEvent event) {
super.widgetDisposed(event);
if (addAPIBt != null) {
addAPIBt.dispose();
addAPIBt = null;
}
}
@Override
public void widgetSelected(SelectionEvent event) {
super.widgetSelected(event);
Widget widget = event.widget;
if (widget == addAPIBt) {
addAPIBt();
}
}
private void addAPIBt() {
final AbstractInterpreterEditor interpreterEditor = this.container.get();
Assert.isNotNull(interpreterEditor);
final InterpreterInfo info = interpreterEditor.getSelectedInfo();
if (info != null) {
FileDialog dialog = new FileDialog(getShell(), SWT.PRIMARY_MODAL | SWT.MULTI);
dialog.setFilterExtensions(new String[] { "*.api" });
dialog.setText("Select .api file to be converted to .pypredef.");
dialog.setFilterPath(lastFileDialogPath);
final String filePath = dialog.open();
if (filePath != null) {
lastFileDialogPath = filePath;
File filePath1 = new File(filePath);
final String dir = filePath1.getParent();
IInputValidator validator = new IInputValidator() {
@Override
public String isValid(String newText) {
if (newText.length() == 0) {
return "Number not provided.";
}
try {
Integer.parseInt(newText);
} catch (NumberFormatException e) {
return "The string: " + newText + " is not a valid integer.";
}
return null;
}
};
final InputDialog d = new InputDialog(getShell(), "Number of tokens to consider module", "Please specify the number of tokens to consider a module from the .api file\n\n" + "i.e.: if there's a PyQt4.QtCore.QObject and PyQt4.QtCore is a module and QtObject " + "is the first class, the number of tokens to consider a module would be 2 (one for " + "PyQt4 and another for QtCore).", "", validator);
int retCode = d.open();
final ByteArrayOutputStream output = new ByteArrayOutputStream();
if (retCode == InputDialog.OK) {
ProgressMonitorDialog monitorDialog = new AsynchronousProgressMonitorDialog(getShell());
monitorDialog.setBlockOnOpen(false);
final Exception[] exception = new Exception[1];
try {
IRunnableWithProgress operation = new IRunnableWithProgress() {
@Override
public void run(final IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
monitor.beginTask("Restoring PYTHONPATH", IProgressMonitor.UNKNOWN);
IPythonInterpreter interpreter = JythonPlugin.newPythonInterpreter(false, false);
interpreter.setErr(output);
interpreter.setOut(output);
HashMap<String, Object> locals = new HashMap<String, Object>();
locals.put("api_file", filePath);
locals.put("parts_for_module", d.getValue());
locals.put("cancel_monitor", monitor);
try {
JythonPlugin.exec(locals, "convert_api_to_pypredef.py", interpreter);
} catch (Exception e) {
Log.log(e + "\n\n" + output.toString());
exception[0] = e;
}
monitor.done();
}
};
monitorDialog.run(true, true, operation);
} catch (Exception e) {
Log.log(e);
}
Exception e = exception[0];
String contents = output.toString();
if (e == null && contents.indexOf("SUCCESS") != -1) {
addInputToInfo(info, dir);
interpreterEditor.updateTree();
} else {
if (e != null) {
MessageDialog.openError(getShell(), "Error creating .pypredef files", e.getMessage() + "\n\n" + contents);
} else {
MessageDialog.openError(getShell(), "Error creating .pypredef files", contents);
}
}
}
}
}
}
};
predefinedCompletions.createTab("Predefined", "Predefined completions (check <a>Manual</a> for more info).");
createEnvironmentVariablesTab();
createStringSubstitutionTab();
}
use of org.python.pydev.ast.interpreter_managers.InterpreterInfo in project Pydev by fabioz.
the class RefactoringRenameTestBase method checkSize.
/**
* checks if the size of the system modules manager and the project moule manager are coherent
* (we must have more modules in the system than in the project)
*/
@Override
protected void checkSize() {
try {
IInterpreterManager iMan = getInterpreterManager();
InterpreterInfo info = (InterpreterInfo) iMan.getDefaultInterpreterInfo(false);
assertTrue(info.getModulesManager().getSize(true) > 0);
int size = ((ASTManager) natureRefactoring.getAstManager()).getSize();
assertTrue("Interpreter size:" + info.getModulesManager().getSize(true) + " should be smaller than project size:" + size + " " + "(because it contains system+project info)", info.getModulesManager().getSize(true) < size);
} catch (MisconfigurationException e) {
throw new RuntimeException(e);
}
}
use of org.python.pydev.ast.interpreter_managers.InterpreterInfo in project Pydev by fabioz.
the class CodeCompletionTestsBase method restoreSystemPythonPath.
/**
* @return whether is was actually restored (given the force parameter)
*/
protected boolean restoreSystemPythonPath(boolean force, String path) {
if (restoredSystem == null || restoredSystem != this.getClass() || force) {
// restore manager and cache
setInterpreterManager(path);
restoredSystem = this.getClass();
// get default and restore the pythonpath
InterpreterInfo info = getDefaultInterpreterInfo();
this.beforeRestore(info);
info.restoreCompiledLibs(getProgressMonitor());
if (ADD_MX_TO_FORCED_BUILTINS) {
info.addForcedLib("mx");
}
if (ADD_NUMPY_TO_FORCED_BUILTINS) {
info.addForcedLib("numpy");
}
// postconditions
afterRestorSystemPythonPath(info);
return true;
}
return false;
}
Aggregations