File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 22
33import filecmp
44import os
5+ import re
56from pathlib import Path
67from 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 )
Original file line number Diff line number Diff 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 ():
Original file line number Diff line number Diff line change 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)
3752def test_convert_camel_case_to_snake_case (camel_case_str , expected ):
You can’t perform that action at this time.
0 commit comments