Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Fix regex for section_name lookup #41

Merged
merged 2 commits into from
Nov 7, 2024
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
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\n"
),
),
),
Expand Down
Loading