Skip to content

Commit

Permalink
feat: add README loader
Browse files Browse the repository at this point in the history
  • Loading branch information
doomspec committed Oct 30, 2023
1 parent 00d4dc8 commit 303c816
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 19 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
54 changes: 37 additions & 17 deletions moduler/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__)
Expand Down Expand Up @@ -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):
"""
Expand Down
8 changes: 7 additions & 1 deletion moduler/gui.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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



Expand Down
6 changes: 6 additions & 0 deletions playground/visualize_project.py
Original file line number Diff line number Diff line change
@@ -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)

0 comments on commit 303c816

Please sign in to comment.