def _get_imports(self) -> List[ast.stmt]:
     return [
         ast.ImportFrom(
             module='__future__',
             names=[ast.alias(name='absolute_import', asname=None)],
             level=0),
         ast.ImportFrom(module='__future__',
                        names=[ast.alias(name='division', asname=None)],
                        level=0),
         ast.ImportFrom(
             module='__future__',
             names=[ast.alias(name='print_function', asname=None)],
             level=0)
     ]
Beispiel #2
0
def new_object_type_source(parent: ObjectTypeMeta,
                           child: ObjectTypeMeta) -> str:

    assert parent.type == child.base

    tree = Module(body=[], type_ignores=[])

    if parent.type in built_in_types_names():
        import_from = arcor2.object_types.__name__
    else:
        import_from = camel_case_to_snake_case(parent.type)

    tree.body.append(
        ImportFrom(module=import_from,
                   names=[alias(name=parent.type, asname=None)],
                   level=0))

    c = ClassDef(name=child.type,
                 bases=[get_name(parent.type)],
                 keywords=[],
                 body=[],
                 decorator_list=[])

    # TODO add docstring with description (if provided)
    c.body.append(Pass())

    tree.body.append(c)

    return tree_to_str(tree)
Beispiel #3
0
def add_import(node: Module,
               module: str,
               cls: str,
               try_to_import: bool = True) -> None:
    """
    Adds "from ... import ..." to the beginning of the script.

    Parameters
    ----------
    node
    module
    cls

    Returns
    -------

    """
    class AddImportTransformer(NodeTransformer):
        def __init__(self, module: str, cls: str) -> None:
            self.done = False
            self.module = module
            self.cls = cls

        def visit_ImportFrom(self, node: ImportFrom) -> ImportFrom:
            if node.module == self.module:

                for aliass in node.names:
                    if aliass.name == self.cls:
                        self.done = True
                        break
                else:
                    node.names.append(alias(name=self.cls, asname=None))
                    self.done = True

            return node

    if try_to_import:

        try:
            imported_mod = importlib.import_module(module)
        except ModuleNotFoundError as e:
            raise SourceException(e)

        try:
            getattr(imported_mod, cls)
        except AttributeError as e:
            raise SourceException(e)

    tr = AddImportTransformer(module, cls)
    node = tr.visit(node)

    if not tr.done:
        node.body.insert(
            0,
            ImportFrom(module=module,
                       names=[alias(name=cls, asname=None)],
                       level=0))
Beispiel #4
0
 def ensure_mpi(self, canonical_name, alias):
     # if ('mpi4py', None) not in self._import_statements:
     self._import_statements[canonical_name, alias] = [
         typed_ast3.ImportFrom(
             module='mpi4py',
             names=[typed_ast3.alias(name='MPI', asname=None)],
             level=0),
         # typed_ast3.parse('mpi4py.config = no_auto_init', mode='eval') # TODO: may be needed
     ]
Beispiel #5
0
    def __add_type_imports(self, types_2_import):
        if self.__tc == "mypy":
            for t, m in self.__MYPY_SPECIALS:
                if t in types_2_import:
                    types_2_import.remove(t)
                    names = [alias(name=t, asname=None)]
                    import_mypy_special = ImportFrom(module=m,
                                                     names=names,
                                                     level=0)
                    self.__tree.body.insert(self.__insertion_idx,
                                            import_mypy_special)

        if len(types_2_import) > 0:
            names = [alias(name=t, asname=None) for t in types_2_import]
            import_types = ImportFrom(module="typing", names=names, level=0)
            self.__tree.body.insert(self.__insertion_idx, import_types)

        import_typing = Import(names=[alias(name="typing", asname=None)])
        self.__tree.body.insert(self.__insertion_idx, import_typing)
Beispiel #6
0
    def visit_ImportFrom(
            self,
            node: ast.ImportFrom) -> Union[ast.ImportFrom, ast.Try, ast.AST]:
        names = [name.name for name in node.names]
        if node.module == 'dbm' and names == ['ndbm']:
            import_ = ast.Import(names=[ast.alias(name='dbm', asname='ndbm')])
            return self.wrapper.get_body(previous=node,
                                         current=import_)[0]  # type: ignore

        return super().visit_ImportFrom(node)
