Search in sources :

Example 1 with ModulesKeyForFolder

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

the class IOUtils method addAstInfo.

public List<IInfo> addAstInfo(ModulesKey key, boolean generateDelta) throws Exception {
    if (key instanceof ModulesKeyForFolder) {
        addModulesKeyForFolderToIndex(key, generateDelta);
        return new ArrayList<IInfo>(0);
    }
    boolean isZipModule = key instanceof ModulesKeyForZip;
    ModulesKeyForZip modulesKeyForZip = null;
    if (isZipModule) {
        modulesKeyForZip = (ModulesKeyForZip) key;
    }
    Object doc;
    if (isZipModule) {
        doc = FileUtilsFileBuffer.getCustomReturnFromZip(modulesKeyForZip.file, modulesKeyForZip.zipModulePath, null);
    } else {
        doc = FileUtilsFileBuffer.getCustomReturnFromFile(key.file, true, null);
    }
    char[] charArray;
    int len;
    if (doc instanceof IDocument) {
        IDocument document = (IDocument) doc;
        charArray = document.get().toCharArray();
        len = charArray.length;
    } else if (doc instanceof FastStringBuffer) {
        FastStringBuffer fastStringBuffer = (FastStringBuffer) doc;
        // In this case, we can actually get the internal array without doing any copies (and just specifying the len).
        charArray = fastStringBuffer.getInternalCharsArray();
        len = fastStringBuffer.length();
    } else if (doc instanceof String) {
        String str = (String) doc;
        charArray = str.toCharArray();
        len = charArray.length;
    } else if (doc instanceof char[]) {
        charArray = (char[]) doc;
        len = charArray.length;
    } else {
        throw new RuntimeException("Don't know how to handle: " + doc + " -- " + doc.getClass());
    }
    SimpleNode node = FastDefinitionsParser.parse(charArray, key.file.getName(), len, key.file);
    if (node == null) {
        return null;
    }
    return addAstInfo(node, key, generateDelta);
}
Also used : FastStringBuffer(org.python.pydev.shared_core.string.FastStringBuffer) ArrayList(java.util.ArrayList) ModulesKeyForZip(org.python.pydev.core.ModulesKeyForZip) IDocument(org.eclipse.jface.text.IDocument) ModulesKeyForFolder(org.python.pydev.core.ModulesKeyForFolder) SimpleNode(org.python.pydev.parser.jython.SimpleNode)

Example 2 with ModulesKeyForFolder

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

the class DiskCache method loadFrom.

/**
 * Loads from a reader a string that was acquired from writeTo.
 * @param objectsPoolMap
 */
public static DiskCache loadFrom(FastBufferedReader reader, ObjectsPoolMap objectsPoolMap) throws IOException {
    DiskCache diskCache = new DiskCache();
    FastStringBuffer line = reader.readLine();
    if (line.startsWith("-- ")) {
        throw new RuntimeException("Unexpected line: " + line);
    }
    diskCache.folderToPersist = line.toString();
    FastStringBuffer buf = new FastStringBuffer();
    CompleteIndexKey key = null;
    char[] internalCharsArray = line.getInternalCharsArray();
    while (true) {
        line = reader.readLine();
        key = null;
        if (line == null || line.startsWith("-- ")) {
            if (line != null && line.startsWith("-- END DISKCACHE")) {
                return diskCache;
            }
            throw new RuntimeException("Unexpected line: " + line);
        } else {
            int length = line.length();
            int part = 0;
            for (int i = 0; i < length; i++) {
                char c = internalCharsArray[i];
                if (c == '|') {
                    switch(part) {
                        case 0:
                            key = new CompleteIndexKey(ObjectsInternPool.internLocal(objectsPoolMap, buf.toString()));
                            diskCache.add(key);
                            break;
                        case 1:
                            key.lastModified = org.python.pydev.shared_core.string.StringUtils.parsePositiveLong(buf);
                            break;
                        case 2:
                            if (buf.length() > 0) {
                                key.key.file = new File(ObjectsInternPool.internLocal(objectsPoolMap, buf.toString()));
                            }
                            break;
                        case 3:
                            // regular folder path or zip path
                            if (buf.endsWith("|^")) {
                                key.key = new ModulesKeyForFolder(key.key.name, key.key.file);
                            } else {
                                key.key = new ModulesKeyForZip(key.key.name, key.key.file, ObjectsInternPool.internLocal(objectsPoolMap, buf.toString()), true);
                            }
                            break;
                        case 4:
                            // isfile in zip
                            if (buf.toString().equals("0")) {
                                ((ModulesKeyForZip) key.key).isFile = true;
                            }
                            break;
                        default:
                            throw new RuntimeException("Unexpected part in line: " + line);
                    }
                    part++;
                    buf.clear();
                } else {
                    buf.append(c);
                }
            }
            // Found end of line... this is the last part and depends on where we stopped previously.
            if (buf.length() > 0) {
                switch(part) {
                    case 1:
                        key.lastModified = StringUtils.parsePositiveLong(buf);
                        break;
                    case 2:
                        // File also written.
                        key.key.file = new File(ObjectsInternPool.internLocal(objectsPoolMap, buf.toString()));
                        break;
                    case 3:
                        // path in zip
                        key.key = new ModulesKeyForZip(key.key.name, key.key.file, ObjectsInternPool.internLocal(objectsPoolMap, buf.toString()), true);
                        break;
                    case 4:
                        // isfile in zip
                        if (buf.toString().equals("0")) {
                            ((ModulesKeyForZip) key.key).isFile = true;
                        }
                        break;
                }
                buf.clear();
            }
        }
    }
}
Also used : FastStringBuffer(org.python.pydev.shared_core.string.FastStringBuffer) ModulesKeyForZip(org.python.pydev.core.ModulesKeyForZip) File(java.io.File) ModulesKeyForFolder(org.python.pydev.core.ModulesKeyForFolder)

