예제 #1
0
    def __init__(self, import_target: str):
        self.can_be_imported: bool = False
        self._import_identifier: str = import_target

        try:
            module_origin: str = importlib.util.find_spec(import_target).origin
        except BaseException:
            return

        path: List[str] = module_origin.split(os.sep)
        self.path: str = module_origin.replace(os.sep, '/')

        super().__init__(ast.Module(body=[]), path[-1])
        if import_target == 'typing':
            self.symbols.update(
                {symbol_id: symbol for symbol_id, symbol in self._get_types_from_typing_lib().items()
                 if symbol_id not in Type.builtin_types()
                 })
            self.can_be_imported = True

        else:
            import re

            inside_python_folder = any(re.search(r'python(\d\.?)*', folder.lower()) for folder in path)
            updated_tree = None

            if 'boa3' in path and '/'.join(path[path.index('boa3'):]).startswith('boa3/builtin'):
                pkg_start_index = path.index('builtin') + 1
                if path[pkg_start_index] == path[-1]:
                    self.symbols = self._get_boa3_builtin_symbols()
                else:
                    pkg = import_target.split('.')
                    pkg = pkg[pkg.index('builtin') + 1:]
                    self.symbols = self._get_boa3_builtin_package(pkg)
                self.can_be_imported = True

            elif not (inside_python_folder and 'lib' in path):
                # TODO: only user modules and typing lib imports are implemented
                try:
                    from boa3.analyser.analyser import Analyser
                    analyser = Analyser.analyse(module_origin)

                    # include only imported symbols
                    if analyser.is_analysed:
                        self.symbols.update(
                            {symbol_id: symbol for symbol_id, symbol in analyser.symbol_table.items()
                             if symbol_id not in Type.all_types()
                             })
                    updated_tree = analyser.ast_tree
                    self.can_be_imported = analyser.is_analysed
                except FileNotFoundError:
                    self.can_be_imported = False

                if updated_tree is not None:
                    self._tree = updated_tree
예제 #2
0
 def __include_builtins_symbols(self):
     """
     Include the Python builtins in the global symbol table
     """
     self.symbol_table.update(Type.builtin_types())