Search in sources :

Example 1 with EmptyModule

use of org.python.pydev.ast.codecompletion.revisited.modules.EmptyModule 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 2 with EmptyModule

use of org.python.pydev.ast.codecompletion.revisited.modules.EmptyModule in project Pydev by fabioz.

the class SystemModulesManager method getBuiltinModule.

@Override
public AbstractModule getBuiltinModule(String name, boolean dontSearchInit, IModuleRequestState moduleRequest) {
    AbstractModule n = null;
    // check for supported builtins these don't have files associated.
    // they are the first to be passed because the user can force a module to be builtin, because there
    // is some information that is only useful when you have builtins, such as os and wxPython (those can
    // be source modules, but they have so much runtime info that it is almost impossible to get useful information
    // from statically analyzing them).
    String[] builtins = getBuiltins();
    if (builtins == null || this.info == null) {
        // still on startup
        return null;
    }
    // for temporary access (so that we don't generate many instances of it)
    ModulesKey keyForCacheAccess = new ModulesKey(null, null);
    // A different choice for users that want more complete information on the libraries they're dealing
    // with is using predefined modules. Those will
    File predefinedModule = this.info.getPredefinedModule(name, moduleRequest);
    boolean found = predefinedModule != null && predefinedModule.exists();
    if (!found && !name.endsWith(".__init__")) {
        final String nameWithInit = name + ".__init__";
        predefinedModule = this.info.getPredefinedModule(nameWithInit, moduleRequest);
        found = predefinedModule != null && predefinedModule.exists();
        if (found) {
            name = nameWithInit;
        }
    }
    if (found) {
        keyForCacheAccess.name = name;
        keyForCacheAccess.file = predefinedModule;
        n = cachePredefined.getObj(keyForCacheAccess, this);
        if ((n instanceof PredefinedSourceModule)) {
            PredefinedSourceModule predefinedSourceModule = (PredefinedSourceModule) n;
            if (predefinedSourceModule.isSynched()) {
                return n;
            }
        // otherwise (not PredefinedSourceModule or not synched), just keep going to create
        // it as a predefined source module
        }
        boolean tryToParse = true;
        Long lastModified = null;
        if (predefinedFilesNotParsedToTimestamp == null) {
            predefinedFilesNotParsedToTimestamp = new HashMap<File, Long>();
        } else {
            Long lastTimeChanged = predefinedFilesNotParsedToTimestamp.get(predefinedModule);
            if (lastTimeChanged != null) {
                lastModified = FileUtils.lastModified(predefinedModule);
                if (lastTimeChanged.equals(lastModified)) {
                    tryToParse = false;
                } else {
                    predefinedFilesNotParsedToTimestamp.remove(predefinedModule);
                }
            }
        }
        if (tryToParse) {
            IDocument doc;
            try {
                doc = FileUtilsFileBuffer.getDocFromFile(predefinedModule);
                IGrammarVersionProvider provider = new IGrammarVersionProvider() {

                    @Override
                    public int getGrammarVersion() throws MisconfigurationException {
                        // Always Python 3.0 here
                        return IGrammarVersionProvider.LATEST_GRAMMAR_PY3_VERSION;
                    }

                    @Override
                    public AdditionalGrammarVersionsToCheck getAdditionalGrammarVersions() throws MisconfigurationException {
                        return null;
                    }
                };
                ParseOutput obj = PyParser.reparseDocument(new PyParser.ParserInfo(doc, provider, name, predefinedModule));
                if (obj.error != null) {
                    if (lastModified == null) {
                        lastModified = FileUtils.lastModified(predefinedModule);
                    }
                    predefinedFilesNotParsedToTimestamp.put(predefinedModule, lastModified);
                    Log.log("Unable to parse: " + predefinedModule, obj.error);
                } else if (obj.ast != null) {
                    SimpleNode ast = (SimpleNode) obj.ast;
                    if ("builtins".equals(name) || "__builtin__".equals(name)) {
                        // None/False/True must be added as they're not there by default.
                        if (ast instanceof Module) {
                            Module module = (Module) ast;
                            PyAstFactory astFactory = new PyAstFactory(new AdapterPrefs("\n", info.getModulesManager().getNature()));
                            module.body = ArrayUtils.concatArrays(module.body, new stmtType[] { astFactory.createAssign(astFactory.createStoreName("None"), astFactory.createNone()), astFactory.createAssign(astFactory.createStoreName("False"), astFactory.createFalse()), astFactory.createAssign(astFactory.createStoreName("True"), astFactory.createTrue()), astFactory.createAssign(astFactory.createStoreName("__builtins__"), astFactory.createName("Any")) });
                        }
                    }
                    n = new PredefinedSourceModule(name, predefinedModule, ast, obj.error);
                    cachePredefined.add(keyForCacheAccess, n, this);
                    // doAddSingleModule(keyForCacheAccess, n);
                    return n;
                }
            // keep on going
            } catch (Throwable e) {
                Log.log(e);
            }
        }
    }
    boolean foundStartingWithBuiltin = false;
    FastStringBuffer buffer = null;
    for (int i = 0; i < builtins.length; i++) {
        String forcedBuiltin = builtins[i];
        if (name.startsWith(forcedBuiltin)) {
            if (name.length() > forcedBuiltin.length() && name.charAt(forcedBuiltin.length()) == '.') {
                foundStartingWithBuiltin = true;
                keyForCacheAccess.name = name;
                n = cache.getObj(keyForCacheAccess, this);
                if (n == null && dontSearchInit == false) {
                    if (buffer == null) {
                        buffer = new FastStringBuffer();
                    } else {
                        buffer.clear();
                    }
                    keyForCacheAccess.name = buffer.append(name).append(".__init__").toString();
                    n = cache.getObj(keyForCacheAccess, this);
                }
                if (n instanceof EmptyModule || n instanceof SourceModule) {
                    // it is actually found as a source module, so, we have to 'coerce' it to a compiled module
                    n = new CompiledModule(name, this, this.getNature());
                    doAddSingleModule(new ModulesKey(n.getName(), null), n);
                    return n;
                }
            }
            if (name.equals(forcedBuiltin)) {
                keyForCacheAccess.name = name;
                n = cache.getObj(keyForCacheAccess, this);
                if (n == null || n instanceof EmptyModule || n instanceof SourceModule) {
                    // still not created or not defined as compiled module (as it should be)
                    n = new CompiledModule(name, this, this.getNature());
                    doAddSingleModule(new ModulesKey(n.getName(), null), n);
                    return n;
                }
            }
            if (n instanceof CompiledModule) {
                return n;
            }
        }
    }
    if (foundStartingWithBuiltin) {
        if (builtinsNotConsidered.getObj(name) != null) {
            return null;
        }
        // ok, just add it if it is some module that actually exists
        n = new CompiledModule(name, this, this.getNature());
        TokensList globalTokens = n.getGlobalTokens();
        // the module to see its return values (because that's slow)
        if (globalTokens.size() > 0 && contains(globalTokens, "__file__")) {
            doAddSingleModule(new ModulesKey(name, null), n);
            return n;
        } else {
            builtinsNotConsidered.add(name, name);
            return null;
        }
    }
    return null;
}
Also used : ParseOutput(org.python.pydev.shared_core.parsing.BaseParser.ParseOutput) SimpleNode(org.python.pydev.parser.jython.SimpleNode) PredefinedSourceModule(org.python.pydev.ast.codecompletion.revisited.modules.PredefinedSourceModule) PyAstFactory(org.python.pydev.parser.jython.ast.factory.PyAstFactory) EmptyModule(org.python.pydev.ast.codecompletion.revisited.modules.EmptyModule) TokensList(org.python.pydev.core.TokensList) PredefinedSourceModule(org.python.pydev.ast.codecompletion.revisited.modules.PredefinedSourceModule) SourceModule(org.python.pydev.ast.codecompletion.revisited.modules.SourceModule) FastStringBuffer(org.python.pydev.shared_core.string.FastStringBuffer) IGrammarVersionProvider(org.python.pydev.core.IGrammarVersionProvider) PyParser(org.python.pydev.parser.PyParser) AbstractModule(org.python.pydev.ast.codecompletion.revisited.modules.AbstractModule) ModulesKey(org.python.pydev.core.ModulesKey) AdapterPrefs(org.python.pydev.parser.jython.ast.factory.AdapterPrefs) AbstractModule(org.python.pydev.ast.codecompletion.revisited.modules.AbstractModule) IModule(org.python.pydev.core.IModule) Module(org.python.pydev.parser.jython.ast.Module) PredefinedSourceModule(org.python.pydev.ast.codecompletion.revisited.modules.PredefinedSourceModule) SourceModule(org.python.pydev.ast.codecompletion.revisited.modules.SourceModule) EmptyModule(org.python.pydev.ast.codecompletion.revisited.modules.EmptyModule) CompiledModule(org.python.pydev.ast.codecompletion.revisited.modules.CompiledModule) CompiledModule(org.python.pydev.ast.codecompletion.revisited.modules.CompiledModule) File(java.io.File) IDocument(org.eclipse.jface.text.IDocument)

