Skip to content

Commit

Permalink
fix: sections
Browse files Browse the repository at this point in the history
  • Loading branch information
kvankova committed Nov 7, 2024
1 parent 1dfc5e7 commit bf26d5c
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 7 deletions.
24 changes: 18 additions & 6 deletions src/script_content_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ def read(self, scripts: list[ScriptMetadata]) -> list[ScriptMetadata]: ...

class ScriptContentReader:
def __init__(self) -> None:
self._section_start_regex = r".*code_embedder:.*start"
self._section_end_regex = r".*code_embedder:.*end"
self._section_start_regex = r".*code_embedder:section_name start"
self._section_end_regex = r".*code_embedder:section_name end"

def read(self, scripts: list[ScriptMetadata]) -> list[ScriptMetadata]:
scripts_with_full_contents = self._read_full_script(scripts)
Expand Down Expand Up @@ -68,7 +68,9 @@ def _extract_part(self, script: ScriptMetadata) -> str:
start, end = self._extract_object_part(script)

elif script.extraction_type == "section":
start, end = self._extract_section_part(lines)
start, end = self._extract_section_part(
lines=lines, section=script.extraction_part
)
if not self._validate_section_bounds(start, end, script):
return ""

Expand Down Expand Up @@ -96,11 +98,21 @@ def _extract_object_part(self, script: ScriptMetadata) -> tuple[int | None, int

return None, None

def _extract_section_part(self, lines: list[str]) -> tuple[int | None, int | None]:
def _extract_section_part(
self, lines: list[str], section: str | None = None
) -> tuple[int | None, int | None]:
if not section:
return None, None

updated_section_start_regex = self._section_start_regex.replace(
"section_name", section
)
updated_section_end_regex = self._section_end_regex.replace("section_name", section)

for i, line in enumerate(lines):
if re.search(self._section_start_regex, line):
if re.search(updated_section_start_regex, line):
start = i + 1
elif re.search(self._section_end_regex, line):
elif re.search(updated_section_end_regex, line):
return start, i

return None, None
Expand Down
4 changes: 4 additions & 0 deletions tests/data/example_section.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,7 @@
# code_embedder:A start
print("Printing only section A")
# code_embedder:A end

# code_embedder:B start
print("Printing only section B")
# code_embedder:B end
5 changes: 5 additions & 0 deletions tests/data/expected_readme1.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ name: Test
description: Description
```
This will be filled:
```python:tests/data/example_section.py:s:B
print("Printing only section B")
```

This won't be updated:
```
print("Hello, World!")
Expand Down
5 changes: 4 additions & 1 deletion tests/data/readme1.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ This is a test readme.

This will be filled:
```python:tests/data/example_section.py:s:A
print("Printing only section A")
```

This won't be updated:
Expand All @@ -13,6 +12,10 @@ name: Test
description: Description
```
This will be filled:
```python:tests/data/example_section.py:s:B
```

This won't be updated:
```
print("Hello, World!")
Expand Down
4 changes: 4 additions & 0 deletions tests/test_script_content_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ def create_script_metadata(
"# code_embedder:A start\n"
'print("Printing only section A")\n'
"# code_embedder:A end\n"
"\n"
"# code_embedder:B start\n"
'print("Printing only section B")\n'
"# code_embedder:B end"
),
),
),
Expand Down

0 comments on commit bf26d5c

Please sign in to comment.