@@ -33,6 +33,7 @@ def init_config(
3333 convert_keys_to_snake_case : bool = False ,
3434 add_underscore_prefix_to_keywords : bool = False ,
3535 raise_error_non_identifiers : bool = False ,
36+ replace_dashes_with_underscores : bool = False ,
3637 merge_configs : bool = True ,
3738 sections_ignored_on_merge : Optional [List [str ]] = None ,
3839 validate_data_types : bool = True ,
@@ -49,6 +50,7 @@ def init_config(
4950 convert_keys_to_snake_case: convert config section names to snake case
5051 add_underscore_prefix_to_keywords: add underscore prefix to Python keywords
5152 raise_error_non_identifiers: raise error if config section name is not a valid identifier
53+ replace_dashes_with_underscores: replace dashes with underscores in section names and keys
5254 merge_configs: merge default config with production config (setting to `False` disables flags below)
5355 sections_ignored_on_merge: list of sections to ignore when merging configs
5456 validate_data_types: raise error if data types in production config are not the same as default
@@ -94,8 +96,10 @@ def _sanitize_section(section: str) -> str:
9496 err_msg = f'section `{ section } ` is not a valid identifier, aborting'
9597 logger .error (err_msg )
9698 raise PyyaError (err_msg )
99+ if replace_dashes_with_underscores :
100+ section = section .replace ('-' , '_' )
97101 if add_underscore_prefix_to_keywords and keyword .iskeyword (section ):
98- logger .info (f'section `{ section } ` is a keyword, renaming to `_{ section } `' )
102+ logger .debug (f'section `{ section } ` is a keyword, renaming to `_{ section } `' )
99103 section = f'_{ section } '
100104 return section
101105
@@ -163,7 +167,9 @@ def _model_and_stub_from_dict(
163167 fields : Dict [Any , Any ] = {}
164168 if path is None :
165169 path = []
166- class_name = '' .join (part .capitalize () if i > 0 else part for i , part in enumerate (path + [name ]))
170+ class_name = '' .join (part .capitalize () if i > 0 else part for i , part in enumerate (path + [name ])).replace (
171+ '-' , '_'
172+ )
167173 stub_lines = [f'class { class_name } (Dict[str, Any]):' ]
168174 nested_stubs = []
169175 py_type : Any
@@ -198,7 +204,7 @@ def _model_and_stub_from_dict(
198204 if not keyword .iskeyword (section ) and section .isidentifier ():
199205 stub_lines .append (f' { section } : { py_type .__name__ } ' )
200206 fields [section ] = (py_type , entry )
201- stub_code = '\n \n ' .join (nested_stubs + ['\n ' .join (stub_lines )]). replace ( '-' , '_' )
207+ stub_code = '\n \n ' .join (nested_stubs + ['\n ' .join (stub_lines )])
202208 return create_model (name , ** fields , __base__ = ExtraBase ), stub_code
203209
204210 def _get_default_raw_data () -> ConfigType :
0 commit comments