Ejemplo n.º 1
0
        /// <summary>
        /// Creates a new interpreter factory with the specified options. This
        /// interpreter always includes a cached completion database.
        /// </summary>
        public static IPythonInterpreterFactory CreateInterpreterFactory(InterpreterConfiguration configuration, InterpreterFactoryCreationOptions options = null)
        {
            options = options?.Clone() ?? new InterpreterFactoryCreationOptions();

            if (string.IsNullOrEmpty(options.DatabasePath))
            {
                options.DatabasePath = Path.Combine(
                    PythonTypeDatabase.CompletionDatabasePath,
                    GetRelativePathForConfigurationId(configuration.Id)
                    );
            }

            if (options.NoDatabase)
            {
                // Use the (currenty experimental) non-database backed factory
                var fact = new Ast.AstPythonInterpreterFactory(configuration, options);
                return(fact);
            }
            else
            {
                // Use the database-backed factory
                var fact = new CPythonInterpreterFactory(configuration, options);
                if (options.WatchFileSystem)
                {
                    fact.BeginRefreshIsCurrent();
                }
                return(fact);
            }
        }
Ejemplo n.º 2
0
        private async Task <IReadOnlyDictionary <string, string> > GetUserSearchPathPackagesAsync(CancellationToken cancellationToken)
        {
            _log?.Log(TraceLevel.Verbose, "GetUserSearchPathPackagesAsync");
            var ussp = _userSearchPathPackages;

            if (ussp == null)
            {
                IReadOnlyList <string> usp;
                lock (_userSearchPathsLock) {
                    usp  = _userSearchPaths;
                    ussp = _userSearchPathPackages;
                }
                if (ussp != null || usp == null || !usp.Any())
                {
                    return(ussp);
                }

                _log?.Log(TraceLevel.Verbose, "GetImportableModulesAsync");
                var requireInitPy = ModulePath.PythonVersionRequiresInitPyFiles(_factory.Configuration.Version);
                ussp = await AstPythonInterpreterFactory.GetImportableModulesAsync(usp, requireInitPy, cancellationToken);

                lock (_userSearchPathsLock) {
                    if (_userSearchPathPackages == null)
                    {
                        _userSearchPathPackages = ussp;
                    }
                    else
                    {
                        ussp = _userSearchPathPackages;
                    }
                }
            }

            _log?.Log(TraceLevel.Verbose, "GetUserSearchPathPackagesAsync", ussp.Keys.Cast <object>().ToArray());
            return(ussp);
        }
Ejemplo n.º 3
0
        public void Imported(IModuleContext context)
        {
            if (_scraped)
            {
                return;
            }
            _scraped = true;

            var interp = context as AstPythonInterpreter;
            var fact   = interp?.Factory;

            if (fact == null || !File.Exists(fact.Configuration.InterpreterPath))
            {
                return;
            }

            ModulePath mp = AstPythonInterpreterFactory.FindModule(fact, _filePath);

            if (string.IsNullOrEmpty(mp.FullName))
            {
                return;
            }

            var sm = PythonToolsInstallPath.TryGetFile("scrape_module.py", GetType().Assembly);

            if (!File.Exists(sm))
            {
                return;
            }

            Stream code = null;

            using (var p = ProcessOutput.RunHiddenAndCapture(
                       fact.Configuration.InterpreterPath, "-E", sm, mp.LibraryPath, mp.ModuleName
                       )) {
                p.Wait();
                if (p.ExitCode == 0)
                {
                    var ms = new MemoryStream();
                    code = ms;
                    using (var sw = new StreamWriter(ms, Encoding.UTF8, 4096, true)) {
                        foreach (var line in p.StandardOutputLines)
                        {
                            sw.WriteLine(line);
                        }
                    }
                }
            }

            if (code == null)
            {
                return;
            }

            PythonAst ast;

            code.Seek(0, SeekOrigin.Begin);
            using (var sr = new StreamReader(code, Encoding.UTF8))
                using (var parser = Parser.CreateParser(sr, fact.GetLanguageVersion())) {
                    ast = parser.ParseFile();
                }

            lock (_members) {
                var walker = new AstAnalysisWalker(interp, ast, this, _filePath, _members, false);
                ast.Walk(walker);
            }
        }