Exemple #1
0
        private bool TryCreateStubModule(string name, out IPythonModule module)
        {
            module = null;
            var moduleImport = CurrentPathResolver.GetModuleImportFromModuleName(name);

            if (moduleImport != null)
            {
                if (moduleImport.IsCompiled)
                {
                    Log?.Log(TraceEventType.Warning, "Unsupported native module in stubs", moduleImport.FullName, moduleImport.ModulePath);
                    return(false);
                }

                module = new StubPythonModule(moduleImport.FullName, moduleImport.ModulePath, true, Services);
                return(true);
            }

            var i = name.IndexOf('.');

            if (i == 0)
            {
                Debug.Fail("Invalid module name");
                return(false);
            }

            var stubPath = CurrentPathResolver.GetPossibleModuleStubPaths(name).FirstOrDefault(p => FileSystem.FileExists(p));

            if (stubPath != null)
            {
                module = new StubPythonModule(name, stubPath, true, Services);
                return(true);
            }
            return(false);
        }
Exemple #2
0
        protected override IPythonModule CreateModule(string name)
        {
            var moduleImport = CurrentPathResolver.GetModuleImportFromModuleName(name);

            if (moduleImport != default)
            {
                if (moduleImport.IsCompiled)
                {
                    Log?.Log(TraceEventType.Warning, "Unsupported native module in stubs", moduleImport.FullName, moduleImport.ModulePath);
                    return(null);
                }
                return(new StubPythonModule(moduleImport.FullName, moduleImport.ModulePath, true, Services));
            }

            var i = name.IndexOf('.');

            if (i == 0)
            {
                Debug.Fail("Invalid module name");
                return(null);
            }

            var stubPath = CurrentPathResolver.GetPossibleModuleStubPaths(name).FirstOrDefault(p => FileSystem.FileExists(p));

            return(stubPath != null ? new StubPythonModule(name, stubPath, true, Services) : null);
        }
        protected override IPythonModule CreateModule(string name)
        {
            var mp = FindModuleInSearchPath(_typeStubPaths, null, name);

            if (mp != null)
            {
                if (mp.Value.IsCompiled)
                {
                    _log?.Log(TraceEventType.Warning, "Unsupported native module in stubs", mp.Value.FullName, mp.Value.SourceFile);
                    return(null);
                }
                return(new StubPythonModule(mp.Value.FullName, mp.Value.SourceFile, true, _services));
            }

            var i = name.IndexOf('.');

            if (i == 0)
            {
                Debug.Fail("Invalid module name");
                return(null);
            }

            var stubPath = CurrentPathResolver.GetPossibleModuleStubPaths(name).FirstOrDefault(p => _fs.FileExists(p));

            return(stubPath != null ? new StubPythonModule(name, stubPath, true, _services) : null);
        }
        private async Task <IPythonModule> ImportFromTypeStubsAsync(string name, CancellationToken cancellationToken = default)
        {
            var mp = FindModuleInSearchPath(_typeStubPaths, null, name);

            if (mp != null)
            {
                if (mp.Value.IsCompiled)
                {
                    _log?.Log(TraceEventType.Warning, "Unsupported native module in stubs", mp.Value.FullName, mp.Value.SourceFile);
                    return(null);
                }
                return(await CreateStubModuleAsync(mp.Value.FullName, mp.Value.SourceFile, cancellationToken));
            }

            var i = name.IndexOf('.');

            if (i == 0)
            {
                Debug.Fail("Invalid module name");
                return(null);
            }

            var stubPath = CurrentPathResolver.GetPossibleModuleStubPaths(name).FirstOrDefault(p => _fs.FileExists(p));

            return(stubPath != null ? await CreateStubModuleAsync(name, stubPath, cancellationToken) : null);
        }
