use of org.python.pydev.core.IPythonNature 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.core.IPythonNature in project Pydev by fabioz.
the class Flake8Analysis method createFlake8Process.
/**
* Creates the flake8 process and starts getting its output.
*/
void createFlake8Process(IExternalCodeAnalysisStream out) throws CoreException, MisconfigurationException, PythonNatureWithoutProjectException {
String flake8Executable = FileUtils.getFileAbsolutePath(flake8Location);
String target = location.toOSString();
ArrayList<String> cmdList = new ArrayList<String>();
cmdList.add(flake8Executable);
String userArgs = StringUtils.replaceNewLines(Flake8Preferences.getFlake8Args(resource), " ");
List<String> userArgsAsList = new ArrayList<>(Arrays.asList(ProcessUtils.parseArguments(userArgs)));
// run flake8 in project location
IProject project = resource.getProject();
if (project == null || !project.isAccessible()) {
// If the project is no longer valid, we can't do much.
Log.log("Unable to run flake8 in: " + target + ". Project not available (" + project + ").");
return;
}
File workingDir = project.getLocation().toFile();
for (String s : userArgsAsList) {
if (s.startsWith("--format=")) {
// ignore that as we'll add the '--format' as needed ourselves.
continue;
}
cmdList.add(s);
}
cmdList.add("--format=default");
cmdList.add(target);
String[] args = cmdList.toArray(new String[0]);
// run executable command (flake8 or flake8.bat or flake8.exe)
WriteToStreamHelper.write("Flake8: Executing command line:", out, (Object) args);
IPythonNature nature = PythonNature.getPythonNature(project);
ICallback0<Process> launchProcessCallback = () -> {
SimpleRunner simpleRunner = new SimpleRunner();
final Tuple<Process, String> r = simpleRunner.run(args, workingDir, nature, null, null);
Process process = r.o1;
return process;
};
this.processWatchDoc = new ExternalAnalizerProcessWatchDoc(out, monitor, this, launchProcessCallback, project, true);
this.processWatchDoc.start();
}
use of org.python.pydev.core.IPythonNature in project Pydev by fabioz.
the class MypyAnalysis method createMypyProcess.
/**
* Creates the mypy process and starts getting its output.
*/
void createMypyProcess(IExternalCodeAnalysisStream out) throws CoreException, MisconfigurationException, PythonNatureWithoutProjectException {
String mypyExecutable = FileUtils.getFileAbsolutePath(mypyLocation);
String target = location.toOSString();
ArrayList<String> cmdList = new ArrayList<String>();
cmdList.add(mypyExecutable);
String userArgs = StringUtils.replaceNewLines(MypyPreferences.getMypyArgs(resource), " ");
List<String> userArgsAsList = new ArrayList<>(Arrays.asList(ProcessUtils.parseArguments(userArgs)));
if (!userArgsAsList.contains("--show-column-numbers")) {
userArgsAsList.add("--show-column-numbers");
}
boolean foundFollowImports = false;
boolean foundCacheDir = false;
for (String arg : userArgsAsList) {
if (arg.startsWith("--follow-imports=silent")) {
foundFollowImports = true;
}
if (arg.startsWith("--cache-dir")) {
foundCacheDir = true;
}
}
if (!foundFollowImports) {
// We just want warnings for the current file.
userArgsAsList.add("--follow-imports=silent");
}
// run mypy in project location
IProject project = resource.getProject();
if (project == null || !project.isAccessible()) {
// If the project is no longer valid, we can't do much.
Log.log("Unable to run mypy in: " + target + ". Project not available (" + project + ").");
return;
}
File workingDir = project.getLocation().toFile();
if (!foundCacheDir) {
// Set a cache dir if one is not given.
userArgsAsList.add("--cache-dir=" + new File(workingDir, ".mypy_cache").toString());
}
cmdList.addAll(userArgsAsList);
cmdList.add(target);
String[] args = cmdList.toArray(new String[0]);
// run executable command (mypy or mypy.bat or mypy.exe)
WriteToStreamHelper.write("Mypy: Executing command line:", out, (Object) args);
IPythonNature nature = PythonNature.getPythonNature(project);
ICallback<String[], String[]> updateEnv = null;
if (MypyPreferences.getAddProjectFoldersToMyPyPath(resource)) {
Collection<String> addToMypyPath = new HashSet<String>();
IModulesManager[] managersInvolved = nature.getAstManager().getModulesManager().getManagersInvolved(false);
for (IModulesManager iModulesManager : managersInvolved) {
for (String s : StringUtils.split(iModulesManager.getNature().getPythonPathNature().getOnlyProjectPythonPathStr(true), "|")) {
if (!s.isEmpty()) {
addToMypyPath.add(s);
}
}
}
if (addToMypyPath.size() > 0) {
updateEnv = new ICallback<String[], String[]>() {
@Override
public String[] call(String[] arg) {
for (int i = 0; i < arg.length; i++) {
// Update var
if (arg[i].startsWith("MYPYPATH=")) {
arg[i] = arg[i] + SimpleRunner.getPythonPathSeparator() + StringUtils.join(SimpleRunner.getPythonPathSeparator(), addToMypyPath);
return arg;
}
}
// Create new var.
return ArrayUtils.concatArrays(arg, new String[] { "MYPYPATH=" + StringUtils.join(SimpleRunner.getPythonPathSeparator(), addToMypyPath) });
}
};
}
}
final ICallback<String[], String[]> finalUpdateEnv = updateEnv;
ICallback0<Process> launchProcessCallback = () -> {
SimpleRunner simpleRunner = new SimpleRunner();
final Tuple<Process, String> r = simpleRunner.run(args, workingDir, nature, null, finalUpdateEnv);
Process process = r.o1;
return process;
};
this.processWatchDoc = new ExternalAnalizerProcessWatchDoc(out, monitor, this, launchProcessCallback, project, true);
this.processWatchDoc.start();
}
use of org.python.pydev.core.IPythonNature in project Pydev by fabioz.
the class MatchImportsVisitorTest method testMatchImports2.
public void testMatchImports2() throws Exception {
Document doc = new Document("" + // rename a.b.c
"import a.b.c.d\n" + // rename a.b.c
"import a.b.c\n" + "import a.b\n" + "");
IPythonNature nature = new PythonNatureStub();
ParseOutput obj = PyParser.reparseDocument(new PyParser.ParserInfo(doc, nature));
SourceModule module = (SourceModule) AbstractModule.createModule((SimpleNode) obj.ast, null, "z", null);
MatchImportsVisitor visitor = new MatchImportsVisitor(nature, "a.b.c", module, null);
module.getAst().accept(visitor);
assertEquals(visitor.importsMatchingOnAliasPart.size(), 2);
assertEquals(visitor.occurrences.size(), 2);
}
use of org.python.pydev.core.IPythonNature in project Pydev by fabioz.
the class RefactoringRenameTestBase method setUp.
/**
* In the setUp, it initializes the files in the refactoring project
* @see com.python.pydev.analysis.refactoring.refactorer.refactorings.renamelocal.RefactoringLocalTestBase#setUp()
*/
@Override
public void setUp() throws Exception {
super.setUp();
if (filesInRefactoringProject == null) {
filesInRefactoringProject = PyFileListing.getPyFilesBelow(new File(TestDependent.TEST_COM_REFACTORING_PYSRC_LOC), new NullProgressMonitor(), true).getFoundPyFileInfos();
ArrayList<Tuple<List<ModulesKey>, IPythonNature>> iFiles = new ArrayList<Tuple<List<ModulesKey>, IPythonNature>>();
List<ModulesKey> modules = new ArrayList<ModulesKey>();
iFiles.add(new Tuple<List<ModulesKey>, IPythonNature>(modules, natureRefactoring));
FastStringBuffer tempBuf = new FastStringBuffer();
for (PyFileInfo info : filesInRefactoringProject) {
File f = info.getFile();
String modName = info.getModuleName(tempBuf);
ModulesKey modulesKey = new ModulesKey(modName, f);
modules.add(modulesKey);
SourceModule mod = (SourceModule) AbstractModule.createModule(modName, f, natureRefactoring, true);
// also create the additional info so that it can be used for finds
AbstractAdditionalTokensInfo additionalInfo = AdditionalProjectInterpreterInfo.getAdditionalInfoForProject(natureRefactoring);
additionalInfo.addAstInfo(mod.getAst(), modulesKey, false);
}
// RefactorerFindReferences.FORCED_RETURN = iFiles;
}
setUpConfigWorkspaceFiles();
}
Aggregations