mod = "foo" import ast, inspect import importlib mod = importlib.import_module(mod) p = ast.parse(inspect.getsource(mod)) from collections import defaultdict data = defaultdict(defaultdict) for node in p.body: if isinstance(node, (ast.ImportFrom, ast.Import)): continue if isinstance(node, (ast.ClassDef, ast.FunctionDef)): data["classes"][node.name] = mod.__dict__[node.name] elif isinstance(node, ast.Assign): for trg in node.targets: if isinstance(node.value, ast.Num): data["assignments"][trg.id] = node.value.n elif isinstance(node.value, ast.Str): data["assignments"][trg.id] = node.value.s else: data["assignments"][trg.id] = mod.__dict__[trg.id]
Output:
There is a nice explanation here , which lists the actions of different types and their attributes, based on which:
class Nodes(ast.NodeVisitor): def __init__(self): self.data = defaultdict() super(Nodes, self).__init__() def visit_FunctionDef(self, node): self.data[node.name] = mod.__dict__[node.name] print("In FunctionDef with funcion {}".format(node.name)) def visit_ClassDef(self, node): self.data[node.name] = mod.__dict__[node.name] def visit_Assign(self, node): for trg in node.targets: if isinstance(node.value, (ast.Str, ast.Num, ast.Dict, ast.List, ast.ListComp, ast.NameConstant)): self.data[trg.id] = mod.__dict__[trg.id] self.generic_visit(node) def visit_Name(self, node): """ class Name(idctx) A variable name. id holds the name as a string and ctx is either class Load class Store class Del. """ print("In Name with {}\n".format(node.id)) # def visit_Dict(self, node): """ class Dict(keys, values) A dictionary. keys and values hold lists of nodes with matching order """ print("In Dict keys = {}, values = {}\n".format(node.keys,node.values)) def visit_Set(self,node): """ class Set(elts) A set. elts holds a list of nodes representing the elements. """ print("In Set elts = {}\n".format(node.elts)) def visit_List(self, node): """ class List(eltsctx) lts holds a list of nodes representing the elements. ctx is Store if the container is an assignment target (ie (x,y)=pt), and Load otherwise. """ print("In List elts = {}\nctx = {}\n".format(node.elts,node.ctx)) def visit_Tuple(self, node): """ class Tuple(eltsctx) lts holds a list of nodes representing the elements. ctx is Store if the container is an assignment target (ie (x,y)=pt), and Load otherwise. """ print("In Tuple elts = {}\nctx = {}\n".format(node.elts,node.ctx)) def visit_NameConstant(self, node): """ class NameConstant(value) True, False or None. "value" holds one of those constants. """ print("In NameConstant getting value {}\n".format(node.value)) def visit_Load(self, node): print("In Load with node {}\n".format(node.func)) def visit_Call(self, node): """ class Call(func, args, keywords, starargs, kwargs) A function call. func is the function, which will often be a Name or Attribute object. Of the arguments: args holds a list of the arguments passed by position. keywords holds a list of keyword objects representing arguments passed by keyword.starargs and kwargs each hold a single node, for arguments passed as *args and **kwargs. """ print("In Call with node {}\n".format(node.func)) def visit_Num(self, node): print("In Num getting value {}\n".format(node.n)) def visit_Str(self, node): print("In Str getting value {}\n".format(node.s)) f = Nodes() f.visit(p) print(f.data)