Beispiel #7
0
        def visit_ImportFrom(self, node: ImportFrom) -> ImportFrom:
            if node.module == self.module:

                for aliass in node.names:
                    if aliass.name == self.cls:
                        self.done = True
                        break
                else:
                    node.names.append(alias(name=self.cls, asname=None))
                    self.done = True

            return node
Beispiel #8
0
 def ensure_import(self,
                   canonical_name: str,
                   alias: t.Optional[str] = None):
     if (canonical_name, alias) not in self._import_statements:
         if canonical_name in ('mpif.h',
                               '?'):  # TODO: other ways to include MPI?
             self.ensure_mpi(canonical_name, alias)
         else:
             self._import_statements[canonical_name, alias] = [
                 typed_ast3.Import(names=[
                     typed_ast3.alias(name=canonical_name, asname=alias)
                 ])
             ]
Beispiel #9
0
    def _replace_import(self, node: ast.Import, from_: str,
                        to: str) -> ast.Try:
        """Replace import with try/except with old and new import."""
        self._tree_changed = True

        rewrote_name = node.names[0].name.replace(from_, to, 1)
        import_as = node.names[0].asname or node.names[0].name.split('.')[-1]

        rewrote = ast.Import(
            names=[ast.alias(name=rewrote_name, asname=import_as)])

        return self.wrapper.get_body(
            previous=node,  # type: ignore
            current=rewrote)[0]
Beispiel #10
0
 def _get_replaced_import_from_part(
         self, node: ast.ImportFrom, alias: ast.alias,
         names_to_replace: Dict[str, Tuple[str, str]]) -> ast.ImportFrom:
     """Returns import from statement with changed module or alias."""
     full_name = '{}.{}'.format(node.module, alias.name)
     if full_name in names_to_replace:
         full_name = full_name.replace(names_to_replace[full_name][0],
                                       names_to_replace[full_name][1], 1)
     module_name = '.'.join(full_name.split('.')[:-1])
     name = full_name.split('.')[-1]
     return ast.ImportFrom(
         module=module_name,
         names=[ast.alias(name=name, asname=alias.asname or alias.name)],
         level=node.level)
 def test_ast_validator_synthetic(self):
     examples = ((typed_ast3,
                  typed_ast3.FormattedValue(typed_ast3.Str('value'), None,
                                            None)),
                 (typed_ast3,
                  typed_ast3.keyword(
                      None, typed_ast3.Name('value', typed_ast3.Load()))),
                 (typed_ast3,
                  typed_ast3.ImportFrom('pkg',
                                        [typed_ast3.alias('module', None)],
                                        None)))
     for fields_first, (ast_module, example) in itertools.product(
         (False, True), examples):
         with self.subTest(example=example):
             # tree = ast_module.Expression(example)
             validator = AstValidator[ast_module](fields_first=fields_first,
                                                  mode=None)
             validator.visit(example)
