Search in sources :

Example 1 with IPythonNature

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;
}
Also used : MisconfigurationException(org.python.pydev.core.MisconfigurationException) ArrayList(java.util.ArrayList) IPythonNature(org.python.pydev.core.IPythonNature) SystemPythonNature(org.python.pydev.plugin.nature.SystemPythonNature) IProject(org.eclipse.core.resources.IProject) CoreException(org.eclipse.core.runtime.CoreException) OperationCanceledException(org.eclipse.core.runtime.OperationCanceledException) PythonNatureWithoutProjectException(org.python.pydev.core.PythonNatureWithoutProjectException) MisconfigurationException(org.python.pydev.core.MisconfigurationException) PythonNatureWithoutProjectException(org.python.pydev.core.PythonNatureWithoutProjectException) Tuple(org.python.pydev.shared_core.structure.Tuple)

Example 2 with IPythonNature

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();
}
Also used : SimpleRunner(org.python.pydev.ast.runners.SimpleRunner) ArrayList(java.util.ArrayList) IPythonNature(org.python.pydev.core.IPythonNature) ExternalAnalizerProcessWatchDoc(com.python.pydev.analysis.external.ExternalAnalizerProcessWatchDoc) IProject(org.eclipse.core.resources.IProject) IFile(org.eclipse.core.resources.IFile) File(java.io.File) Tuple(org.python.pydev.shared_core.structure.Tuple)

Example 3 with IPythonNature

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();
}
Also used : SimpleRunner(org.python.pydev.ast.runners.SimpleRunner) ArrayList(java.util.ArrayList) IPythonNature(org.python.pydev.core.IPythonNature) IModulesManager(org.python.pydev.core.IModulesManager) ExternalAnalizerProcessWatchDoc(com.python.pydev.analysis.external.ExternalAnalizerProcessWatchDoc) IProject(org.eclipse.core.resources.IProject) IFile(org.eclipse.core.resources.IFile) File(java.io.File) Tuple(org.python.pydev.shared_core.structure.Tuple) HashSet(java.util.HashSet)

Example 4 with IPythonNature

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);
}
Also used : SourceModule(org.python.pydev.ast.codecompletion.revisited.modules.SourceModule) ParseOutput(org.python.pydev.shared_core.parsing.BaseParser.ParseOutput) IPythonNature(org.python.pydev.core.IPythonNature) PyParser(org.python.pydev.parser.PyParser) MatchImportsVisitor(com.python.pydev.analysis.refactoring.wizards.rename.MatchImportsVisitor) Document(org.eclipse.jface.text.Document) PythonNatureStub(org.python.pydev.parser.PythonNatureStub) SimpleNode(org.python.pydev.parser.jython.SimpleNode)

Example 5 with IPythonNature

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();
}
Also used : NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) SourceModule(org.python.pydev.ast.codecompletion.revisited.modules.SourceModule) ASTEntryWithSourceModule(org.python.pydev.ast.codecompletion.revisited.modules.ASTEntryWithSourceModule) FastStringBuffer(org.python.pydev.shared_core.string.FastStringBuffer) ArrayList(java.util.ArrayList) IPythonNature(org.python.pydev.core.IPythonNature) ModulesKey(org.python.pydev.core.ModulesKey) List(java.util.List) ArrayList(java.util.ArrayList) PyFileInfo(org.python.pydev.ast.listing_utils.PyFileListing.PyFileInfo) AbstractAdditionalTokensInfo(com.python.pydev.analysis.additionalinfo.AbstractAdditionalTokensInfo) IFile(org.eclipse.core.resources.IFile) File(java.io.File) Tuple(org.python.pydev.shared_core.structure.Tuple)

Aggregations

IPythonNature (org.python.pydev.core.IPythonNature)85 MisconfigurationException (org.python.pydev.core.MisconfigurationException)33 ArrayList (java.util.ArrayList)30 File (java.io.File)26 IProject (org.eclipse.core.resources.IProject)20 IFile (org.eclipse.core.resources.IFile)18 CoreException (org.eclipse.core.runtime.CoreException)17 Tuple (org.python.pydev.shared_core.structure.Tuple)14 IDocument (org.eclipse.jface.text.IDocument)12 SourceModule (org.python.pydev.ast.codecompletion.revisited.modules.SourceModule)12 ICodeCompletionASTManager (org.python.pydev.core.ICodeCompletionASTManager)11 IModule (org.python.pydev.core.IModule)10 TokensList (org.python.pydev.core.TokensList)10 HashSet (java.util.HashSet)9 List (java.util.List)9 BadLocationException (org.eclipse.jface.text.BadLocationException)9 IInterpreterInfo (org.python.pydev.core.IInterpreterInfo)9 SimpleNode (org.python.pydev.parser.jython.SimpleNode)9 PyEdit (org.python.pydev.editor.PyEdit)8 SystemPythonNature (org.python.pydev.plugin.nature.SystemPythonNature)8