def import_name(self, module_name): """Load a name like 'sys' or 'foo.bar.baz'. Args: module_name: The name of the module. May contain dots. Returns: The parsed pytd, instance of pytd.TypeDeclUnit, or None if we didn't find the module. """ assert "/" not in module_name log.debug("Trying to import %s", module_name) # Builtin modules (but not standard library modules!) take precedence # over modules in PYTHONPATH. mod = pytd_utils.ParsePredefinedPyTD("builtins", module_name, self.python_version) if mod: log.debug("Found builtins %s", module_name) return self._load_file(filename=self.PREFIX + module_name, module_name=module_name, ast=mod) module_name_split = module_name.split(".") for prefix in self.import_drop_prefixes: module_name_split = utils.list_strip_prefix(module_name_split, prefix.split(".")) for searchdir in self.pythonpath: path = os.path.join(searchdir, *module_name_split) if os.path.isdir(path): # TODO(pludemann): need test case (esp. for empty __init__.py) init_ast = self._load_pytd_from_glob(os.path.join(path, "__init__"), module_name) if init_ast is not None: log.debug("Found module %s/__init__ in path %s", module_name, path) return init_ast else: # We allow directories to not have an __init__ file. # The module's empty, but you can still load submodules. return self._create_empty( filename=os.path.join(path, "__init__.pytd"), module_name=module_name) else: file_ast = self._load_pytd_from_glob(path, module_name) if file_ast is not None: log.debug("Found module %s in path %s", module_name, path) return file_ast # The standard library is (typically) at the end of PYTHONPATH. mod = pytd_utils.ParsePredefinedPyTD("stdlib", module_name, self.python_version) if mod: log.debug("Found stdlib %s", module_name) return self._load_file(filename="stdlib:"+module_name, module_name=module_name, ast=mod) elif self.import_error_is_fatal: log.critical("Couldn't import module %s", module_name) # TODO(pludemann): sys.exit(-1) ? else: # TODO(pludemann): suppress warning if main is processing multiple files: log.warn("Couldn't import module %s", module_name) return None
def testListLstrip(self): self.assertEqual([1, 2, 3], utils.list_strip_prefix([1, 2, 3], [])) self.assertEqual([2, 3], utils.list_strip_prefix([1, 2, 3], [1])) self.assertEqual([3], utils.list_strip_prefix([1, 2, 3], [1, 2])) self.assertEqual([], utils.list_strip_prefix([1, 2, 3], [1, 2, 3])) self.assertEqual([1, 2, 3], utils.list_strip_prefix([1, 2, 3], [0, 1, 2, 3])) self.assertEqual([], utils.list_strip_prefix([], [1, 2, 3])) self.assertEqual(list("wellington"), utils.list_strip_prefix( list("newwellington"), list("new")))
def test_list_strip_prefix(self): self.assertEqual([1, 2, 3], utils.list_strip_prefix([1, 2, 3], [])) self.assertEqual([2, 3], utils.list_strip_prefix([1, 2, 3], [1])) self.assertEqual([3], utils.list_strip_prefix([1, 2, 3], [1, 2])) self.assertEqual([], utils.list_strip_prefix([1, 2, 3], [1, 2, 3])) self.assertEqual([1, 2, 3], utils.list_strip_prefix([1, 2, 3], [0, 1, 2, 3])) self.assertEqual([], utils.list_strip_prefix([], [1, 2, 3])) self.assertEqual(list("wellington"), utils.list_strip_prefix( list("newwellington"), list("new"))) self.assertEqual( "a.somewhat.long.path.src2.d3.shrdlu".split("."), utils.list_strip_prefix( "top.a.somewhat.long.path.src2.d3.shrdlu".split("."), "top".split(".")))
def testListStripPrefix(self): self.assertEqual([1, 2, 3], utils.list_strip_prefix([1, 2, 3], [])) self.assertEqual([2, 3], utils.list_strip_prefix([1, 2, 3], [1])) self.assertEqual([3], utils.list_strip_prefix([1, 2, 3], [1, 2])) self.assertEqual([], utils.list_strip_prefix([1, 2, 3], [1, 2, 3])) self.assertEqual([1, 2, 3], utils.list_strip_prefix([1, 2, 3], [0, 1, 2, 3])) self.assertEqual([], utils.list_strip_prefix([], [1, 2, 3])) self.assertEqual(list("wellington"), utils.list_strip_prefix( list("newwellington"), list("new"))) self.assertEqual( "a.somewhat.long.path.src2.d3.shrdlu".split("."), utils.list_strip_prefix( "top.a.somewhat.long.path.src2.d3.shrdlu".split("."), "top".split(".")))
def import_name(self, module_name): """Load a name like 'sys' or 'foo.bar.baz'. Args: module_name: The name of the module. May contain dots. Returns: The parsed pytd, instance of pytd.TypeDeclUnit, or None if we the module wasn't found. """ assert os.sep not in module_name, (os.sep, module_name) log.debug("Trying to import %r", module_name) # Builtin modules (but not standard library modules!) take precedence # over modules in PYTHONPATH. mod = pytd_utils.ParsePredefinedPyTD("builtins", module_name, self.python_version) if mod: log.debug("Found builtins %r", module_name) return self._load_file(filename=self.PREFIX + module_name, module_name=module_name, ast=mod) module_name_split = module_name.split(".") for prefix in self.import_drop_prefixes: module_name_split = utils.list_strip_prefix( module_name_split, prefix.split(".")) file_ast = self._import_file(module_name, module_name_split) if file_ast: return file_ast # The standard library is (typically) at the end of PYTHONPATH. mod = pytd_utils.ParsePredefinedPyTD("stdlib", module_name, self.python_version) if mod: return self._load_file(filename="stdlib:" + module_name, module_name=module_name, ast=mod) else: log.warning( "Couldn't import module %s %r in (path=%r) imports_map: %s", module_name, module_name_split, self.pythonpath, "%d items" % len(self.imports_map) if self.imports_map else "none") if self.imports_map is not None: for short_path, long_path in self.imports_map.items(): log.debug("%r => %r", short_path, long_path) return None
def import_name(self, module_name): """Load a name like 'sys' or 'foo.bar.baz'. Args: module_name: The name of the module. May contain dots. Returns: The parsed pytd, instance of pytd.TypeDeclUnit, or None if we the module wasn't found. """ assert os.sep not in module_name, (os.sep, module_name) log.debug("Trying to import %r", module_name) # Builtin modules (but not standard library modules!) take precedence # over modules in PYTHONPATH. mod = pytd_utils.ParsePredefinedPyTD("builtins", module_name, self.python_version) if mod: log.debug("Found builtins %r", module_name) return self._load_file(filename=self.PREFIX + module_name, module_name=module_name, ast=mod) module_name_split = module_name.split(".") for prefix in self.import_drop_prefixes: module_name_split = utils.list_strip_prefix(module_name_split, prefix.split(".")) file_ast = self._import_file(module_name, module_name_split) if file_ast: return file_ast # The standard library is (typically) at the end of PYTHONPATH. mod = pytd_utils.ParsePredefinedPyTD("stdlib", module_name, self.python_version) if mod: return self._load_file(filename="stdlib:"+module_name, module_name=module_name, ast=mod) else: log.warning( "Couldn't import module %s %r in (path=%r) imports_map: %s", module_name, module_name_split, self.pythonpath, "%d items" % len(self.imports_map) if self.imports_map else "none") if self.imports_map is not None: for short_path, long_path in self.imports_map.items(): log.debug("%r => %r", short_path, long_path) return None
def _import_name(self, module_name): """Load a name like 'sys' or 'foo.bar.baz'. Args: module_name: The name of the module. May contain dots. Returns: The parsed file, instance of pytd.TypeDeclUnit, or None if we the module wasn't found. """ assert os.sep not in module_name, (os.sep, module_name) log.debug("Trying to import %r", module_name) # Builtin modules (but not standard library modules!) take precedence # over modules in PYTHONPATH. mod = self._load_builtin("builtins", module_name) if mod: return mod # We're guaranteed that self.options.import_drop_prefixes is empty if # self.options.imports_map was given, so there's no conflict between the # lookup in self.options.import_drop_prefixes and self.options.imports_map # (which is used by _load_pyi, which is called by _import_file). module_name_split = module_name.split(".") for prefix in self.options.import_drop_prefixes: module_name_split = utils.list_strip_prefix(module_name_split, prefix.split(".")) file_ast = self._import_file(module_name, module_name_split) if file_ast: return file_ast # The standard library is (typically) at the end of PYTHONPATH. mod = self._load_builtin("stdlib", module_name) if mod: return mod log.warning("Couldn't import module %s %r in (path=%r) imports_map: %s", module_name, module_name_split, self.options.pythonpath, "%d items" % len(self.options.imports_map) if self.options.imports_map else "none") if log.isEnabledFor(logging.DEBUG) and self.options.imports_map: for module, path in self.options.imports_map.items(): log.debug("%s -> %s", module, path) return None