Search in sources :

Example 31 with ModulesKey

use of org.python.pydev.core.ModulesKey in project Pydev by fabioz.

the class ModulesManagerTest method testBuildKeysForRegularEntries.

public void testBuildKeysForRegularEntries() {
    ProjectModulesManager modulesManager = (ProjectModulesManager) nature2.getAstManager().getModulesManager();
    String pythonpath = TestDependent.TEST_PYSRC_TESTING_LOC + "namespace_pkg/";
    ProjectStub project = new ProjectStub("testProjectStubRefactoring", pythonpath, new IProject[0], new IProject[0]);
    IProgressMonitor monitor = getProgressMonitor();
    modulesManager.pythonPathHelper.setPythonPath(pythonpath);
    ModulesFoundStructure modulesFound = modulesManager.pythonPathHelper.getModulesFoundStructure(project, monitor);
    PyPublicTreeMap<ModulesKey, ModulesKey> keys = ModulesManager.buildKeysFromModulesFound(monitor, modulesFound);
    ModulesManager.buildKeysForRegularEntries(monitor, modulesFound, keys, false);
    assertTrue(keys.containsKey(new ModulesKey("folder1.__init__", null)));
    assertTrue(keys.containsKey(new ModulesKey("folder1.folder2.__init__", null)));
    assertTrue(keys.containsKey(new ModulesKey("folder1.folder2.mymod", null)));
    assertFalse(keys.containsKey(new ModulesKey(".__init__", null)));
}
Also used : IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) ModulesKey(org.python.pydev.core.ModulesKey)

Example 32 with ModulesKey

use of org.python.pydev.core.ModulesKey in project Pydev by fabioz.

the class RefactorerFinds method findLikelyModulesWithChildren.

private HashSet<SourceModule> findLikelyModulesWithChildren(RefactoringRequest request, HierarchyNodeModel model, List<AbstractAdditionalDependencyInfo> infoForProject) {
    // get the modules that are most likely to have that declaration.
    HashSet<SourceModule> modulesToAnalyze = new HashSet<SourceModule>();
    for (AbstractAdditionalDependencyInfo additionalInfo : infoForProject) {
        IProgressMonitor monitor = request.getMonitor();
        if (monitor == null) {
            monitor = new NullProgressMonitor();
        }
        monitor.beginTask("Find likely modules with children", 100);
        monitor.setTaskName("Searching: " + model.name);
        try {
            List<ModulesKey> modules;
            try {
                request.pushMonitor(new SubProgressMonitor(monitor, 90));
                if (additionalInfo instanceof AdditionalProjectInterpreterInfo) {
                    AdditionalProjectInterpreterInfo additionalProjectInterpreterInfo = (AdditionalProjectInterpreterInfo) additionalInfo;
                    modules = additionalProjectInterpreterInfo.getModulesWithToken(model.name, monitor);
                } else {
                    continue;
                }
                if (monitor.isCanceled()) {
                    throw new OperationCanceledException();
                }
            } finally {
                request.popMonitor().done();
            }
            try {
                request.pushMonitor(new SubProgressMonitor(monitor, 10));
                request.getMonitor().beginTask("Find likely modules with children", modules.size());
                for (ModulesKey declaringModuleName : modules) {
                    if (DEBUG) {
                        System.out.println("findLikelyModulesWithChildren: " + declaringModuleName);
                    }
                    IModule module = null;
                    IPythonNature pythonNature = null;
                    if (additionalInfo instanceof AdditionalProjectInterpreterInfo) {
                        AdditionalProjectInterpreterInfo projectInterpreterInfo = (AdditionalProjectInterpreterInfo) additionalInfo;
                        pythonNature = PythonNature.getPythonNature(projectInterpreterInfo.getProject());
                    }
                    if (pythonNature == null) {
                        pythonNature = request.nature;
                    }
                    BaseModuleRequest moduleRequest = new BaseModuleRequest(request.acceptTypeshed);
                    module = pythonNature.getAstManager().getModule(declaringModuleName.name, pythonNature, false, moduleRequest);
                    if (module == null && pythonNature != request.nature) {
                        module = request.nature.getAstManager().getModule(declaringModuleName.name, request.nature, false, moduleRequest);
                    }
                    if (module instanceof SourceModule) {
                        modulesToAnalyze.add((SourceModule) module);
                    }
                    request.getMonitor().worked(1);
                }
            } finally {
                request.popMonitor().done();
            }
        } finally {
            monitor.done();
        }
    }
    return modulesToAnalyze;
}
Also used : SourceModule(org.python.pydev.ast.codecompletion.revisited.modules.SourceModule) NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) IModule(org.python.pydev.core.IModule) BaseModuleRequest(org.python.pydev.core.BaseModuleRequest) OperationCanceledException(org.eclipse.core.runtime.OperationCanceledException) IPythonNature(org.python.pydev.core.IPythonNature) SubProgressMonitor(org.eclipse.core.runtime.SubProgressMonitor) AdditionalProjectInterpreterInfo(com.python.pydev.analysis.additionalinfo.AdditionalProjectInterpreterInfo) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) ModulesKey(org.python.pydev.core.ModulesKey) AbstractAdditionalDependencyInfo(com.python.pydev.analysis.additionalinfo.AbstractAdditionalDependencyInfo) HashSet(java.util.HashSet)

