def _process_function(self, symbol, parent=None): """Process an ast.FunctionDef object to extract data.""" function = model.Function(symbol.name) #TODO: Decorators #We are not going to collect data from decorators yet. # for decorator in symbol.decorator_list: #Decorators can be: Name, Call, Attributes # function.decorators.append(decorator.id) if symbol.args.vararg is not None: assign = model.Assign(symbol.args.vararg) assign.add_data(symbol.lineno, '__builtin__.list', None, None) function.args[assign.name] = assign if symbol.args.kwarg is not None: assign = model.Assign(symbol.args.kwarg) assign.add_data(symbol.lineno, '__builtin__.dict', None, None) function.args[assign.name] = assign #We store the arguments to compare with default backwards defaults = [] for value in symbol.args.defaults: #TODO: In some cases we can have something like: a=os.path type_value = value.__class__ data_type = self.__mapping.get(type_value, None) defaults.append((data_type, type_value)) for arg in reversed(symbol.args.args): if isinstance(arg, ast.Tuple): self._parse_tuple_in_func_arg(arg, function, symbol.lineno) continue elif arg.id == 'self': continue assign = model.Assign(arg.id) data_type = (model.late_resolution, None) if defaults: data_type = defaults.pop() assign.add_data(symbol.lineno, data_type[0], None, data_type[1]) function.args[assign.name] = assign for sym in symbol.body: if sym.__class__ is ast.Assign: result = self._process_assign(sym) function.add_attributes(result[0]) if parent is not None: parent.add_attributes(result[1]) elif sym.__class__ is ast.FunctionDef: function.add_function(self._process_function(sym)) if sym.__class__ is not ast.Assign: self._search_recursive_for_types(function, sym, parent) return function
def _parse_tuple_in_func_arg(self, symbol_tuple, function, lineno=0): """Parse the tuple inside a function argument call.""" for item in symbol_tuple.elts: assign = model.Assign(item.id) data_type = (model.late_resolution, None) assign.add_data(lineno, data_type[0], None, data_type[1]) function.args[assign.name] = assign