Skip to content

Commit a1d68e6

Browse files
authored
fix: Handle MaaS acronym in class-generator filename generation (#2665)
* fix: Handle MaaS acronym in class-generator filename generation Fixes #2664 * fix: Replace sys.exit with raise ValueError for filename validation
1 parent 5af218e commit a1d68e6

3 files changed

Lines changed: 36 additions & 1 deletion

File tree

class_generator/core/generator.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import filecmp
44
import os
5+
import re
56
from pathlib import Path
67
from typing import Any
78

@@ -55,6 +56,16 @@ def generate_resource_file_from_dict(
5556

5657
output = "# Generated using https://github.com/RedHatQE/openshift-python-wrapper/blob/main/scripts/resource/README.md\n\n"
5758
formatted_kind_str = convert_camel_case_to_snake_case(name=resource_dict["kind"])
59+
60+
if re.search(r"_[a-z]_", formatted_kind_str):
61+
msg = (
62+
f"'{resource_dict['kind']}' produced invalid filename '{formatted_kind_str}.py'.\n"
63+
f"Add '{resource_dict['kind']}' acronym to 'normalize_acronyms' dict in:\n"
64+
f" ocp_resources/utils/utils.py -> convert_camel_case_to_snake_case()"
65+
)
66+
LOGGER.error(msg)
67+
raise ValueError(msg)
68+
5869
_file_suffix: str = f"{'_' + output_file_suffix if output_file_suffix else ''}"
5970

6071
if add_tests:
@@ -94,8 +105,10 @@ def generate_resource_file_from_dict(
94105
output += rendered
95106

96107
if dry_run:
108+
console = Console()
109+
console.print(f"\n[bold green]Would create file:[/bold green] {_output_file}")
97110
_code = Syntax(code=output, lexer="python", line_numbers=True)
98-
Console().print(_code)
111+
console.print(_code)
99112

100113
else:
101114
write_and_format_rendered(filepath=_output_file, output=output)

ocp_resources/utils/utils.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,13 @@ def convert_camel_case_to_snake_case(name: str) -> str:
8888
if name.lower() in do_not_process_list:
8989
return name.lower()
9090

91+
# Known mixed-case acronyms that should be treated as single words.
92+
# e.g., "MaaS" (Metal-as-a-Service) should convert to "maas", not "maa_s".
93+
normalize_acronyms = {"MaaS": "Maas"}
94+
95+
for acronym, replacement in normalize_acronyms.items():
96+
name = name.replace(acronym, replacement)
97+
9198
formatted_str: str = ""
9299

93100
if name.islower():

tests/test_camelcase_to_snake.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,21 @@
3232
"data_volume_ttl_seconds",
3333
id="combined_uppercase_word_followed_by_uppercase_word_is_last_ends_with_lowercase",
3434
),
35+
pytest.param(
36+
"MaaSModelRef",
37+
"maas_model_ref",
38+
id="mixed_case_acronym_maas_with_following_words",
39+
),
40+
pytest.param(
41+
"MaaSAuthPolicy",
42+
"maas_auth_policy",
43+
id="mixed_case_acronym_maas_auth_policy",
44+
),
45+
pytest.param(
46+
"MaaSSubscription",
47+
"maas_subscription",
48+
id="mixed_case_acronym_maas_subscription",
49+
),
3550
],
3651
)
3752
def test_convert_camel_case_to_snake_case(camel_case_str, expected):

0 commit comments

Comments
 (0)