Example 3 with ModulesKeyForFolder

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

the class ModulesManager method handleLineParts.

private static void handleLineParts(ModulesManager modulesManager, HashMap<Integer, String> intToString, String[] split, int size, ArrayList<ModulesKey> lst) {
    if (size > 0 && split[0].length() > 0) {
        // Just making sure we have something there.
        ModulesKey key;
        if (size == 1) {
            key = new ModulesKey(split[0], null);
            // restore with empty modules.
            lst.add(key);
        } else if (size == 2) {
            key = new ModulesKey(split[0], new File(split[1]));
            // restore with empty modules.
            lst.add(key);
        } else if (size == 3) {
            try {
                key = new ModulesKeyForFolder(split[0], new File(split[1]));
                lst.add(key);
            } catch (NumberFormatException e) {
                Log.log(e);
            }
        } else if (size == 4) {
            try {
                key = new // module name
                ModulesKeyForZip(// module name
                split[0], // zip file (usually repeated over and over again)
                new File(intToString.get(Integer.parseInt(split[1]))), // path in zip
                split[2], // is file (false = folder)
                split[3].equals("1"));
                // restore with empty modules.
                lst.add(key);
            } catch (NumberFormatException e) {
                Log.log(e);
            }
        }
    }
}
Also used : ModulesKey(org.python.pydev.core.ModulesKey) ModulesKeyForZip(org.python.pydev.core.ModulesKeyForZip) File(java.io.File) ModulesKeyForFolder(org.python.pydev.core.ModulesKeyForFolder)

Example 4 with ModulesKeyForFolder

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

the class ModulesManager method saveToFile.

