Search in sources :

Example 36 with ModulesKey

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

the class DiskCache method writeTo.

/**
 * Writes this cache in a format that may later be restored with loadFrom.
 */
public void writeTo(FastStringBuffer tempBuf) {
    tempBuf.append("-- START DISKCACHE_" + DiskCache.VERSION + "\n");
    tempBuf.append(folderToPersist);
    tempBuf.append('\n');
    for (CompleteIndexKey key : keys.values()) {
        ModulesKey modKey = key.key;
        tempBuf.append(modKey.name);
        tempBuf.append('|');
        tempBuf.append(key.lastModified);
        tempBuf.append('|');
        if (modKey.file != null) {
            tempBuf.append(modKey.file.toString());
        } else {
        // could be null!
        }
        if (modKey instanceof ModulesKeyForZip) {
            ModulesKeyForZip modulesKeyForZip = (ModulesKeyForZip) modKey;
            tempBuf.append('|');
            tempBuf.append(modulesKeyForZip.zipModulePath);
            tempBuf.append('|');
            tempBuf.append(modulesKeyForZip.isFile ? '0' : '1');
        } else if (modKey instanceof ModulesKeyForFolder) {
            tempBuf.append('|');
            tempBuf.append('^');
        }
        tempBuf.append('\n');
    }
    tempBuf.append("-- END DISKCACHE\n");
}
Also used : ModulesKey(org.python.pydev.core.ModulesKey) ModulesKeyForZip(org.python.pydev.core.ModulesKeyForZip) ModulesKeyForFolder(org.python.pydev.core.ModulesKeyForFolder)

Example 37 with ModulesKey

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

the class DiskCacheTest method testDiskCacheWithZipModulesKey.

public void testDiskCacheWithZipModulesKey() throws Exception {
    DiskCache cache = new DiskCache(new File(baseDir, ".cache"), "_test_disk_cache");
    cache.add(new CompleteIndexKey(new ModulesKey("mod1", new File(baseDir, "f1")), 100));
    cache.add(new CompleteIndexKey(new ModulesKey("modnull", null), 100));
    cache.add(new CompleteIndexKey(new ModulesKeyForZip("mod2", new File(baseDir, "my.zip"), "path", true), 100));
    cache.add(new CompleteIndexKey(new ModulesKeyForZip("mod3", new File(baseDir, "my.zip"), "path2", false), 100));
    FastStringBuffer tempBuf = new FastStringBuffer();
    cache.writeTo(tempBuf);
    FastBufferedReader reader = new FastBufferedReader(new StringReader(tempBuf.toString()));
    // 
    FastStringBuffer line = reader.readLine();
    assertEquals(line.toString(), "-- START DISKCACHE_" + DiskCache.VERSION);
    ObjectsPoolMap objectsPoolMap = new ObjectsPoolMap();
    DiskCache loadFrom = DiskCache.loadFrom(reader, objectsPoolMap);
    assertEquals(cache.keys(), loadFrom.keys());
    assertEquals(cache.getFolderToPersist(), loadFrom.getFolderToPersist());
    CompleteIndexKey mod = cache.keys().get(new CompleteIndexKey("mod2"));
    ModulesKeyForZip zip = (ModulesKeyForZip) mod.key;
    assertEquals(zip.zipModulePath, "path");
    mod = loadFrom.keys().get(new CompleteIndexKey("mod2"));
    zip = (ModulesKeyForZip) mod.key;
    assertEquals(zip.zipModulePath, "path");
    mod = loadFrom.keys().get(new CompleteIndexKey("modnull"));
    assertNull(mod.key.file);
    mod = loadFrom.keys().get(new CompleteIndexKey("mod3"));
    zip = (ModulesKeyForZip) mod.key;
    assertEquals(zip.zipModulePath, "path2");
    assertTrue(zip.isFile);
}
Also used : FastBufferedReader(org.python.pydev.core.FastBufferedReader) ObjectsPoolMap(org.python.pydev.core.ObjectsInternPool.ObjectsPoolMap) FastStringBuffer(org.python.pydev.shared_core.string.FastStringBuffer) StringReader(java.io.StringReader) ModulesKey(org.python.pydev.core.ModulesKey) ModulesKeyForZip(org.python.pydev.core.ModulesKeyForZip) File(java.io.File)

Example 38 with ModulesKey

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

the class ModulesManager method buildKeysForRegularEntries.

