diff --git a/README.md b/README.md index fd15e09..f233634 100644 --- a/README.md +++ b/README.md @@ -3,7 +3,7 @@ Moduler helps make the skeletons of your Python project more readable by making it **sparse** and **meta-annotated**. In Moduler, you can -- Add sections to your functions, classes and modules. +- Add sections to your functions, classes and modules without refactoring - Add semantic annotation to your codes. This includes - Add examples to your existing codes - Add todos for incomplete codes diff --git a/moduler/core.py b/moduler/core.py index 4e5f426..75d3b46 100644 --- a/moduler/core.py +++ b/moduler/core.py @@ -36,8 +36,21 @@ def build_module_tree(module) -> Struct: process_sub_modules(sub_modules, module_struct) + readme = extract_readme(root_path) + if readme is not None: + readme_struct = Struct("document", readme, None, "README") + module_struct.children.insert(0, readme_struct) + return module_struct +def extract_readme(module_dir): + readme_path = os.path.join(module_dir, "README.md") + if not os.path.exists(readme_path): + return None + with open(readme_path, "r") as f: + readme = f.read() + return readme + def extract_module_tree_without_comment(module, root_path): module_struct: Struct = Struct("module", module, None, module.__name__) @@ -238,29 +251,36 @@ def build_section_tree(root_struct: Struct): return root_struct -def process_sub_modules(sub_modules, root_struct: Struct): +def process_sub_modules(sub_modules: List | Dict, root_struct: Struct): if isinstance(sub_modules, list): - for i, sub_module in enumerate(sub_modules): - if isinstance(sub_module, dict): - process_sub_modules(sub_module, root_struct) - elif isinstance(sub_module, str): - root_struct.children.append(Struct("comment", sub_module, None, None)) - elif sub_module is not None: - root_struct.children.append(build_module_tree(sub_module)) + process_sub_modules_in_list(root_struct, sub_modules) elif isinstance(sub_modules, dict): - for title, sub_module_list in sub_modules.items(): - if root_struct.struct_type == "section": - new_level = root_struct.obj[1] + 1 - new_section = Struct("section", (title, new_level), None, title) - process_sub_modules(sub_module_list, new_section) - else: - new_section = Struct("section", (title, 1), None, title) - process_sub_modules(sub_module_list, new_section) - root_struct.children.append(new_section) + process_sub_modules_in_dict(root_struct, sub_modules) else: assert False +def process_sub_modules_in_list(root_struct: Struct, sub_modules: List): + for i, sub_module in enumerate(sub_modules): + if isinstance(sub_module, dict): + process_sub_modules(sub_module, root_struct) + elif isinstance(sub_module, str): + root_struct.children.append(Struct("comment", sub_module, None, None)) + elif sub_module is not None: + root_struct.children.append(build_module_tree(sub_module)) + + +def process_sub_modules_in_dict(root_struct: Struct, sub_modules: Dict): + for title, sub_module_list in sub_modules.items(): + if root_struct.struct_type == "section": + new_level = root_struct.obj[1] + 1 + new_section = Struct("section", (title, new_level), None, title) + process_sub_modules(sub_module_list, new_section) + else: + new_section = Struct("section", (title, 1), None, title) + process_sub_modules(sub_module_list, new_section) + root_struct.children.append(new_section) + def add_raw_comments_to_struct(cmt_structs: List[Struct], root_struct: Struct): """ diff --git a/moduler/gui.py b/moduler/gui.py index 07731e5..de05e9e 100644 --- a/moduler/gui.py +++ b/moduler/gui.py @@ -17,7 +17,7 @@ def draw_module_tree(): def draw_treemap(struct: Struct): ids, labels, parents, texts = extract_tree_ingredients(struct) - + texts = [hypenate_texts(text) for text in texts] fig = go.Figure(go.Treemap( labels=labels, parents=parents, @@ -78,6 +78,12 @@ def add_ingredients_to_lists(root: Struct, root_path: str, root_idx: int, labels elif child.struct_type == "function": texts.append("") add_ingredients_to_lists(child, child_path, len(texts)-1, labels, parents, texts, ids) + elif child.struct_type == "document": + texts.append(child.obj) + add_ingredients_to_lists(child, child_path, len(texts) - 1, labels, parents, + texts, ids) + else: + raise diff --git a/playground/visualize_project.py b/playground/visualize_project.py new file mode 100644 index 0000000..63b5bbe --- /dev/null +++ b/playground/visualize_project.py @@ -0,0 +1,6 @@ +from moduler.core import build_module_tree +from moduler.gui import draw_treemap +import moduler + +struct = build_module_tree(moduler) +draw_treemap(struct) \ No newline at end of file