def visit_class(self, name, bases, body): visitor = descend(body.children, ClassVisitor) if is_test_class(name, bases): methods = [TestMethod(n, c) for (n, a, c) in visitor.methods] klass = TestClass(name=name, test_cases=methods, code=body) else: methods = [create_definition(n, a, c, Method) for (n, a, c) in visitor.methods] klass = Class(name=name, methods=methods, bases=bases) self.objects.append(klass) self.past_imports = True
def find_method_code(code, method_name): """Return part of the code tree that corresponds to the given method definition. """ class LocalizeMethodVisitor(ASTVisitor): def __init__(self): ASTVisitor.__init__(self) self.method_body = None def visit_function(self, name, args, body): if name == method_name: self.method_body = body return descend(code.children, LocalizeMethodVisitor).method_body
def inspect_code(project, path, code): try: tree = parse(code) except ParseError as e: log.warning("Inspection of module %s failed with error %s" % (path,e)) return project.create_module(path, errors=[e]) #try to skip visitor = descend(tree, ModuleVisitor) # We assume that all test classes in this module has dependencies on # all imports the module contains. for test_class in [o for o in visitor.objects if isinstance(o, TestClass)]: # We gathered all imports in a single list, but import lists of those # classes may diverge in time, so we don't want to share their # structure. test_class.imports = visitor.imports[:] return project.create_module(path, code=tree, objects=visitor.objects, imports=visitor.imports, main_snippet=visitor.main_snippet, last_import=visitor.last_import)
def visit_class(self, name, bases, body): visitor = descend(body.children, ClassVisitor) self.methods += visitor.methods self.classes += 1
self.functions += 1 class ClassVisitor(ASTVisitor): def __init__(self): ASTVisitor.__init__(self) self.methods = 0 def visit_function(self, name, args, body): self.methods += 1 try: tree = parse(code) except ParseError, e: notify("Caught parse error: %s." % e) raise GatheringError("Failed at code metrics.") visitor = descend(tree, ModuleVisitor) loc = len(filter(not_blank_nor_comment, code.splitlines())) return loc, visitor.functions, visitor.classes, visitor.methods # A copy from pythoscope.inspector.file_system. def rlistdir(path): """Resursive directory listing. Yield all files below given path, ignoring those which names begin with a dot. """ if os.path.basename(path).startswith('.'): return if os.path.isdir(path): for entry in os.listdir(path):
def visit_class(self, name, bases, body): # Ignore definitions of subclasses. pass def visit_function(self, name, args, body): self.methods.append((name, args, body)) def inspect_module(project, path): return inspect_code(project, path, read_file_contents(path)) # :: (Project, string, string) -> Module def inspect_code(project, path, code): try: tree = parse(code) except ParseError, e: log.warning("Inspection of module %s failed." % path) return project.create_module(path, errors=[e]) visitor = descend(tree, ModuleVisitor) # We assume that all test classes in this module has dependencies on # all imports the module contains. for test_class in [o for o in visitor.objects if isinstance(o, TestClass)]: # We gathered all imports in a single list, but import lists of those # classes may diverge in time, so we don't want to share their # structure. test_class.imports = visitor.imports[:] return project.create_module(path, code=tree, objects=visitor.objects, imports=visitor.imports, main_snippet=visitor.main_snippet, last_import=visitor.last_import)