Beispiel #12
0
    def visit_ImportFrom(self, node: ast3.ImportFrom) -> VisitorOutput:
        """Defines how to import (from) modules (supported and nonsupported)

        For example, it converts::

            from numpy import array
            from numpy import *
            from somelib import var, othervar as var2
            from otherlib import *

        into::

            from pytropos.libs_checking import numpy_module
            st['array'] = numpy_module.attr['array', pos...]

            from pytropos.libs_checking import numpy_module
            st.importStar(numpy_module)

            st['var'] = pt.Top
            st['var2'] = pt.Top

            st.importStar()
            """

        libs: 'List[ast3.AST]' = []

        if node.module in self._supported_modules:
            module_name = self._supported_modules[node.module]
            # from pytropos.libs_checking import module_name
            libs.append(
                ast3.ImportFrom(
                    module='pytropos.libs_checking',
                    names=[ast3.alias(name=module_name, asname=None)],
                    level=0,
                ))
            if node.names[0].name == '*':
                # st.importStar(module_name)
                libs.append(
                    ast3.Expr(value=ast3.Call(
                        func=ast3.Attribute(
                            value=ast3.Name(id='st', ctx=ast3.Load()),
                            attr='importStar',
                            ctx=ast3.Load(),
                        ),
                        args=[ast3.Name(id=module_name, ctx=ast3.Load())],
                        keywords=[],
                    ), ))
            else:
                for alias in node.names:
                    # st['asname'] = modname.attr['name']

                    pos = pos_as_tuple(node)

                    if pos is not None:
                        attrname = ast3.Tuple(
                            elts=[ast3.Str(s=alias.name), pos],
                            ctx=ast3.Load())  # type: ast3.expr
                    else:
                        attrname = ast3.Str(s=alias.name)

                    libs.append(
                        ast3.Assign(
                            targets=[
                                ast3.Subscript(
                                    value=ast3.Name(id='st', ctx=ast3.Load()),
                                    slice=ast3.Index(value=ast3.Str(
                                        s=alias.asname if alias.
                                        asname else alias.name), ),
                                    ctx=ast3.Store(),
                                ),
                            ],
                            value=ast3.Subscript(
                                value=ast3.Attribute(
                                    value=ast3.Name(id=module_name,
                                                    ctx=ast3.Load()),
                                    attr='attr',
                                    ctx=ast3.Load(),
                                ),
                                slice=ast3.Index(value=attrname, ),
                                ctx=ast3.Load(),
                            ),
                        ))
        else:
            if node.names[0].name == '*':
                # st.importStar()
                libs.append(
                    ast3.Expr(value=ast3.Call(
                        func=ast3.Attribute(
                            value=ast3.Name(id='st', ctx=ast3.Load()),
                            attr='importStar',
                            ctx=ast3.Load(),
                        ),
                        args=[],
                        keywords=[],
                    ), ))
            else:
                libs.extend(
                    ast3.parse(  # type: ignore
                        '\n'.join([
                            "st['{asname}'] = pt.Top".format(
                                asname=alias.asname if alias.asname else alias.
                                name) for alias in node.names
                        ])).body)

        return libs
Beispiel #13
0
    def visit_Import(self, node: ast3.Import) -> VisitorOutput:
        """Defines how to import modules (supported and nonsupported)

        For example, it converts::

            import numpy
            import numpy as np
            import somelib, otherlib as other

        into::

            from pytropos.libs_checking import numpy_module
            st['numpy'] = numpy_module

            from pytropos.libs_checking import numpy_module
            st['np'] = numpy_module

            st['somelib'] = pt.ModuleTop
            st['other'] = pt.ModuleTop
        """

        # Checking if the library being loaded is supported by pytropos
        non_supported_modules: 'List[str]' = []
        modules_supported: 'List[Tuple[str, Optional[str]]]' = []
        for alias in node.names:
            if alias.name in self._supported_modules:
                modules_supported.append(
                    (alias.name, alias.asname))  # noqa: E201,E202
            else:
                non_supported_modules.append(
                    alias.name if alias.asname is None else alias.asname)

        # Loading fake modules from pytropos (if supported)
        libs: 'List[ast3.AST]' = []
        if len(modules_supported) > 0:
            libs.append(
                ast3.ImportFrom(
                    module='pytropos.libs_checking',
                    names=[
                        ast3.alias(name=self._supported_modules[name],
                                   asname=None)
                        for [name, asname] in modules_supported
                    ],
                    level=0,
                ))
            libs.extend(
                ast3.parse(  # type: ignore
                    '\n'.join([
                        f"st['{asname}'] = {self._supported_modules[name]}"
                        for name, asname in modules_supported
                    ])).body)

        # Loading modules as Any (if not supported)
        if non_supported_modules:
            libs.extend(
                ast3.parse(  # type: ignore
                    '\n'.join([
                        f"st['{name}'] = pt.ModuleTop"
                        for name in non_supported_modules
                    ])).body)

        return libs
