Skip to content

Commit 326f437

Browse files
Deduplicate scenario file suffixes
Suggested by SonarQube to avoid duplicating the .yaml literal across scenario loading code.
1 parent 3696d91 commit 326f437

3 files changed

Lines changed: 12 additions & 6 deletions

File tree

src/swiss_ai_model_launch/loadtest/core.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88

99
import yaml
1010

11+
from swiss_ai_model_launch.loadtest.scenario_files import SCENARIO_SUFFIXES, YAML_SCENARIO_SUFFIXES
12+
1113
from .models import LoadtestConfig, ServerConfig
1214

1315
_BUILTIN_SCENARIOS_DIR = files("swiss_ai_model_launch.assets").joinpath("scenarios")
@@ -16,7 +18,7 @@
1618

1719
def _load_scenario_definition_file(path: Traversable | Path, ext: str) -> dict[str, Any]:
1820
text = path.read_text()
19-
data = yaml.safe_load(text) if ext in (".yaml", ".yml") else json.loads(text)
21+
data = yaml.safe_load(text) if ext in YAML_SCENARIO_SUFFIXES else json.loads(text)
2022
if not isinstance(data, dict):
2123
raise ValueError(f"Scenario file must contain a top-level object: {path}")
2224
return data
@@ -27,12 +29,12 @@ def _load_scenario_definition(name: str) -> dict[str, Any] | None:
2729
2830
Supports .yaml, .yml, and .json; returns a plain dict ready for JSON serialization.
2931
"""
30-
for ext in (".yaml", ".yml", ".json"):
32+
for ext in SCENARIO_SUFFIXES:
3133
custom_path = _CUSTOM_SCENARIOS_DIR / f"{name}{ext}"
3234
if custom_path.exists():
3335
return _load_scenario_definition_file(custom_path, ext)
3436

35-
for ext in (".yaml", ".yml", ".json"):
37+
for ext in SCENARIO_SUFFIXES:
3638
builtin_path = _BUILTIN_SCENARIOS_DIR.joinpath(f"{name}{ext}")
3739
if builtin_path.is_file():
3840
return _load_scenario_definition_file(builtin_path, ext)

src/swiss_ai_model_launch/loadtest/models.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88

99
import yaml
1010

11+
from swiss_ai_model_launch.loadtest.scenario_files import SCENARIO_SUFFIXES, YAML_SCENARIO_SUFFIXES
12+
1113

1214
@dataclass
1315
class ScenarioConfig:
@@ -27,7 +29,7 @@ def _scenario_suffix(path: Traversable | Path) -> str:
2729

2830
def _load_scenario_file(path: Traversable | Path) -> ScenarioConfig:
2931
text = path.read_text()
30-
data = yaml.safe_load(text) if _scenario_suffix(path) in (".yaml", ".yml") else json.loads(text)
32+
data = yaml.safe_load(text) if _scenario_suffix(path) in YAML_SCENARIO_SUFFIXES else json.loads(text)
3133
return ScenarioConfig(
3234
name=data["name"],
3335
max_tokens=data.get("max_tokens", "2048"),
@@ -40,12 +42,12 @@ def load_scenarios() -> list[ScenarioConfig]:
4042
"""Load built-in scenarios, with any user-defined ones from CWD/scenarios/ appended."""
4143
scenarios: dict[str, ScenarioConfig] = {}
4244
for path in sorted(_BUILTIN_SCENARIOS_DIR.iterdir(), key=lambda p: p.name):
43-
if _scenario_suffix(path) in (".yaml", ".yml", ".json"):
45+
if _scenario_suffix(path) in SCENARIO_SUFFIXES:
4446
s = _load_scenario_file(path)
4547
scenarios[s.name] = s
4648
if _CUSTOM_SCENARIOS_DIR.exists():
4749
for path in sorted(_CUSTOM_SCENARIOS_DIR.glob("*")):
48-
if _scenario_suffix(path) in (".yaml", ".yml", ".json"):
50+
if _scenario_suffix(path) in SCENARIO_SUFFIXES:
4951
s = _load_scenario_file(path)
5052
scenarios[s.name] = s
5153
# custom is always appended last as the catch-all
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
YAML_SCENARIO_SUFFIXES = (".yaml", ".yml")
2+
SCENARIO_SUFFIXES = (*YAML_SCENARIO_SUFFIXES, ".json")

0 commit comments

Comments
 (0)