public static void buildKeysForRegularEntries(IProgressMonitor monitor, ModulesFoundStructure modulesFound, PyPublicTreeMap<ModulesKey, ModulesKey> keys, boolean includeOnlySourceModules) {
    String[] dottedValidSourceFiles = FileTypesPreferences.getDottedValidSourceFiles();
    int j = 0;
    FastStringBuffer buffer = new FastStringBuffer();
    Map<String, File> packages = new HashMap<String, File>();
    String packageM;
    File packageF;
    // now, create in memory modules for all the loaded files (empty modules).
    for (Iterator<Map.Entry<File, String>> iterator = modulesFound.regularModules.entrySet().iterator(); iterator.hasNext() && monitor.isCanceled() == false; j++) {
        Map.Entry<File, String> entry = iterator.next();
        String m = entry.getValue();
        if (m != null) {
            if (j % 20 == 0) {
                // no need to report all the time (that's pretty fast now)
                buffer.clear();
                monitor.setTaskName(buffer.append("Module resolved: ").append(m).toString());
                monitor.worked(1);
            }
            // we don't load them at this time.
            File f = entry.getKey();
            if (includeOnlySourceModules) {
                // check if we should include only source modules
                if (!PythonPathHelper.isValidSourceFile(f.getName())) {
                    continue;
                }
            }
            packageM = FullRepIterable.getParentModule(m);
            packageF = f.getParentFile();
            while (!packageM.isEmpty()) {
                packages.put(packageM, packageF);
                packageM = FullRepIterable.getParentModule(packageM);
                packageF = packageF.getParentFile();
            }
            ModulesKey modulesKey = new ModulesKey(m, f);
            // no conflict (easy)
            if (!keys.containsKey(modulesKey)) {
                keys.put(modulesKey, modulesKey);
            } else {
                // we have a conflict, so, let's resolve which one to keep (the old one or this one)
                if (PythonPathHelper.isValidSourceFile(f.getName(), dottedValidSourceFiles)) {
                    // source files have priority over other modules (dlls) -- if both are source, there is no real way to resolve
                    // this priority, so, let's just add it over.
                    keys.put(modulesKey, modulesKey);
                }
            }
        }
    }
    for (Map.Entry<String, File> packageEntry : packages.entrySet()) {
        buffer.clear();
        File f = packageEntry.getValue();
        ModulesKey modulesKeyForFolder = new ModulesKeyForFolder(buffer.append(packageEntry.getKey()).append(".__init__").toString(), f);
        if (!keys.containsKey(modulesKeyForFolder)) {
            keys.put(modulesKeyForFolder, modulesKeyForFolder);
        }
    }
}
Also used : FastStringBuffer(org.python.pydev.shared_core.string.FastStringBuffer) HashMap(java.util.HashMap) ModulesKeyForFolder(org.python.pydev.core.ModulesKeyForFolder) Entry(org.python.pydev.ast.codecompletion.revisited.PyPublicTreeMap.Entry) ModulesKey(org.python.pydev.core.ModulesKey) File(java.io.File) LRUMap(org.python.pydev.shared_core.cache.LRUMap) Map(java.util.Map) SortedMap(java.util.SortedMap) HashMap(java.util.HashMap) AbstractMap(java.util.AbstractMap) TreeMap(java.util.TreeMap)

Example 39 with ModulesKey

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

the class ModulesManager method buildKeysForZipContents.

public static void buildKeysForZipContents(PyPublicTreeMap<ModulesKey, ModulesKey> keys, ZipContents zipContents) {
    for (String filePathInZip : zipContents.foundFileZipPaths) {
        String modName = StringUtils.stripExtension(filePathInZip).replace('/', '.');
        if (DEBUG_ZIP) {
            System.out.println("Found in zip:" + modName);
        }
        ModulesKey k = new ModulesKeyForZip(modName, zipContents.zipFile, filePathInZip, true);
        keys.put(k, k);
        if (zipContents.zipContentsType == ZipContents.ZIP_CONTENTS_TYPE_JAR) {
            // folder modules are only created for jars (because for python files, the __init__.py is required).
            for (String s : new FullRepIterable(FullRepIterable.getWithoutLastPart(modName))) {
                // the one without the last part was already added
                k = new ModulesKeyForZip(s, zipContents.zipFile, s.replace('.', '/'), false);
                keys.put(k, k);
            }
        }
    }
}
Also used : FullRepIterable(org.python.pydev.shared_core.string.FullRepIterable) ModulesKey(org.python.pydev.core.ModulesKey) ModulesKeyForZip(org.python.pydev.core.ModulesKeyForZip)

Example 40 with ModulesKey

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

the class ModulesManager method getAllDirectModulesStartingWith.

@Override
public SortedMap<ModulesKey, ModulesKey> getAllDirectModulesStartingWith(String strStartingWith) {
    if (strStartingWith.length() == 0) {
        synchronized (modulesKeysLock) {
            // a java.util.ConcurrentModificationException on places that use it)
            return new PyPublicTreeMap<ModulesKey, ModulesKey>(modulesKeys);
        }
    }
    ModulesKey startingWith = new ModulesKey(strStartingWith, null);
    ModulesKey endingWith = new ModulesKey(strStartingWith + "\uffff\uffff\uffff\uffff", null);
    synchronized (modulesKeysLock) {
        // a java.util.ConcurrentModificationException on places that use it)
        return new PyPublicTreeMap<ModulesKey, ModulesKey>(modulesKeys.subMap(startingWith, endingWith));
    }
}
Also used : ModulesKey(org.python.pydev.core.ModulesKey)

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