Skip to content

Commit 4939f02

Browse files
authored
🚧 ensure a valid python identifier for report_manger.py streamlit file (#105)
* 🚧 ensure a valid python identifier for report_manger.py streamlit file save subsection are also referenced as Page instances, therefore they need a valid Python variable name. This would not be catched at build time, but only when report is being run. * 🚚 move utils to subpackage - helps to split up utils into submodules * ✨ add test for valid identifier fct. move to utils * 🐛 add missing import
1 parent cdabfb1 commit 4939f02

File tree

4 files changed

+41
-2
lines changed

4 files changed

+41
-2
lines changed

src/vuegen/streamlit_reportview.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import os
2+
import re
23
import subprocess
34
import sys
45
from pathlib import Path
@@ -9,6 +10,7 @@
910

1011
from . import report as r
1112
from .utils import create_folder, generate_footer, is_url
13+
from .utils.variables import make_valid_identifier
1214

1315

1416
class StreamlitReportView(r.WebAppReportView):
@@ -114,7 +116,15 @@ def generate_report(
114116
)
115117

116118
for subsection in section.subsections:
117-
subsection_name_var = subsection.title.replace(" ", "_")
119+
# ! could add a non-integer to ensure it's a valid identifier
120+
subsection_name_var = make_valid_identifier(subsection.title)
121+
if not subsection_name_var.isidentifier():
122+
self.report.logger.warning(
123+
f"Subsection name '{subsection_name_var}' is not a valid identifier."
124+
)
125+
raise ValueError(
126+
f"Subsection name is not a valid Python identifier: {subsection_name_var}"
127+
)
118128
subsection_file_path = (
119129
Path(section_name_var) / f"{subsection_name_var}.py"
120130
).as_posix() # Make sure it's Posix Paths
@@ -336,10 +346,11 @@ def _generate_sections(self, output_dir: str, static_dir: str) -> None:
336346
)
337347
try:
338348
# Create subsection file
349+
_subsection_name = make_valid_identifier(subsection.title)
339350
subsection_file_path = (
340351
Path(output_dir)
341352
/ section_name_var
342-
/ f"{subsection.title.replace(' ', '_')}.py"
353+
/ f"{_subsection_name}.py"
343354
)
344355

345356
# Generate content and imports for the subsection
File renamed without changes.

src/vuegen/utils/variables.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import re
2+
3+
4+
def make_valid_identifier(name: str) -> str:
5+
"""Create a valid Python identifier from a given name.
6+
7+
Used in streamlit report sections to build structure (pages).
8+
"""
9+
ret = re.sub(r"[^a-zA-Z0-9]", "_", name)
10+
if not ret[0].isalpha():
11+
ret = "_" + ret
12+
return ret

tests/utils/test_variables.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import pytest
2+
3+
from vuegen.utils.variables import make_valid_identifier
4+
5+
test_cases = [
6+
("a.non valid name", "a_non_valid_name"),
7+
("1.non valid name", "_1_non_valid_name"),
8+
("a_valid_name", "a_valid_name"),
9+
("test", "test"),
10+
]
11+
12+
13+
@pytest.mark.parametrize("var_name,expected", test_cases)
14+
def test_make_valid_identifier(var_name, expected):
15+
"""Test the make_valid_identifier function."""
16+
assert make_valid_identifier(var_name) == expected

0 commit comments

Comments
 (0)