2525
2626import toml
2727
28+ log = logging .getLogger (__name__ )
29+
2830# ==============================================================================
2931
3032
@@ -110,7 +112,11 @@ def executable_path(path: str) -> Path:
110112
111113
112114def _load_data_from_toml (
113- path : Path , section : str , * , path_must_exist : bool = True , section_must_exist : bool = True
115+ path : Path ,
116+ section : str ,
117+ * ,
118+ path_must_exist : bool = True ,
119+ section_must_exist : bool = True ,
114120) -> dict :
115121 """
116122 Load a TOML file and return the corresponding config dictionary.
@@ -127,23 +133,23 @@ def _load_data_from_toml(
127133 section_must_exist: Whether a missing section in the TOML file is considered an error or not
128134 """
129135 try :
130- with path .open (mode = 'r' ) as fd :
136+ with path .open (mode = 'r' , encoding = 'utf-8' ) as fd :
131137 config = toml .load (fd )
132138 if section :
133139 for sub_section in section .split ('.' ):
134140 config = config [sub_section ]
135- logging .debug ('Loading data from %s table of %s' , section , path )
141+ log .debug ('Loading data from %s table of %s' , section , path )
136142 else :
137143 config = {key : value for key , value in config .items () if not isinstance (value , dict )}
138- logging .debug ('Loading data from root table of %s' , path )
144+ log .debug ('Loading data from root table of %s' , path )
139145 except FileNotFoundError as err :
140146 if path_must_exist :
141147 raise TOMLFileNotFoundError (path ) from err
142- logging .debug ('TOML file %s does not exist (not an error)' , str (path ))
148+ log .debug ('TOML file %s does not exist (not an error)' , str (path ))
143149 except KeyError as err :
144150 if section_must_exist :
145151 raise TOMLSectionKeyError (section , path ) from err
146- logging .debug ('TOML file %s does not have a %s section (not an error)' , str (path ), section )
152+ log .debug ('TOML file %s does not have a %s section (not an error)' , str (path ), section )
147153 else :
148154 return config
149155 return {}
@@ -245,7 +251,9 @@ def parse_known_args(
245251
246252 if self ._default_config_name is not None :
247253 namespace = self ._load_from_toml (
248- namespace = namespace , path = Path (self ._default_config_name ), path_must_exist = False
254+ namespace = namespace ,
255+ path = Path (self ._default_config_name ),
256+ path_must_exist = False ,
249257 )
250258
251259 namespace , args = super ().parse_known_args (args = args , namespace = namespace )
@@ -265,11 +273,13 @@ def parse_known_args(
265273 if namespace .dump_toml :
266274 exclude_keys = {'positionals' , 'dump_toml' }
267275 print (
268- toml .dumps ({
269- key : value
270- for key , value in vars (namespace ).items ()
271- if value != self ._default_args [key ] and key not in exclude_keys
272- })
276+ toml .dumps (
277+ {
278+ key : value
279+ for key , value in vars (namespace ).items ()
280+ if value != self ._default_args [key ] and key not in exclude_keys
281+ }
282+ )
273283 )
274284 sys .exit (0 )
275285
@@ -297,7 +307,10 @@ def _load_from_toml( # noqa: PLR0913
297307 overridable_keys: List of keys that can be overridden by values in the TOML file
298308 """
299309 config = _load_data_from_toml (
300- path , section , path_must_exist = path_must_exist , section_must_exist = section_must_exist
310+ path ,
311+ section ,
312+ path_must_exist = path_must_exist ,
313+ section_must_exist = section_must_exist ,
301314 )
302315
303316 for key , value in config .items ():
@@ -309,9 +322,9 @@ def _load_from_toml( # noqa: PLR0913
309322 if default_value is not None and not isinstance (value , type (default_value )):
310323 raise TOMLTypeError (type (value ), type (default_value ), key )
311324 if overridable_keys is not None and key not in overridable_keys :
312- logging .debug (' skipping non-overridable key: "%s"' , key )
325+ log .debug (' skipping non-overridable key: "%s"' , key )
313326 continue
314327
315- logging .debug (' setting %s = %s' , key , value )
328+ log .debug (' setting %s = %s' , key , value )
316329 setattr (namespace , key , value )
317330 return namespace
0 commit comments