Search in sources :

Example 11 with ModulesKey

use of org.python.pydev.core.ModulesKey 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 12 with ModulesKey

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

the class ModulesManager method getModule.

/**
 * This method returns the module that corresponds to the path passed as a parameter.
 *
 * @param name the name of the module we're looking for  (e.g.: mod1.mod2)
 * @param dontSearchInit is used in a negative form because initially it was isLookingForRelative, but
 * it actually defines if we should look in __init__ modules too, so, the name matches the old signature.
 *
 * NOTE: isLookingForRelative description was: when looking for relative imports, we don't check for __init__
 * @return the module represented by this name
 */
protected IModule getModule(boolean acceptCompiledModule, String name, IPythonNature nature, boolean dontSearchInit, IModuleRequestState moduleRequest) {
    synchronized (lockTemporaryModules) {
        SortedMap<Integer, IModule> map = temporaryModules.get(name);
        if (map != null && map.size() > 0) {
            if (DEBUG_TEMPORARY_MODULES) {
                System.out.println("Returning temporary module: " + name);
            }
            return map.get(map.lastKey());
        }
    }
    AbstractModule n = null;
    ModulesKey keyForCacheAccess = new ModulesKey(null, null);
    if (!dontSearchInit) {
        if (n == null) {
            keyForCacheAccess.name = (String) StringUtils.join(".", new String[] { name, "__init__" }, null);
            n = cache.getObj(keyForCacheAccess, this);
            if (n != null) {
                name = keyForCacheAccess.name;
            }
        }
    }
    if (n == null) {
        keyForCacheAccess.name = name;
        n = cache.getObj(keyForCacheAccess, this);
    }
    if (n instanceof SourceModule) {
        // ok, module exists, let's check if it is synched with the filesystem version...
        SourceModule s = (SourceModule) n;
        if (!s.isSynched()) {
            // change it for an empty and proceed as usual.
            n = (AbstractModule) addModule(createModulesKey(s.getName(), s.getFile()));
        }
    }
    if (n instanceof EmptyModule) {
        EmptyModule e = (EmptyModule) n;
        if (e.f != null) {
            if (!e.f.exists()) {
                // if the file does not exist anymore, just remove it.
                keyForCacheAccess.name = name;
                keyForCacheAccess.file = e.f;
                doRemoveSingleModule(keyForCacheAccess);
                n = null;
            } else {
                // file exists
                n = checkOverride(name, nature, n);
                if (n instanceof EmptyModule) {
                    // ok, handle case where the file is actually from a zip file...
                    if (e instanceof EmptyModuleForZip) {
                        EmptyModuleForZip emptyModuleForZip = (EmptyModuleForZip) e;
                        if (emptyModuleForZip.pathInZip.endsWith(".class") || !emptyModuleForZip.isFile) {
                            // handle java class... (if it's a class or a folder in a jar)
                            try {
                                if (createModuleFromJar != null) {
                                    n = createModuleFromJar.call(emptyModuleForZip, nature);
                                    n = decorateModule(n, nature);
                                } else {
                                    n = null;
                                }
                            } catch (Throwable e1) {
                                Log.log("Unable to create module from jar (note: JDT is required for Jython development): " + emptyModuleForZip + " project: " + (nature != null ? nature.getProject() : "null"), e1);
                                n = null;
                            }
                        } else if (FileTypesPreferences.isValidDll(emptyModuleForZip.pathInZip)) {
                            // .pyd
                            n = new CompiledModule(name, this, nature);
                            n = decorateModule(n, nature);
                        } else if (PythonPathHelper.isValidSourceFile(emptyModuleForZip.pathInZip)) {
                            // handle python file from zip... we have to create it getting the contents from the zip file
                            try {
                                IDocument doc = FileUtilsFileBuffer.getDocFromZip(emptyModuleForZip.f, emptyModuleForZip.pathInZip);
                                // NOTE: The nature (and so the grammar to be used) must be defined by this modules
                                // manager (and not by the initial caller)!!
                                n = AbstractModule.createModuleFromDoc(name, emptyModuleForZip.f, doc, this.getNature(), false);
                                SourceModule zipModule = (SourceModule) n;
                                zipModule.zipFilePath = emptyModuleForZip.pathInZip;
                                n = decorateModule(n, nature);
                            } catch (Exception exc1) {
                                Log.log(exc1);
                                n = null;
                            }
                        }
                    } else if (e instanceof EmptyModuleForFolder) {
                        try {
                            n = new InitFromDirModule(name, e.f, new Module(new stmtType[0]), null, this.getNature());
                            n = decorateModule(n, nature);
                        } catch (Exception e1) {
                            Log.log(e1);
                            n = null;
                        }
                    } else {
                        // regular case... just go on and create it.
                        try {
                            // NOTE: The nature (and so the grammar to be used) must be defined by this modules
                            // manager (and not by the initial caller)!!
                            n = AbstractModule.createModule(name, e.f, this.getNature(), true);
                            n = decorateModule(n, nature);
                        } catch (IOException exc) {
                            keyForCacheAccess.name = name;
                            keyForCacheAccess.file = e.f;
                            doRemoveSingleModule(keyForCacheAccess);
                            n = null;
                        } catch (MisconfigurationException exc) {
                            Log.log(exc);
                            n = null;
                        }
                    }
                }
            }
        } else {
            // ok, it does not have a file associated, so, we treat it as a builtin (this can happen in java jars)
            n = checkOverride(name, nature, n);
            if (n instanceof EmptyModule) {
                if (acceptCompiledModule) {
                    n = new CompiledModule(name, this, nature);
                    n = decorateModule(n, nature);
                } else {
                    return null;
                }
            }
        }
        if (n != null) {
            doAddSingleModule(createModulesKey(name, e.f), n);
        } else {
            Log.log(("The module " + name + " could not be found nor created!"));
        }
    }
    if (n instanceof EmptyModule) {
        throw new RuntimeException("Should not be an empty module anymore: " + n);
    }
    if (n instanceof SourceModule) {
        SourceModule sourceModule = (SourceModule) n;
        // now, here's a catch... it may be a bootstrap module...
        if (sourceModule.isBootstrapModule()) {
            // if it's a bootstrap module, we must replace it for the related compiled module.
            n = new CompiledModule(name, this, nature);
            n = decorateModule(n, nature);
        }
    }
    return n;
}
Also used : SourceModule(org.python.pydev.ast.codecompletion.revisited.modules.SourceModule) IModule(org.python.pydev.core.IModule) InitFromDirModule(org.python.pydev.ast.codecompletion.revisited.modules.InitFromDirModule) org.python.pydev.parser.jython.ast.stmtType(org.python.pydev.parser.jython.ast.stmtType) MisconfigurationException(org.python.pydev.core.MisconfigurationException) EmptyModuleForZip(org.python.pydev.ast.codecompletion.revisited.modules.EmptyModuleForZip) IOException(java.io.IOException) IOException(java.io.IOException) MisconfigurationException(org.python.pydev.core.MisconfigurationException) AbstractModule(org.python.pydev.ast.codecompletion.revisited.modules.AbstractModule) EmptyModuleForFolder(org.python.pydev.ast.codecompletion.revisited.modules.EmptyModuleForFolder) ModulesKey(org.python.pydev.core.ModulesKey) CompiledModule(org.python.pydev.ast.codecompletion.revisited.modules.CompiledModule) AbstractModule(org.python.pydev.ast.codecompletion.revisited.modules.AbstractModule) IModule(org.python.pydev.core.IModule) Module(org.python.pydev.parser.jython.ast.Module) InitFromDirModule(org.python.pydev.ast.codecompletion.revisited.modules.InitFromDirModule) SourceModule(org.python.pydev.ast.codecompletion.revisited.modules.SourceModule) CompiledModule(org.python.pydev.ast.codecompletion.revisited.modules.CompiledModule) EmptyModule(org.python.pydev.ast.codecompletion.revisited.modules.EmptyModule) EmptyModule(org.python.pydev.ast.codecompletion.revisited.modules.EmptyModule) IDocument(org.eclipse.jface.text.IDocument)

