Esempio n. 1
0
        internal PythonModule(ModuleCreationOptions creationOptions, IServiceContainer services)
            : this(creationOptions.ModuleName, creationOptions.ModuleType, services)
        {
            Check.ArgumentNotNull(nameof(services), services);

            FileSystem = services.GetService <IFileSystem>();
            Location   = new LocationInfo(creationOptions.FilePath, creationOptions.Uri, 1, 1);

            var uri = creationOptions.Uri;

            if (uri == null && !string.IsNullOrEmpty(creationOptions.FilePath))
            {
                Uri.TryCreate(creationOptions.FilePath, UriKind.Absolute, out uri);
            }
            Uri      = uri;
            FilePath = creationOptions.FilePath ?? uri?.LocalPath;
            Stub     = creationOptions.Stub;
            if (Stub is PythonModule stub && ModuleType != ModuleType.Stub)
            {
                stub.PrimaryModule = this;
            }

            if (ModuleType == ModuleType.Specialized || ModuleType == ModuleType.Unresolved)
            {
                ContentState = State.Analyzed;
            }
            InitializeContent(creationOptions.Content, 0);
        }
        public async Task <IDocument> ImportFromCacheAsync(string name, CancellationToken cancellationToken)
        {
            if (string.IsNullOrEmpty(ModuleCachePath))
            {
                return(null);
            }

            var cache = GetCacheFilePath("python.{0}.pyi".FormatInvariant(name));

            if (!_fs.FileExists(cache))
            {
                cache = GetCacheFilePath("python._{0}.pyi".FormatInvariant(name));
                if (!_fs.FileExists(cache))
                {
                    cache = GetCacheFilePath("{0}.pyi".FormatInvariant(name));
                    if (!_fs.FileExists(cache))
                    {
                        return(null);
                    }
                }
            }

            var rdt = _services.GetService <IRunningDocumentTable>();
            var mco = new ModuleCreationOptions {
                ModuleName  = name,
                ModuleType  = ModuleType.Stub,
                FilePath    = cache,
                LoadOptions = ModuleLoadOptions.Analyze
            };
            var module = rdt.AddModule(mco);

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

            return(module);
        }
        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);
        }
        internal PythonModule(ModuleCreationOptions creationOptions, IServiceContainer services)
            : this(creationOptions.ModuleName, creationOptions.ModuleType, services)
        {
            Check.ArgumentNotNull(nameof(services), services);

            FileSystem = services.GetService <IFileSystem>();
            Location   = new LocationInfo(creationOptions.FilePath, creationOptions.Uri, 1, 1);

            var uri = creationOptions.Uri;

            if (uri == null && !string.IsNullOrEmpty(creationOptions.FilePath))
            {
                Uri.TryCreate(creationOptions.FilePath, UriKind.Absolute, out uri);
            }
            Uri      = uri;
            FilePath = creationOptions.FilePath ?? uri?.LocalPath;
            Stub     = creationOptions.Stub;

            InitializeContent(creationOptions.Content, creationOptions.LoadOptions);
        }