Exemple #5
0
        /// <summary>
        /// Provides ability to specialize module by replacing module import by
        /// <see cref="IPythonModule"/> implementation in code. Real module
        /// content is loaded and analyzed only for class/functions definitions
        /// so the original documentation can be extracted.
        /// </summary>
        /// <param name="name">Module to specialize.</param>
        /// <param name="specializationConstructor">Specialized module constructor.</param>
        /// <returns>Original (library) module loaded as stub.</returns>
        public IPythonModule SpecializeModule(string name, Func <string, IPythonModule> specializationConstructor)
        {
            var import = CurrentPathResolver.GetModuleImportFromModuleName(name);
            var module = specializationConstructor(import?.ModulePath);

            _specialized[name] = module;
            return(module);
        }
        protected override IPythonModule CreateModule(string name)
        {
            var moduleImport = CurrentPathResolver.GetModuleImportFromModuleName(name);

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

            if (moduleImport.ModulePath != null)
            {
                var module = GetRdt().GetDocument(new Uri(moduleImport.ModulePath));
                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);
                return(new CompiledBuiltinPythonModule(name, stub, _services));
            }

            if (moduleImport.IsCompiled)
            {
                _log?.Log(TraceEventType.Verbose, "Create compiled (scraped): ", moduleImport.FullName, moduleImport.ModulePath, moduleImport.RootPath);
                return(new CompiledPythonModule(moduleImport.FullName, ModuleType.Compiled, moduleImport.ModulePath, stub, _services));
            }

            _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
            };

            return(GetRdt().AddModule(mco));
        }
        /// <summary>
        /// Provides ability to specialize module by replacing module import by
        /// <see cref="IPythonModule"/> implementation in code. Real module
        /// content is loaded and analyzed only for class/functions definitions
        /// so the original documentation can be extracted.
        /// </summary>
        /// <param name="name">Module to specialize.</param>
        /// <param name="specializationConstructor">Specialized module constructor.</param>
        /// <param name="replaceExisting">Replace existing loaded module, if any.</param>
        /// <returns>Specialized module.</returns>
        public IPythonModule SpecializeModule(string name, Func <string, IPythonModule> specializationConstructor, bool replaceExisting = false)
        {
            var import = CurrentPathResolver.GetModuleImportFromModuleName(name);
            var module = specializationConstructor(import?.ModulePath);

            _specialized[name] = module;

            if (replaceExisting)
            {
                Modules.TryRemove(name, out _);
            }
            return(module);
        }
        /// <summary>
        /// Provides ability to specialize module by replacing module import by
        /// <see cref="IPythonModule"/> implementation in code. Real module
        /// content is loaded and analyzed only for class/functions definitions
        /// so the original documentation can be extracted.
        /// </summary>
        /// <param name="name">Module to specialize.</param>
        /// <param name="specializationConstructor">Specialized module constructor.</param>
        /// <param name="cancellationToken">Cancellation token.</param>
        /// <returns>Original (library) module loaded as stub.</returns>
        public async Task <IPythonModule> SpecializeModuleAsync(string name, Func <string, IPythonModule> specializationConstructor, CancellationToken cancellationToken = default)
        {
            var import = CurrentPathResolver.GetModuleImportFromModuleName(name);

            if (!string.IsNullOrEmpty(import?.ModulePath))
            {
                var module = specializationConstructor(import.ModulePath);
                _modules[name] = module;
                await module.LoadAndAnalyzeAsync(cancellationToken);

                return(module);
            }
            return(null);
        }
        private bool TryCreateModuleStub(string name, string modulePath, out IPythonModule module)
        {
            // First check stub next to the module.
            if (!string.IsNullOrEmpty(modulePath))
            {
                var pyiPath = Path.ChangeExtension(modulePath, "pyi");
                if (FileSystem.FileExists(pyiPath))
                {
                    module = new StubPythonModule(name, pyiPath, false, Services);
                    return(true);
                }
            }

            // Try location of stubs that are in a separate folder next to the package.
            var stubPath = CurrentPathResolver.GetPossibleModuleStubPaths(name).FirstOrDefault(p => FileSystem.FileExists(p));

            module = !string.IsNullOrEmpty(stubPath) ? new StubPythonModule(name, stubPath, false, Services) : null;
            return(module != null);
        }
        private async Task <IPythonModule> ImportFromSearchPathsAsync(string name, CancellationToken cancellationToken)
        {
            var moduleImport = CurrentPathResolver.GetModuleImportFromModuleName(name);

            if (moduleImport == null)
            {
                _log?.Log(TraceEventType.Verbose, "Import not found: ", name);
                return(null);
            }
            // If there is a stub, make sure it is loaded and attached
            var stub = await ImportFromTypeStubsAsync(moduleImport.IsBuiltin?name : moduleImport.FullName, cancellationToken);

            IPythonModule module;

            if (moduleImport.IsBuiltin)
            {
                _log?.Log(TraceEventType.Verbose, "Import built-in compiled (scraped) module: ", name, Configuration.InterpreterPath);
                module = new CompiledBuiltinPythonModule(name, stub, _services);
            }
            else if (moduleImport.IsCompiled)
            {
                _log?.Log(TraceEventType.Verbose, "Import 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);
                var rdt = _services.GetService <IRunningDocumentTable>();
                // TODO: handle user code and library module separately.
                var mco = new ModuleCreationOptions {
                    ModuleName  = moduleImport.FullName,
                    ModuleType  = ModuleType.Library,
                    FilePath    = moduleImport.ModulePath,
                    Stub        = stub,
                    LoadOptions = ModuleLoadOptions.Analyze
                };
                module = rdt.AddModule(mco);
            }

            await module.LoadAndAnalyzeAsync(cancellationToken).ConfigureAwait(false);

            return(module);
        }
        protected override IPythonModule CreateModule(string name)
        {
            var moduleImport = CurrentPathResolver.GetModuleImportFromModuleName(name);

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

            IPythonModule module;

            if (moduleImport.ModulePath != null)
            {
                module = GetRdt().GetDocument(new Uri(moduleImport.ModulePath));
                if (module != null)
                {
                    GetRdt().LockDocument(module.Uri);
                    return(module);
                }
            }

            var moduleType = moduleImport.IsBuiltin ? ModuleType.CompiledBuiltin
                : moduleImport.IsCompiled ? ModuleType.Compiled
                : moduleImport.IsLibrary ? ModuleType.Library
                : ModuleType.User;

            var dbs = GetDbService();

            if (dbs != null)
            {
                var sw = Stopwatch.StartNew();
                module = dbs.RestoreModule(name, moduleImport.ModulePath, moduleType);
                sw.Stop();
                if (module != null)
                {
                    Log?.Log(TraceEventType.Verbose, $"Restored from database: {name} in {sw.ElapsedMilliseconds} ms.");
                    Interpreter.ModuleResolution.SpecializeModule(name, x => module, true);
                    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))
            {
                Analyzer.InvalidateAnalysis(stub);
            }
            else
            {
                // 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);
                return(new CompiledBuiltinPythonModule(name, stub, Services));
            }

            if (moduleImport.IsCompiled)
            {
                Log?.Log(TraceEventType.Verbose, "Create compiled (scraped): ", moduleImport.FullName, moduleImport.ModulePath, moduleImport.RootPath);
                return(new CompiledPythonModule(moduleImport.FullName, ModuleType.Compiled, moduleImport.ModulePath, stub, false, Services));
            }

            Log?.Log(TraceEventType.Verbose, "Import: ", moduleImport.FullName, moduleImport.ModulePath);
            // Module inside workspace == user code.

            var mco = new ModuleCreationOptions {
                ModuleName = moduleImport.FullName,
                ModuleType = moduleType,
                FilePath   = moduleImport.ModulePath,
                Stub       = stub
            };

            return(GetRdt().AddModule(mco));
        }