Beispiel #14
0
def global_actions_class(project: Project) -> str:
    tree = Module(body=[])
    tree.body.append(
        ImportFrom(module='resources',
                   names=[alias(name='Resources', asname=None)],
                   level=0))

    cls_def = ClassDef(name='Actions',
                       bases=[],
                       keywords=[],
                       body=[
                           FunctionDef(name='__init__',
                                       args=arguments(args=[
                                           arg(arg='self',
                                               annotation=None,
                                               type_comment=None),
                                           arg(arg='res',
                                               annotation=Name(id='Resources',
                                                               ctx=Load()),
                                               type_comment=None)
                                       ],
                                                      vararg=None,
                                                      kwonlyargs=[],
                                                      kw_defaults=[],
                                                      kwarg=None,
                                                      defaults=[]),
                                       body=[
                                           Assign(targets=[
                                               Attribute(value=Name(
                                                   id='self', ctx=Load()),
                                                         attr='_res',
                                                         ctx=Store())
                                           ],
                                                  value=Name(id='res',
                                                             ctx=Load()),
                                                  type_comment=None)
                                       ],
                                       decorator_list=[],
                                       returns=None,
                                       type_comment=None)
                       ],
                       decorator_list=[])

    for ap in project.action_points:
        for action in ap.actions:
            ac_obj, ac_type = action.parse_type()

            m = FunctionDef(
                name=clean(action.name),
                args=arguments(
                    args=[arg(arg='self', annotation=None, type_comment=None)],
                    vararg=None,
                    kwonlyargs=[],
                    kw_defaults=[],
                    kwarg=None,
                    defaults=[]),
                body=[
                    Expr(value=Call(func=Attribute(value=Subscript(
                        value=Attribute(value=Attribute(value=Name(id='self',
                                                                   ctx=Load()),
                                                        attr='_res',
                                                        ctx=Load()),
                                        attr='all_instances',
                                        ctx=Load()),
                        slice=Index(value=Str(s=ac_obj, kind='')),
                        ctx=Load()),
                                                   attr=ac_type,
                                                   ctx=Load()),
                                    args=[
                                        Attribute(value=Attribute(value=Name(
                                            id='self', ctx=Load()),
                                                                  attr='_res',
                                                                  ctx=Load()),
                                                  attr=clean(action.name),
                                                  ctx=Load())
                                    ],
                                    keywords=[]))
                ],
                decorator_list=[],
                returns=None,
                type_comment=None)

            cls_def.body.append(m)

    tree.body.append(cls_def)
    return tree_to_str(tree)
Beispiel #15
0
def global_action_points_class(project: Project) -> str:
    tree = Module(body=[])
    tree.body.append(
        ImportFrom(module=arcor2.data.common.__name__,
                   names=[alias(name=ActionPoint.__name__, asname=None)],
                   level=0))
    tree.body.append(
        ImportFrom(module='resources',
                   names=[alias(name='Resources', asname=None)],
                   level=0))

    cls_def = ClassDef(name='ActionPoints',
                       bases=[],
                       keywords=[],
                       body=[
                           FunctionDef(name='__init__',
                                       args=arguments(args=[
                                           arg(arg='self',
                                               annotation=None,
                                               type_comment=None),
                                           arg(arg='res',
                                               annotation=Name(id='Resources',
                                                               ctx=Load()),
                                               type_comment=None)
                                       ],
                                                      vararg=None,
                                                      kwonlyargs=[],
                                                      kw_defaults=[],
                                                      kwarg=None,
                                                      defaults=[]),
                                       body=[
                                           Assign(targets=[
                                               Attribute(value=Name(
                                                   id='self', ctx=Load()),
                                                         attr='_res',
                                                         ctx=Store())
                                           ],
                                                  value=Name(id='res',
                                                             ctx=Load()),
                                                  type_comment=None)
                                       ],
                                       decorator_list=[],
                                       returns=None,
                                       type_comment=None)
                       ],
                       decorator_list=[])

    for ap in project.action_points:
        fd = FunctionDef(
            name=clean(ap.name),  # TODO avoid possible collisions
            args=arguments(
                args=[arg(arg='self', annotation=None, type_comment=None)],
                vararg=None,
                kwonlyargs=[],
                kw_defaults=[],
                kwarg=None,
                defaults=[]),
            body=[
                Return(value=Call(func=Attribute(value=Attribute(
                    value=Attribute(value=Name(id='self', ctx=Load()),
                                    attr='_res',
                                    ctx=Load()),
                    attr='project',
                    ctx=Load()),
                                                 attr='action_point',
                                                 ctx=Load()),
                                  args=[Str(s=ap.id, kind='')],
                                  keywords=[]))
            ],
            decorator_list=[Name(id='property', ctx=Load())],
            returns=Name(id='ActionPoint', ctx=Load()),
            type_comment=None)

        cls_def.body.append(fd)

    tree.body.append(cls_def)
    return tree_to_str(tree)