def visit_ClassDef(self, node): text = None reg = "_register_inst('.'.join([self.__class__.__module__,self.__class__.__name__]))" for stmt in node.body: if isinstance(stmt, ast.FunctionDef) and stmt.name == '__init__': stmt.body = [text_to_node(reg, stmt.lineno)] + stmt.body break else: # no __init__ found, make one text = """ def __init__(self, *args, **kwargs): _register_inst('.'.join([self.__class__.__module__,self.__class__.__name__])) super(%s, self).__init__(*args, **kwargs) """ % node.name node.body = [text_to_node(text, node.lineno)] + node.body return node
def _add_init_monitors(node): """Take the specified AST and translate it into the instrumented version, which will record all instances. """ node = _CtorInstrumenter().visit(node) node.body = [ ast.copy_location( text_to_node('from openmdao.main.project import _register_inst'), node) ] + node.body return node