Example 33 with ModulesKey

use of org.python.pydev.core.ModulesKey in project Pydev by fabioz.

the class AbstractRenameWorkspaceRefactorProcess method doCheckInitialOnWorkspace.

/**
 * This method is made to be used in the checkInitialOnWorkspace implementation.
 *
 * It will find files with possible references in the workspace (from the token
 * name we're searching) and for each file that maps to a module it will
 * call getOccurrencesInOtherModule, and will add those occurrences to
 * the map with the file pointing to the entries.
 *
 * @param status used to add some error status to the refactoring
 * @param request the request used for the refactoring
 */
protected void doCheckInitialOnWorkspace(RefactoringStatus status, RefactoringRequest request) {
    try {
        request.getMonitor().beginTask("Check references on workspace", 100);
        List<Tuple<List<ModulesKey>, IPythonNature>> references;
        try {
            request.pushMonitor(new SubProgressMonitor(request.getMonitor(), 90));
            references = findFilesWithPossibleReferences(request);
            if (request.getMonitor().isCanceled()) {
                return;
            }
        } finally {
            request.popMonitor().done();
        }
        int total = references.size();
        try {
            request.pushMonitor(new SubProgressMonitor(request.getMonitor(), 10));
            request.getMonitor().beginTask("Analyzing references found", total);
            int i = 0;
            IModuleRequestState moduleRequest = new BaseModuleRequest(request.acceptTypeshed);
            for (Tuple<List<ModulesKey>, IPythonNature> file : references) {
                i++;
                request.communicateWork(StringUtils.format("Analyzing %s (%s of %s)", file.o2.getProject(), i, total));
                PythonNature nature = (PythonNature) file.o2;
                if (nature != null) {
                    if (!nature.startRequests()) {
                        continue;
                    }
                    try {
                        for (ModulesKey key : file.o1) {
                            IProjectModulesManager modulesManager = (IProjectModulesManager) nature.getAstManager().getModulesManager();
                            request.checkCancelled();
                            String modName = key.name;
                            if (modName != null) {
                                if (!request.moduleName.equals(modName)) {
                                    // we've already checked the module from the request...
                                    request.checkCancelled();
                                    IModule module = modulesManager.getModuleInDirectManager(modName, nature, false, moduleRequest);
                                    if (module instanceof SourceModule) {
                                        SourceModule sourceModule = (SourceModule) module;
                                        if (sourceModule.getAst() == null) {
                                            status.addWarning("Unable to get AST for: " + modName);
                                            continue;
                                        }
                                        request.checkCancelled();
                                        List<ASTEntry> entryOccurrences = getOccurrencesInOtherModule(status, request, request.qualifier, (SourceModule) module, nature);
                                        if (entryOccurrences.size() > 0) {
                                            addOccurrences(entryOccurrences, key.file, modName);
                                        }
                                    }
                                }
                            }
                        }
                    } finally {
                        nature.endRequests();
                    }
                }
            }
        } finally {
            request.popMonitor().done();
        }
    } catch (OperationCanceledException e) {
    // that's ok
    } catch (Exception e) {
        throw new RuntimeException(e);
    } finally {
        request.getMonitor().done();
    }
}
Also used : IProjectModulesManager(org.python.pydev.core.IProjectModulesManager) SourceModule(org.python.pydev.ast.codecompletion.revisited.modules.SourceModule) IModule(org.python.pydev.core.IModule) IModuleRequestState(org.python.pydev.core.IModuleRequestState) IPythonNature(org.python.pydev.core.IPythonNature) PythonNature(org.python.pydev.plugin.nature.PythonNature) BaseModuleRequest(org.python.pydev.core.BaseModuleRequest) OperationCanceledException(org.eclipse.core.runtime.OperationCanceledException) IPythonNature(org.python.pydev.core.IPythonNature) SubProgressMonitor(org.eclipse.core.runtime.SubProgressMonitor) OperationCanceledException(org.eclipse.core.runtime.OperationCanceledException) ASTEntry(org.python.pydev.parser.visitors.scope.ASTEntry) ModulesKey(org.python.pydev.core.ModulesKey) List(java.util.List) Tuple(org.python.pydev.shared_core.structure.Tuple)

Example 34 with ModulesKey

use of org.python.pydev.core.ModulesKey in project Pydev by fabioz.

the class SystemModulesManager method save.

@Override
public void save() {
    final File workspaceMetadataFile = getIoDirectory();
    DeltaSaver<ModulesKey> d = deltaSaver;
    if (d != null) {
        // When save is called, the deltas don't need to be used anymore.
        d.clearAll();
    }
    this.saveToFile(workspaceMetadataFile);
}
Also used : ModulesKey(org.python.pydev.core.ModulesKey) File(java.io.File)

Example 35 with ModulesKey

