private DocumentEntry CreateDocument(ModuleCreationOptions mco)
        {
            IDocument document;

            switch (mco.ModuleType)
            {
            case ModuleType.Compiled when TryAddModulePath(mco):
                document = new CompiledPythonModule(mco.ModuleName, ModuleType.Compiled, mco.FilePath, mco.Stub, _services);

                break;

            case ModuleType.CompiledBuiltin:
                document = new CompiledBuiltinPythonModule(mco.ModuleName, mco.Stub, _services);
                break;

            case ModuleType.User:
                TryAddModulePath(mco);
                document = new PythonModule(mco, _services);
                break;

            case ModuleType.Library when TryAddModulePath(mco):
                document = new PythonModule(mco, _services);

                break;

            default:
                throw new InvalidOperationException($"CreateDocument does not support module type {mco.ModuleType}");
            }

            var entry = new DocumentEntry(document);

            _documentsByUri[document.Uri] = entry;
            return(entry);
        }
        private IDocument CreateDocument(ModuleCreationOptions mco)
        {
            IDocument document;

            switch (mco.ModuleType)
            {
            case ModuleType.Stub:
                document = new StubPythonModule(mco.ModuleName, mco.FilePath, _services);
                break;

            case ModuleType.Compiled:
                document = new CompiledPythonModule(mco.ModuleName, ModuleType.Compiled, mco.FilePath, mco.Stub, _services);
                break;

            case ModuleType.CompiledBuiltin:
                document = new CompiledBuiltinPythonModule(mco.ModuleName, mco.Stub, _services);
                break;

            case ModuleType.User:
            case ModuleType.Library:
                document = new PythonModule(mco, _services);
                break;

            default:
                throw new InvalidOperationException($"CreateDocument does not support module type {mco.ModuleType}");
            }

            _documentsByUri[document.Uri]    = document;
            _documentsByName[mco.ModuleName] = document;

            _moduleResolution.AddModulePath(document.FilePath);
            return(OpenDocument(document, mco.LoadOptions));
        }
Beispiel #3
0
        protected override IPythonModule CreateModule(string name)
        {
            var moduleImport = CurrentPathResolver.GetModuleImportFromModuleName(name);

            if (moduleImport == null)
            {
                _log?.Log(TraceEventType.Verbose, "Import not found: ", name);
                return(null);
            }

            var module = GetRdt().GetDocument(moduleImport.FullName);

            if (module != null)
            {
                GetRdt().LockDocument(module.Uri);
                return(module);
            }

            // If there is a stub, make sure it is loaded and attached
            // First check stub next to the module.
            if (!TryCreateModuleStub(name, moduleImport.ModulePath, out var stub))
            {
                // If nothing found, try Typeshed.
                stub = _interpreter.TypeshedResolution.GetOrLoadModule(moduleImport.IsBuiltin ? name : moduleImport.FullName);
            }

            // If stub is created and its path equals to module, return that stub as module
            if (stub != null && stub.FilePath.PathEquals(moduleImport.ModulePath))
            {
                return(stub);
            }

            if (moduleImport.IsBuiltin)
            {
                _log?.Log(TraceEventType.Verbose, "Create built-in compiled (scraped) module: ", name, Configuration.InterpreterPath);
                module = new CompiledBuiltinPythonModule(name, stub, _services);
            }
            else if (moduleImport.IsCompiled)
            {
                _log?.Log(TraceEventType.Verbose, "Create compiled (scraped): ", moduleImport.FullName, moduleImport.ModulePath, moduleImport.RootPath);
                module = new CompiledPythonModule(moduleImport.FullName, ModuleType.Compiled, moduleImport.ModulePath, stub, _services);
            }
            else
            {
                _log?.Log(TraceEventType.Verbose, "Import: ", moduleImport.FullName, moduleImport.ModulePath);
                // Module inside workspace == user code.

                var mco = new ModuleCreationOptions {
                    ModuleName = moduleImport.FullName,
                    ModuleType = moduleImport.IsLibrary ? ModuleType.Library : ModuleType.User,
                    FilePath   = moduleImport.ModulePath,
                    Stub       = stub
                };
                module = GetRdt().AddModule(mco);
            }

            return(module);
        }