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)));
}
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;
}
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();
}
}
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);
}
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);
}
}
Aggregations