use of org.python.pydev.core.ModulesKey in project Pydev by fabioz.

the class CompiledModule method updateCache.

/**
 * Updates the file with the cache to have the given information.
 */
private static void updateCache(final String name, IModulesManager manager, final Tuple<File, IToken[]> info) {
    try {
        if (info != null && info.o2 != null && info.o2.length > 10) {
            // Don't cache anything less than 10 tokens.
            File f = getCacheFile(name, manager);
            // Only cache modules that are in the system modules manager.
            if (f == null && !(manager instanceof ISystemModulesManager)) {
                ISystemModulesManager systemModulesManager = manager.getSystemModulesManager();
                // i.e.: just making sure it won't be used later on...
                manager = null;
                // a project we don't cache it for now).
                for (String part : new FullRepIterable(name)) {
                    if (systemModulesManager.hasModule(new ModulesKey(part, null))) {
                        f = getCacheFile(name, systemModulesManager);
                        break;
                    }
                    if (!part.contains(".")) {
                        part += ".__init__";
                        if (systemModulesManager.hasModule(new ModulesKey(part, null))) {
                            f = getCacheFile(name, systemModulesManager);
                            break;
                        }
                    }
                }
            }
            if (f != null) {
                final File cacheFile = f;
                IRunnableWithMonitor runnable = new IRunnableWithMonitor() {

                    @Override
                    public void run() {
                        try (OutputStream out = new FileOutputStream(cacheFile)) {
                            try (GZIPOutputStream gzip = new GZIPOutputStream(out)) {
                                try (BufferedOutputStream buf = new BufferedOutputStream(gzip)) {
                                    try (ObjectOutputStream stream = new ObjectOutputStream(buf)) {
                                        stream.writeObject(name);
                                        stream.writeObject(info.o1);
                                        IToken[] toks = info.o2;
                                        int size = toks.length;
                                        stream.writeInt(size);
                                        // the docstrings later on).
                                        for (int i = 0; i < size; i++) {
                                            IToken tok = toks[i];
                                            stream.writeObject(tok.getRepresentation());
                                            stream.writeInt(tok.getType());
                                            stream.writeObject(tok.getArgs());
                                            stream.writeObject(tok.getParentPackage());
                                        }
                                        for (int i = 0; i < size; i++) {
                                            stream.writeObject(toks[i].getDocStr());
                                        }
                                    }
                                }
                            }
                        } catch (Exception e) {
                            Log.log(e);
                        }
                    }

                    @Override
                    public void setMonitor(IProgressMonitor monitor) {
                    }
                };
                RunnableAsJobsPoolThread.getSingleton().scheduleToRun(runnable, "Cache module: " + name);
            }
        }
    } catch (Exception e) {
        Log.log(e);
    }
}
Also used : GZIPOutputStream(java.util.zip.GZIPOutputStream) BufferedOutputStream(java.io.BufferedOutputStream) ObjectOutputStream(java.io.ObjectOutputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) ObjectOutputStream(java.io.ObjectOutputStream) CoreException(org.eclipse.core.runtime.CoreException) PythonNatureWithoutProjectException(org.python.pydev.core.PythonNatureWithoutProjectException) IOException(java.io.IOException) MisconfigurationException(org.python.pydev.core.MisconfigurationException) ISystemModulesManager(org.python.pydev.core.ISystemModulesManager) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) GZIPOutputStream(java.util.zip.GZIPOutputStream) IToken(org.python.pydev.core.IToken) FullRepIterable(org.python.pydev.shared_core.string.FullRepIterable) FileOutputStream(java.io.FileOutputStream) ModulesKey(org.python.pydev.core.ModulesKey) IRunnableWithMonitor(org.python.pydev.core.concurrency.IRunnableWithMonitor) File(java.io.File) BufferedOutputStream(java.io.BufferedOutputStream)

Aggregations

ModulesKey (org.python.pydev.core.ModulesKey)58 File (java.io.File)23 ArrayList (java.util.ArrayList)13 HashSet (java.util.HashSet)11 MisconfigurationException (org.python.pydev.core.MisconfigurationException)11 ModulesKeyForZip (org.python.pydev.core.ModulesKeyForZip)10 FastStringBuffer (org.python.pydev.shared_core.string.FastStringBuffer)10 IOException (java.io.IOException)9 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)9 SourceModule (org.python.pydev.ast.codecompletion.revisited.modules.SourceModule)9 Tuple (org.python.pydev.shared_core.structure.Tuple)9 IFile (org.eclipse.core.resources.IFile)8 IPythonNature (org.python.pydev.core.IPythonNature)8 HashMap (java.util.HashMap)7 ModulesKeyForFolder (org.python.pydev.core.ModulesKeyForFolder)7 IModule (org.python.pydev.core.IModule)6 List (java.util.List)5 IDocument (org.eclipse.jface.text.IDocument)5 AbstractModule (org.python.pydev.ast.codecompletion.revisited.modules.AbstractModule)5 AbstractAdditionalTokensInfo (com.python.pydev.analysis.additionalinfo.AbstractAdditionalTokensInfo)4