Example #1
0
        public IList <string> GetModuleNames()
        {
            var ussp = GetUserSearchPathPackages();
            var ssp  = _factory.GetImportableModules();

            IEnumerable <string> names = null;

            if (ussp != null)
            {
                names = ussp.Keys;
            }
            if (ssp != null)
            {
                names = names?.Union(ssp.Keys) ?? ssp.Keys;
            }

            return(names.MaybeEnumerate().ToArray());
        }
Example #2
0
        private IReadOnlyDictionary <string, string> GetUserSearchPathPackages()
        {
            var ussp = _userSearchPathPackages;

            if (ussp == null)
            {
                lock (_userSearchPathsLock) {
                    if (ussp == null && _userSearchPaths != null && _userSearchPaths.Any())
                    {
                        ussp = AstPythonInterpreterFactory.GetImportableModules(_userSearchPaths);
                    }
                }
            }
            return(ussp);
        }
Example #3
0
        private async Task <IPythonModule> ImportFromSearchPathsAsyncWorker(string name)
        {
            var mmp = FindModuleInSearchPath(
                _userSearchPaths,
                await GetUserSearchPathPackagesAsync().ConfigureAwait(false),
                name
                );

            if (mmp.HasValue)
            {
                lock (_userSearchPathsLock) {
                    if (_userSearchPathImported == null)
                    {
                        _userSearchPathImported = new HashSet <string>();
                    }
                    _userSearchPathImported.Add(name);
                }
            }
            else
            {
                _log?.Log(TraceLevel.Verbose, "FindModule", name, "system");
                var sp = await _factory.GetSearchPathsAsync().ConfigureAwait(false);

                _log?.Log(TraceLevel.Verbose, "FindModule", name, "system", string.Join(", ", sp));
                var mods = await _factory.GetImportableModulesAsync().ConfigureAwait(false);

                mmp = FindModuleInSearchPath(sp, mods, name);
            }

            if (!mmp.HasValue)
            {
                _log?.Log(TraceLevel.Verbose, "ImportNotFound", name);
                return(null);
            }

            var mp = mmp.Value;

#if USE_TYPESHED
            lock (_typeShedPathsLock) {
                if (_typeShedPaths == null)
                {
                    var typeshed = FindModuleInSearchPath(_factory.GetSearchPaths(), _factory.GetImportableModules(), "typeshed");
                    if (typeshed.HasValue)
                    {
                        _typeShedPaths = GetTypeShedPaths(PathUtils.GetParent(typeshed.Value.SourceFile)).ToArray();
                    }
                    else
                    {
                        _typeShedPaths = Array.Empty <string>();
                    }
                }
                if (_typeShedPaths.Any())
                {
                    var mtsp = FindModuleInSearchPath(_typeShedPaths, null, mp.FullName);
                    if (mtsp.HasValue)
                    {
                        mp = mtsp.Value;
                    }
                }
            }
#endif

            if (mp.IsCompiled)
            {
                _log?.Log(TraceLevel.Verbose, "ImportScraped", mp.FullName, _factory.FastRelativePath(mp.SourceFile));
                return(new AstScrapedPythonModule(mp.FullName, mp.SourceFile));
            }

            _log?.Log(TraceLevel.Verbose, "Import", mp.FullName, _factory.FastRelativePath(mp.SourceFile));
            return(AstPythonModule.FromFile(this, mp.SourceFile, _factory.LanguageVersion, mp.FullName));
        }