Example 13 with ModulesKey

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

the class ModulesManager method changePythonPath.

/**
 * Change the pythonpath (used for both: system and project)
 *
 * @param project: may be null
 * @param defaultSelectedInterpreter: may be null
 */
@Override
public void changePythonPath(String pythonpath, final IProject project, IProgressMonitor monitor) {
    if (monitor == null) {
        monitor = new NullProgressMonitor();
    }
    pythonPathHelper.setPythonPath(pythonpath);
    ModulesFoundStructure modulesFound = pythonPathHelper.getModulesFoundStructure(project, monitor);
    PyPublicTreeMap<ModulesKey, ModulesKey> keys = buildKeysFromModulesFound(monitor, modulesFound);
    onChangePythonpath(keys);
    synchronized (modulesKeysLock) {
        cache.clear();
        cachePredefined.clear();
        // assign to instance variable
        this.modulesKeys.clear();
        this.modulesKeys.putAll(keys);
    }
}
Also used : NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) ModulesKey(org.python.pydev.core.ModulesKey)

Example 14 with ModulesKey

use of org.python.pydev.core.ModulesKey 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 15 with ModulesKey

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

the class SystemModulesManager method onChangePythonpath.

/**
 * Called after the pythonpath is changed.
 */
@Override
protected void onChangePythonpath(SortedMap<ModulesKey, ModulesKey> keys) {
    // create the builtin modules
    String[] builtins = getBuiltins();
    if (builtins != null) {
        for (int i = 0; i < builtins.length; i++) {
            String name = builtins[i];
            final ModulesKey k = new ModulesKey(name, null);
            keys.put(k, k);
        }
    }
    super.onChangePythonpath(keys);
}
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