@Override
public void saveToFile(File workspaceMetadataFile) {
    if (workspaceMetadataFile.exists() && !workspaceMetadataFile.isDirectory()) {
        try {
            FileUtils.deleteFile(workspaceMetadataFile);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
    if (!workspaceMetadataFile.exists()) {
        workspaceMetadataFile.mkdirs();
    }
    File modulesKeysFile = new File(workspaceMetadataFile, "modulesKeys");
    File pythonpatHelperFile = new File(workspaceMetadataFile, "pythonpath");
    FastStringBuffer buf;
    HashMap<String, Integer> commonTokens = new HashMap<String, Integer>();
    synchronized (modulesKeysLock) {
        buf = new FastStringBuffer(this.modulesKeys.size() * 50);
        buf.append(MODULES_MANAGER_V3);
        for (Iterator<ModulesKey> iter = this.modulesKeys.keySet().iterator(); iter.hasNext(); ) {
            ModulesKey next = iter.next();
            buf.append(next.name);
            if (next.file != null) {
                buf.append("|");
                if (next instanceof ModulesKeyForZip) {
                    ModulesKeyForZip modulesKeyForZip = (ModulesKeyForZip) next;
                    if (modulesKeyForZip.zipModulePath != null) {
                        String fileStr = next.file.toString();
                        Integer t = commonTokens.get(fileStr);
                        if (t == null) {
                            t = commonTokens.size();
                            commonTokens.put(fileStr, t);
                        }
                        buf.append(t);
                        buf.append("|");
                        buf.append(modulesKeyForZip.zipModulePath);
                        buf.append("|");
                        buf.append(modulesKeyForZip.isFile ? '1' : '0');
                    }
                } else if (next instanceof ModulesKeyForFolder) {
                    ModulesKeyForFolder modulesKeyForFolder = (ModulesKeyForFolder) next;
                    if (modulesKeyForFolder.file != null) {
                        buf.append(modulesKeyForFolder.file.toString());
                        buf.append("|^");
                    }
                } else {
                    buf.append(next.file.toString());
                }
            }
            buf.append('\n');
        }
    }
    if (commonTokens.size() > 0) {
        FastStringBuffer header = new FastStringBuffer(buf.length() + (commonTokens.size() * 50));
        header.append(MODULES_MANAGER_V3);
        header.append("--COMMON--\n");
        for (Map.Entry<String, Integer> entries : commonTokens.entrySet()) {
            header.append(entries.getValue());
            header.append('=');
            header.append(entries.getKey());
            header.append('\n');
        }
        header.append("--END-COMMON--\n");
        header.append(buf);
        buf = header;
    }
    FileUtils.writeStrToFile(buf.toString(), modulesKeysFile);
    this.pythonPathHelper.saveToFile(pythonpatHelperFile);
}
Also used : FastStringBuffer(org.python.pydev.shared_core.string.FastStringBuffer) HashMap(java.util.HashMap) IOException(java.io.IOException) ModulesKeyForFolder(org.python.pydev.core.ModulesKeyForFolder) ModulesKey(org.python.pydev.core.ModulesKey) ModulesKeyForZip(org.python.pydev.core.ModulesKeyForZip) 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 5 with ModulesKeyForFolder

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

the class ModulesManagerTest method testLoad.

public void testLoad() throws Exception {
    SystemModulesManager manager = new SystemModulesManager(null);
    manager.addModule(new ModulesKey("bar", new File("bar.py")));
    manager.addModule(new ModulesKey("foo", new File("foo.py")));
    manager.addModule(new ModulesKey("empty", null));
    manager.addModule(new ModulesKeyForZip("zip", new File("zip.zip"), "path", true));
    manager.addModule(new ModulesKeyForFolder("folder.__init__", new File("folder")));
    PythonPathHelper pythonPathHelper = manager.getPythonPathHelper();
    pythonPathHelper.setPythonPath("rara|boo");
    assertEquals(Arrays.asList("rara", "boo"), manager.getPythonPathHelper().getPythonpath());
    File f = new File("modules_manager_testing.temporary_dir");
    try {
        FileUtils.deleteDirectoryTree(f);
    } catch (Exception e1) {
    // ignore
    }
    try {
        manager.saveToFile(f);
        SystemModulesManager loaded = new SystemModulesManager(null);
        SystemModulesManager.loadFromFile(loaded, f);
        ModulesKey[] onlyDirectModules = loaded.getOnlyDirectModules();
        boolean foundZip = false;
        boolean foundFolder = false;
        for (ModulesKey modulesKey : onlyDirectModules) {
            if (modulesKey.name.equals("zip")) {
                ModulesKeyForZip z = (ModulesKeyForZip) modulesKey;
                assertEquals(z.zipModulePath, "path");
                assertEquals(z.file, new File("zip.zip"));
                assertEquals(z.isFile, true);
                foundZip = true;
            } else if (modulesKey.name.equals("folder.__init__")) {
                ModulesKeyForFolder folder = (ModulesKeyForFolder) modulesKey;
                assertEquals(folder.file, new File("folder"));
                foundFolder = true;
            }
        }
        if (!foundZip) {
            fail("Did not find ModulesKeyForZip.");
        }
        if (!foundFolder) {
            fail("Did not find ModulesKeyForFolder.");
        }
        Set<String> set = new HashSet<String>();
        set.add("bar");
        set.add("foo");
        set.add("empty");
        set.add("zip");
        set.add("folder.__init__");
        assertEquals(set, loaded.getAllModuleNames(true, ""));
        assertEquals(Arrays.asList("rara", "boo"), loaded.getPythonPathHelper().getPythonpath());
    } finally {
        FileUtils.deleteDirectoryTree(f);
    }
}
Also used : ModulesKey(org.python.pydev.core.ModulesKey) ModulesKeyForZip(org.python.pydev.core.ModulesKeyForZip) File(java.io.File) ModulesKeyForFolder(org.python.pydev.core.ModulesKeyForFolder) HashSet(java.util.HashSet)

Aggregations

ModulesKeyForFolder (org.python.pydev.core.ModulesKeyForFolder)9 ModulesKey (org.python.pydev.core.ModulesKey)7 ModulesKeyForZip (org.python.pydev.core.ModulesKeyForZip)7 File (java.io.File)6 FastStringBuffer (org.python.pydev.shared_core.string.FastStringBuffer)5 IOException (java.io.IOException)3 AbstractMap (java.util.AbstractMap)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 SortedMap (java.util.SortedMap)2 TreeMap (java.util.TreeMap)2 LRUMap (org.python.pydev.shared_core.cache.LRUMap)2 IBufferFiller (com.python.pydev.analysis.additionalinfo.AbstractAdditionalDependencyInfo.IBufferFiller)1 FileInputStream (java.io.FileInputStream)1 InputStream (java.io.InputStream)1 HashSet (java.util.HashSet)1 TreeSet (java.util.TreeSet)1 LinkedBlockingQueue (java.util.concurrent.LinkedBlockingQueue)1 ZipFile (java.util.zip.ZipFile)1