Beispiel #1
0
    def visit_Import(self, import_node: ast.Import):
        """
        Includes methods and variables from other modules into the current scope

        :param import_node:
        """
        import_alias: Dict[str] = \
            {alias.name: alias.asname if alias.asname is not None else alias.name for alias in import_node.names}

        for target, alias in import_alias.items():
            self._log_import(target)
            analyser = self._analyse_module_to_import(import_node, target)
            if analyser is not None:
                new_symbols: Dict[str, ISymbol] = analyser.export_symbols()
                for symbol in [
                        symbol for symbol in analyser.symbols
                        if symbol not in new_symbols
                ]:
                    # if there's a symbol that couldn't be loaded, log a compiler error
                    self._log_unresolved_import(
                        import_node, '{0}.{1}'.format(target, symbol))

                imported_module = Import(analyser.path, analyser.tree,
                                         analyser)
                self._current_scope.include_symbol(alias, imported_module)
Beispiel #2
0
    def visit_ImportFrom(self, import_from: ast.ImportFrom):
        """
        Includes methods and variables from other modules into the current scope

        :param import_from:
        """
        self._log_import(import_from.module)
        analyser = self._analyse_module_to_import(import_from,
                                                  import_from.module)
        if analyser is not None:
            import_alias: Dict[str] = \
                {alias.name: alias.asname if alias.asname is not None else alias.name for alias in import_from.names}

            new_symbols: Dict[str, ISymbol] = analyser.export_symbols(
                list(import_alias.keys()))
            # includes the module to be able to generate the functions
            imported_module = Import(analyser.path, analyser.tree, analyser,
                                     import_alias)
            self._current_scope.include_symbol(import_from.module,
                                               imported_module)

            for name, alias in import_alias.items():
                if name in new_symbols:
                    self._current_scope.include_symbol(
                        alias, imported_module.symbols[name])
                else:
                    # if there's a symbol that couldn't be loaded, log a compiler error
                    self._log_unresolved_import(import_from, name)