def visit_With(self, n):
     return ast3.With(
         [
             ast3.withitem(self.visit(n.context_expr),
                           self.maybe_visit(n.optional_vars))
         ],
         self.visit(n.body),
         n.type_comment,
     )
Example #2
0
def empty_script_tree(add_main_loop: bool = True) -> Module:
    """
    Creates barebones of the script (empty 'main' function).

    Returns
    -------

    """

    main_body: List[stmt] = []

    if add_main_loop:
        main_body.append(
            While(test=NameConstant(value=True), body=[Pass()], orelse=[]))
    else:
        """
        put there "pass" in order to make code valid even if there is no other statement (e.g. no object from resources)
        """
        main_body.append(Pass())

    # TODO helper function for try ... except

    tree = Module(body=[
        FunctionDef(name='main',
                    args=arguments(args=[
                        arg(arg='res',
                            annotation=Name(id='Resources', ctx=Load()),
                            type_comment=None)
                    ],
                                   vararg=None,
                                   kwonlyargs=[],
                                   kw_defaults=[],
                                   kwarg=None,
                                   defaults=[]),
                    body=main_body,
                    decorator_list=[],
                    returns=NameConstant(value=None),
                    type_comment=None),
        If(test=Compare(left=Name(id='__name__', ctx=Load()),
                        ops=[Eq()],
                        comparators=[Str(s='__main__', kind='')]),
           body=[
               Try(body=[
                   With(items=[
                       withitem(context_expr=Call(func=Name(id='Resources',
                                                            ctx=Load()),
                                                  args=[],
                                                  keywords=[]),
                                optional_vars=Name(id='res', ctx=Store()))
                   ],
                        body=[
                            Expr(value=Call(func=Name(id='main', ctx=Load()),
                                            args=[Name(id='res', ctx=Load())],
                                            keywords=[]))
                        ],
                        type_comment=None)
               ],
                   handlers=[
                       ExceptHandler(
                           type=Name(id='Exception', ctx=Load()),
                           name='e',
                           body=[
                               Expr(value=Call(func=Name(id='print_exception',
                                                         ctx=Load()),
                                               args=[Name(id='e', ctx=Load())],
                                               keywords=[]))
                           ])
                   ],
                   orelse=[],
                   finalbody=[])
           ],
           orelse=[])
    ],
                  type_ignores=[])

    add_import(tree, "arcor2.helpers", "print_exception")
    add_import(tree, "resources", "Resources", try_to_import=False)

    return tree
Example #3
0
 def visit_With(self, n):
     return ast3.With([ast3.withitem(self.visit(n.context_expr), self.maybe_visit(n.optional_vars))],
                       self.visit(n.body),
                       n.type_comment)