Aggregations

IDocument (org.eclipse.jface.text.IDocument)2 AbstractModule (org.python.pydev.ast.codecompletion.revisited.modules.AbstractModule)2 CompiledModule (org.python.pydev.ast.codecompletion.revisited.modules.CompiledModule)2 EmptyModule (org.python.pydev.ast.codecompletion.revisited.modules.EmptyModule)2 SourceModule (org.python.pydev.ast.codecompletion.revisited.modules.SourceModule)2 IModule (org.python.pydev.core.IModule)2 ModulesKey (org.python.pydev.core.ModulesKey)2 Module (org.python.pydev.parser.jython.ast.Module)2 File (java.io.File)1 IOException (java.io.IOException)1 EmptyModuleForFolder (org.python.pydev.ast.codecompletion.revisited.modules.EmptyModuleForFolder)1 EmptyModuleForZip (org.python.pydev.ast.codecompletion.revisited.modules.EmptyModuleForZip)1 InitFromDirModule (org.python.pydev.ast.codecompletion.revisited.modules.InitFromDirModule)1 PredefinedSourceModule (org.python.pydev.ast.codecompletion.revisited.modules.PredefinedSourceModule)1 IGrammarVersionProvider (org.python.pydev.core.IGrammarVersionProvider)1 MisconfigurationException (org.python.pydev.core.MisconfigurationException)1 TokensList (org.python.pydev.core.TokensList)1 PyParser (org.python.pydev.parser.PyParser)1 SimpleNode (org.python.pydev.parser.jython.SimpleNode)1 AdapterPrefs (org.python.pydev.parser.jython.ast.factory.AdapterPrefs)1