Skip to content

Commit 615adba

Browse files
Added function to find the class meta data
1 parent 7313fd7 commit 615adba

2 files changed

Lines changed: 36 additions & 1 deletion

File tree

py_module_info/_core.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,33 @@ def get_func_meta_data(func: ast.FunctionDef, code: str, only_func_names: bool =
5252
return meta_data
5353

5454

55+
def get_class_bases(_class: ast.ClassDef, code: str):
56+
"""Returns the names of all the inherited classes in a class"""
57+
bases = []
58+
for b in _class.bases:
59+
bases.append(ast.get_source_segment(code, b))
60+
61+
bases = list(set(bases))
62+
bases.sort()
63+
return bases
64+
65+
66+
def get_class_meta_data(_class: ast.ClassDef, code: str):
67+
68+
meta_data = {}
69+
if isinstance(_class, ast.ClassDef):
70+
meta_data["bases"] = get_class_bases(_class, code)
71+
72+
methods = {}
73+
for f in ast.walk(_class):
74+
if isinstance(f, ast.FunctionDef):
75+
methods[f.name] = get_func_meta_data(f, code)
76+
77+
meta_data["methods"] = methods
78+
79+
return meta_data
80+
81+
5582
def find_function_def_in_class_def(tree: ast.Module) -> ast.Module:
5683
"""adds an attribute parent to the ast.FunctionDef in ClassDef
5784
so that it can be uniquely identified as methods in ClassDef

py_module_info/main.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from typing import Union
66

77
from ._core import find_function_def_in_class_def
8+
from ._core import get_class_meta_data
89
from ._core import get_func_meta_data
910
from ._core import get_imports
1011

@@ -124,4 +125,11 @@ def foo():
124125
return func_info
125126

126127
def get_classes_info(self) -> Dict[str, List[ast.Call]]:
127-
pass
128+
129+
class_info = {}
130+
131+
for child in ast.walk(self._tree):
132+
if isinstance(child, ast.ClassDef):
133+
class_info[child.name] = get_class_meta_data(
134+
child, read_file_code(self.filename))
135+
return class_info

0 commit comments

Comments
 (0)