Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions nirman/yaml_parser.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import yaml
from typing import Any, List, Tuple


def parse_yaml_tree(yaml_text: str) -> List[Tuple[int, str, bool]]:
data = yaml.safe_load(yaml_text)
tree: List[Tuple[int, str, bool]] = []

def walk(node: Any, depth: int):
if isinstance(node, dict):
for key, value in node.items():

# --- SPECIAL CASE: handle `files` as direct files ---
if key == "files":
if isinstance(value, list):
for item in value:
tree.append((depth, str(item), False))
else:
tree.append((depth, str(value), False))
continue

# Normal dict key = folder
tree.append((depth, key, True))
walk(value, depth + 1)

elif isinstance(node, list):
for item in node:
if isinstance(item, dict):
for key, value in item.items():
tree.append((depth, key, True))
walk(value, depth + 1)
else:
tree.append((depth, str(item), False))

else:
tree.append((depth, str(node), False))

walk(data, 0)
return tree
7 changes: 7 additions & 0 deletions structure.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
project:
src:
- main.py
- utils:
- helper.py
tests:
- test_main.py
31 changes: 31 additions & 0 deletions tests/test_yaml_parser.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import pytest
from nirman.yaml_parser import parse_yaml_tree


def test_simple_yaml_tree():
yaml_data = """
project:
src:
- main.py
- utils:
- helper.py
tests:
- test_main.py
files:
- README.md
"""

expected = [
(0, "project", True),
(1, "src", True),
(2, "main.py", False),
(2, "utils", True),
(3, "helper.py", False),
(1, "tests", True),
(2, "test_main.py", False),
(1, "README.md", False),
]

parsed = parse_yaml_tree(yaml_data)
assert parsed == expected