use of org.python.pydev.plugin.nature.SystemPythonNature in project Pydev by fabioz.
the class AdditionalProjectInterpreterInfo method getAdditionalInfoAndNature.
public static List<Tuple<AbstractAdditionalTokensInfo, IPythonNature>> getAdditionalInfoAndNature(IPythonNature nature, boolean addSystemInfo, boolean addReferencingProjects, boolean addReferencedProjects) throws MisconfigurationException {
List<Tuple<AbstractAdditionalTokensInfo, IPythonNature>> ret = new ArrayList<Tuple<AbstractAdditionalTokensInfo, IPythonNature>>();
IProject project = nature.getProject();
// get for the system info
if (addSystemInfo) {
AbstractAdditionalTokensInfo systemInfo;
try {
systemInfo = AdditionalSystemInterpreterInfo.getAdditionalSystemInfo(InterpreterManagersAPI.getInterpreterManager(nature), nature.getProjectInterpreter().getExecutableOrJar());
} catch (MisconfigurationException e) {
throw e;
} catch (PythonNatureWithoutProjectException e) {
throw new RuntimeException(e);
}
ret.add(new Tuple<AbstractAdditionalTokensInfo, IPythonNature>(systemInfo, new SystemPythonNature(nature.getRelatedInterpreterManager())));
}
// get for the current project
if (project != null) {
AbstractAdditionalTokensInfo additionalInfoForProject = getAdditionalInfoForProject(nature);
if (additionalInfoForProject != null) {
ret.add(new Tuple<AbstractAdditionalTokensInfo, IPythonNature>(additionalInfoForProject, nature));
}
try {
if (addReferencedProjects) {
// get for the referenced projects
Set<IProject> referencedProjects = ProjectModulesManager.getReferencedProjects(project);
for (IProject refProject : referencedProjects) {
additionalInfoForProject = getAdditionalInfoForProject(PythonNature.getPythonNature(refProject));
if (additionalInfoForProject != null) {
ret.add(new Tuple<AbstractAdditionalTokensInfo, IPythonNature>(additionalInfoForProject, PythonNature.getPythonNature(refProject)));
}
}
}
if (addReferencingProjects) {
Set<IProject> referencingProjects = ProjectModulesManager.getReferencingProjects(project);
for (IProject refProject : referencingProjects) {
additionalInfoForProject = getAdditionalInfoForProject(PythonNature.getPythonNature(refProject));
if (additionalInfoForProject != null) {
ret.add(new Tuple<AbstractAdditionalTokensInfo, IPythonNature>(additionalInfoForProject, PythonNature.getPythonNature(refProject)));
}
}
}
} catch (Exception e) {
Log.log(e);
}
}
return ret;
}
use of org.python.pydev.plugin.nature.SystemPythonNature in project Pydev by fabioz.
the class InterpreterInfo method obtainUserSitePackages.
@Override
public String obtainUserSitePackages(IInterpreterManager interpreterManager) {
if (userSitePackages == null) {
SimpleRunner simpleRunner = new SimpleRunner();
Tuple<String, String> output = simpleRunner.runAndGetOutput(new String[] { executableOrJar, "-m", "site", PlatformUtils.isWindowsPlatform() ? "--user-site" : "--user-base" }, new File(executableOrJar).getParentFile(), new SystemPythonNature(interpreterManager, this), null, "utf-8");
userSitePackages = output.o1.trim();
}
return userSitePackages;
}
use of org.python.pydev.plugin.nature.SystemPythonNature in project Pydev by fabioz.
the class InterpreterManagersAPI method getInfoForFile.
/**
* @param file the file we want to get info on.
* @return a tuple with the nature to be used and the name of the module represented by the file in that scenario.
*/
public static Tuple<IPythonNature, String> getInfoForFile(File file) {
IInterpreterManager pythonInterpreterManager2 = getPythonInterpreterManager(false);
IInterpreterManager jythonInterpreterManager2 = getJythonInterpreterManager(false);
IInterpreterManager ironpythonInterpreterManager2 = getIronpythonInterpreterManager(false);
if (file != null) {
// Check if we can resolve the manager for the passed file...
Tuple<IPythonNature, String> infoForManager = getInfoForManager(file, pythonInterpreterManager2);
if (infoForManager != null) {
return infoForManager;
}
infoForManager = getInfoForManager(file, jythonInterpreterManager2);
if (infoForManager != null) {
return infoForManager;
}
infoForManager = getInfoForManager(file, ironpythonInterpreterManager2);
if (infoForManager != null) {
return infoForManager;
}
// Ok, the file is not part of the interpreter configuration, but it's still possible that it's part of a
// project... (external projects), so, let's go on and see if there's some match there.
List<IPythonNature> allPythonNatures = PythonNature.getAllPythonNatures();
int size = allPythonNatures.size();
for (int i = 0; i < size; i++) {
IPythonNature nature = allPythonNatures.get(i);
try {
// Note: only resolve in the project sources, as we've already checked the system and we'll be
// checking all projects anyways.
String modName = nature.resolveModuleOnlyInProjectSources(FileUtils.getFileAbsolutePath(file), true);
if (modName != null) {
return new Tuple<IPythonNature, String>(nature, modName);
}
} catch (Exception e) {
Log.log(e);
}
}
}
if (pythonInterpreterManager2.isConfigured()) {
try {
return new Tuple<IPythonNature, String>(new SystemPythonNature(pythonInterpreterManager2), getModNameFromFile(file));
} catch (MisconfigurationException e) {
}
}
if (jythonInterpreterManager2.isConfigured()) {
try {
return new Tuple<IPythonNature, String>(new SystemPythonNature(jythonInterpreterManager2), getModNameFromFile(file));
} catch (MisconfigurationException e) {
}
}
if (ironpythonInterpreterManager2.isConfigured()) {
try {
return new Tuple<IPythonNature, String>(new SystemPythonNature(ironpythonInterpreterManager2), getModNameFromFile(file));
} catch (MisconfigurationException e) {
}
}
// Ok, nothing worked, let's just do a call which'll ask to configure python and return null!
try {
pythonInterpreterManager2.getDefaultInterpreterInfo(true);
} catch (MisconfigurationException e) {
// Ignore
}
return null;
}
use of org.python.pydev.plugin.nature.SystemPythonNature in project Pydev by fabioz.
the class Pep8Runner method runWithPep8BaseScript.
/**
* @param fileContents the contents to be passed in the stdin.
* @param parameters the parameters to pass. Note that a '-' is always added to the parameters to signal we'll pass the file as the input in stdin.
* @param script i.e.: pycodestyle.py, autopep8.py
* @return null if there was some error, otherwise returns the process stdout output.
*/
public static String runWithPep8BaseScript(IDocument doc, String parameters, String script) {
File autopep8File;
try {
autopep8File = CorePlugin.getScriptWithinPySrc(new Path("third_party").append("pep8").append(script).toString());
} catch (CoreException e) {
Log.log("Unable to get " + script + " location.");
return null;
}
if (!autopep8File.exists()) {
Log.log("Specified location for " + script + " does not exist (" + autopep8File + ").");
return null;
}
SimplePythonRunner simplePythonRunner = new SimplePythonRunner();
IInterpreterManager pythonInterpreterManager = InterpreterManagersAPI.getPythonInterpreterManager();
IInterpreterInfo defaultInterpreterInfo;
try {
defaultInterpreterInfo = pythonInterpreterManager.getDefaultInterpreterInfo(false);
} catch (MisconfigurationException e) {
Log.log("No default Python interpreter configured to run " + script);
return null;
}
String[] parseArguments = ProcessUtils.parseArguments(parameters);
List<String> lst = new ArrayList<>(Arrays.asList(parseArguments));
lst.add("-");
String[] cmdarray = SimplePythonRunner.preparePythonCallParameters(defaultInterpreterInfo.getExecutableOrJar(), autopep8File.toString(), lst.toArray(new String[0]));
// Try to find the file's encoding, but if none is given or the specified encoding is
// unsupported, then just default to utf-8
String pythonFileEncoding = null;
try {
pythonFileEncoding = FileUtils.getPythonFileEncoding(doc, null);
if (pythonFileEncoding == null) {
pythonFileEncoding = "utf-8";
}
} catch (UnsupportedEncodingException e) {
pythonFileEncoding = "utf-8";
}
final String encodingUsed = pythonFileEncoding;
SystemPythonNature nature = new SystemPythonNature(pythonInterpreterManager, defaultInterpreterInfo);
ICallback<String[], String[]> updateEnv = new ICallback<String[], String[]>() {
@Override
public String[] call(String[] arg) {
if (arg == null) {
arg = new String[] { "PYTHONIOENCODING=" + encodingUsed };
} else {
arg = ProcessUtils.addOrReplaceEnvVar(arg, "PYTHONIOENCODING", encodingUsed);
}
return arg;
}
};
Tuple<Process, String> r = simplePythonRunner.run(cmdarray, autopep8File.getParentFile(), nature, new NullProgressMonitor(), updateEnv);
try {
r.o1.getOutputStream().write(doc.get().getBytes(pythonFileEncoding));
r.o1.getOutputStream().close();
} catch (IOException e) {
Log.log("Error writing contents to " + script);
return null;
}
Tuple<String, String> processOutput = SimplePythonRunner.getProcessOutput(r.o1, r.o2, new NullProgressMonitor(), pythonFileEncoding);
if (processOutput.o2.length() > 0) {
Log.log(processOutput.o2);
}
if (processOutput.o1.length() > 0) {
return processOutput.o1;
}
return null;
}
use of org.python.pydev.plugin.nature.SystemPythonNature in project Pydev by fabioz.
the class PythonCompletionProcessor method computeCompletionProposals.
/**
* This is the interface implemented to get the completions.
*
* @see org.eclipse.jface.text.contentassist.IContentAssistProcessor#computeCompletionProposals(org.eclipse.jface.text.ITextViewer, int)
*/
@Override
public ICompletionProposal[] computeCompletionProposals(ITextViewer viewer, int documentOffset) {
updateStatus();
ICompletionProposalHandle[] proposals;
try {
// FIRST: discover activation token and qualifier.
IDocument doc = viewer.getDocument();
// list for storing the proposals
TokensOrProposalsList pythonAndTemplateProposals = new TokensOrProposalsList();
IPythonNature nature = edit.getPythonNature();
if (nature == null) {
IInterpreterManager manager = ChooseInterpreterManager.chooseInterpreterManager();
if (manager != null) {
nature = new SystemPythonNature(manager);
} else {
CompletionError completionError = new CompletionError(new RuntimeException("No interpreter configured."));
this.error = completionError.getErrorMessage();
return new ICompletionProposal[] { completionError };
}
}
if (nature == null || !nature.startRequests()) {
return new ICompletionProposal[0];
}
try {
CompletionRequest request = new CompletionRequest(edit.getEditorFile(), nature, doc, documentOffset, codeCompletion, PyCodeCompletionPreferences.getUseSubstringMatchInCodeCompletion());
// Get code completion proposals
if (PyCodeCompletionPreferences.useCodeCompletion()) {
if (whatToShow == SHOW_ALL) {
try {
pythonAndTemplateProposals.addAll(getPythonProposals(documentOffset, doc, request));
} catch (Throwable e) {
Log.log(e);
CompletionError completionError = new CompletionError(e);
this.error = completionError.getErrorMessage();
// Make the error visible to the user!
return new ICompletionProposal[] { completionError };
}
}
}
String[] strs = PySelection.getActivationTokenAndQualifier(doc, documentOffset, false);
String activationToken = strs[0];
String qualifier = strs[1];
// THIRD: Get template proposals (if asked for)
if (request.showTemplates && (activationToken == null || activationToken.trim().length() == 0)) {
TokensOrProposalsList templateProposals = getTemplateProposals(viewer, documentOffset, activationToken, qualifier);
pythonAndTemplateProposals.addAll(templateProposals);
}
// to show the valid ones, we'll get the qualifier from the initial request
proposals = PyCodeCompletionUtils.onlyValid(pythonAndTemplateProposals, request.qualifier, request.isInCalltip, request.useSubstringMatchInCodeCompletion, nature.getProject());
// Note: sorting happens later on.
} finally {
nature.endRequests();
}
} catch (Exception e) {
Log.log(e);
CompletionError completionError = new CompletionError(e);
this.error = completionError.getErrorMessage();
// Make the error visible to the user!
return new ICompletionProposal[] { completionError };
}
doCycle();
return ConvertCompletionProposals.convertHandlesToProposals(proposals);
}
Aggregations