Scan a module for top-level functions and classes. Skips objects with an @undoc decorator, or a name starting with '_'.
| 28 | |
| 29 | |
| 30 | class FuncClsScanner(ast.NodeVisitor): |
| 31 | """Scan a module for top-level functions and classes. |
| 32 | |
| 33 | Skips objects with an @undoc decorator, or a name starting with '_'. |
| 34 | """ |
| 35 | def __init__(self): |
| 36 | ast.NodeVisitor.__init__(self) |
| 37 | self.classes = [] |
| 38 | self.classes_seen = set() |
| 39 | self.functions = [] |
| 40 | |
| 41 | @staticmethod |
| 42 | def has_undoc_decorator(node): |
| 43 | return any(isinstance(d, ast.Name) and d.id == 'undoc' \ |
| 44 | for d in node.decorator_list) |
| 45 | |
| 46 | def visit_If(self, node): |
| 47 | if isinstance(node.test, ast.Compare) \ |
| 48 | and isinstance(node.test.left, ast.Name) \ |
| 49 | and node.test.left.id == '__name__': |
| 50 | return # Ignore classes defined in "if __name__ == '__main__':" |
| 51 | |
| 52 | self.generic_visit(node) |
| 53 | |
| 54 | def visit_FunctionDef(self, node): |
| 55 | if not (node.name.startswith('_') or self.has_undoc_decorator(node)) \ |
| 56 | and node.name not in self.functions: |
| 57 | self.functions.append(node.name) |
| 58 | |
| 59 | def visit_ClassDef(self, node): |
| 60 | if ( |
| 61 | not (node.name.startswith("_") or self.has_undoc_decorator(node)) |
| 62 | and node.name not in self.classes_seen |
| 63 | ): |
| 64 | cls = Obj(name=node.name, sphinx_options={}) |
| 65 | cls.has_init = any( |
| 66 | isinstance(n, ast.FunctionDef) and n.name == "__init__" |
| 67 | for n in node.body |
| 68 | ) |
| 69 | self.classes.append(cls) |
| 70 | self.classes_seen.add(node.name) |
| 71 | |
| 72 | def scan(self, mod): |
| 73 | self.visit(mod) |
| 74 | return self.functions, self.classes |
| 75 | |
| 76 | # Functions and classes |
| 77 | class ApiDocWriter: |
no outgoing calls
no test coverage detected
searching dependent graphs…