def _handle_star(alias): try: names = mod.lookup_star() except SystemError as ex: logging.error(str(ex)) return for name in names: # don't visit until we have already collected all possible symbols in the parent if mod not in self.visited: logging.info("Skipping missing: {}".format(pkg_or_mod_name + '.*')) self.missing.add(pkg_or_mod_name + '.*') return # only add if we have not yet added it if not self.context.has_symbol(name): ref = NameRef(mod.lookup(name)) ref.parent = self.context self.context.add_symbol(name, ref, ast=alias.name)
def visit_ImportFrom(self, node): def _handle_star(alias): try: names = mod.lookup_star() except SystemError as ex: logging.error(str(ex)) return for name in names: # don't visit until we have already collected all possible symbols in the parent if mod not in self.visited: logging.info("Skipping missing: {}".format(pkg_or_mod_name + '.*')) self.missing.add(pkg_or_mod_name + '.*') return # only add if we have not yet added it if not self.context.has_symbol(name): ref = NameRef(mod.lookup(name)) ref.parent = self.context self.context.add_symbol(name, ref, ast=alias.name) # query the module (keeping in mind that this may be a package we want) pkg_or_mod_name = '.' * node.level + str(node.module) mod = self.module.refs.get(pkg_or_mod_name, None) node.module.hl = mod # NOTE: any inspection of main should be limited to runtime # e.g. in pkg_resources, inspection of __requires__ with "from __main__ import __requires__" if pkg_or_mod_name == '__main__': return # if we have not visited the target module, skip it until we have if mod is None: logging.info("Skipping missing: {}".format(pkg_or_mod_name + '.*')) self.missing.add(pkg_or_mod_name + '.*') return for alias in node.names: if str(alias.name) == '*': _handle_star(alias) continue # Note: the queried name may be in the given module (maybe an __init__), or # it may be a submodule in the package of that name if mod.has_symbol(str(alias.name)): sym = mod.lookup(str(alias.name)) elif mod.filename.endswith('__init__.py'): # ensure we have actually visited the real target before we go looking for submodules -- # Note: if we _are_ '.' ourself, then we can continue here without issues if not self.module.filename.endswith('__init__.py') and mod not in self.visited: logging.info("Skipping missing from: {}".format(pkg_or_mod_name + '.' + str(alias.name))) self.missing.add(pkg_or_mod_name + '.' + str(alias.name)) return # filename will be one of either (1) mod/name.py or (2) mod/name/__init__.py real_filename_1 = mod.filename[:-11] + str(alias.name).replace('.', '/') + '.py' real_filename_2 = mod.filename[:-11] + str(alias.name).replace('.', '/') + '/' + '__init__.py' try: sym = self.project.modules_by_path[real_filename_1] except KeyError: try: sym = self.project.modules_by_path[real_filename_2] except KeyError: abs_modname = mod.python_name + '.' + str(alias.name) try: sym = self.project.modules_by_absname[abs_modname] except KeyError: logging.info("Skipping missing from: {}".format(pkg_or_mod_name + '.' + str(alias.name))) self.missing.add(pkg_or_mod_name + '.' + str(alias.name)) return else: logging.info("Skipping missing from: {}".format(pkg_or_mod_name + '.' + str(alias.name))) self.missing.add(pkg_or_mod_name + '.' + str(alias.name)) continue ref = NameRef(sym) ref.parent = self.context if alias.asname: #self.visit(alias.asname) self.context.add_symbol(str(alias.asname), ref, ast=alias.asname) alias.asname.hl = ref alias.name.hl = ref else: #self.visit(alias.name) self.context.add_symbol(str(alias.name), ref, ast=alias.name) alias.name.hl = ref