def __process_old_anns(self, node): # ! For One-at-a-Time, don't remove original type annotations if self.__granularity == "var": return node if isinstance(node, arg): return copy_location( arg(arg=node.arg, annotation=None, type_comment=None), node) elif isinstance(node, FunctionDef): return copy_location( FunctionDef( name=node.name, args=node.args, body=node.body, decorator_list=node.decorator_list, returns=None, type_comment=None, ), node, ) elif isinstance(node, AnnAssign): return copy_location( AnnAssign( target=node.target, annotation=None, value=node.value, simple=node.simple, ), node, ) elif isinstance(node, Assign): return copy_location( Assign( targets=node.targets, annotation=None, value=node.value, type_comment=None, ), node, ) else: return node
def visit_FunctionDef(self, node): self.generic_visit(node) pred_type, pred_prob = self.__extract_type_and_prob( node.name, node.lineno, AnnotationKind.FUNC) if pred_prob is None: return self.__process_old_anns(node) elif pred_prob == self.__IGNORED_PRED_PROB: return node else: return copy_location( FunctionDef( name=node.name, args=node.args, body=node.body, decorator_list=node.decorator_list, returns=Name(id=pred_type, ctx=Load()), type_comment=None, ), node, )
def visit_FunctionDef(self, node: ast.FunctionDef): self._tree_changed = True node.returns = None return self.generic_visit(node) # type: ignore
def derived_resources_class(project: Project) -> str: # TODO temporary and ugly solution of circular import import arcor2.resources from arcor2.resources import ResourcesBase tree = Module(body=[]) parameters = [(act.id, clean(act.name)) for aps in project.action_points for act in aps.actions] add_import(tree, arcor2.resources.__name__, ResourcesBase.__name__) derived_cls_name = "Resources" init_body: List = [ Expr(value=Call(func=Attribute(value=Call( func=Name(id='super', ctx=Load()), args=[ Name(id=derived_cls_name, ctx=Load()), Name(id='self', ctx=Load()) ], keywords=[]), attr='__init__', ctx=Load()), args=[Str(s=project.id)], keywords=[])) ] for a_id, a_name in parameters: init_body.append( Assign(targets=[get_name_attr("self", "_" + a_name, Store)], value=Call(func=get_name_attr("self", "parameters"), args=[Str(s=a_id)], keywords=[]))) cls_def = ClassDef( name=derived_cls_name, bases=[Name(id=ResourcesBase.__name__, ctx=Load())], keywords=[], body=[ FunctionDef(name='__init__', args=arguments(args=[arg(arg='self', annotation=None)], vararg=None, kwonlyargs=[], kw_defaults=[], kwarg=None, defaults=[]), body=init_body, decorator_list=[], returns=None) ], decorator_list=[]) tree.body.append(cls_def) for a_id, a_name in parameters: cls_def.body.append( FunctionDef( name=a_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=Name(id='self', ctx=Load()), attr='print_info', ctx=Load()), args=[ Str(s=a_id, kind=''), get_name_attr('self', '_' + a_name) ], keywords=[])), Return(value=get_name_attr('self', '_' + a_name)) ], decorator_list=[Name(id='property', ctx=Load())], returns=None, type_comment=None)) return tree_to_str(tree)
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)
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)
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
def visit_FunctionDef(self, node: ast.FunctionDef): node.returns = None return self.generic_visit(node) # type: ignore