2727 AppEntryParameters ,
2828 DAVCompressLevel ,
2929)
30- from asgi_webdav .exception import DAVExceptionConfigFileNotFound
3130
3231logger = getLogger (__name__ )
3332
@@ -300,14 +299,7 @@ def update_from_app_args_and_env_and_default_value(
300299 self ._complete_config ()
301300
302301
303- _config : Config = Config ()
304-
305-
306- def get_config () -> Config :
307- return _config
308-
309-
310- def get_config_copy_from_dict (
302+ def generate_config_from_dict (
311303 data : dict [str , Any ], complete_config : bool = False
312304) -> Config :
313305 config = Config .from_dict (data )
@@ -317,21 +309,13 @@ def get_config_copy_from_dict(
317309 return config
318310
319311
320- def reinit_config_from_dict (
321- data : dict [str , Any ], complete_config : bool = False
322- ) -> Config :
323- global _config
324-
325- logger .debug ("Load config value from python object(dict)" )
326- _config = get_config_copy_from_dict (data , complete_config )
327-
328- return _config
329-
330-
331- def reinit_config_from_file (file_name : str , complete_config : bool = False ) -> bool :
312+ def generate_config_from_file (
313+ file : Path | str , complete_config : bool = False
314+ ) -> Config | None :
332315 load_func : Callable [[Any ], Any ]
316+ if not isinstance (file , Path ):
317+ file = Path (file )
333318
334- file = Path (file_name )
335319 match file .suffix :
336320 case ".json" :
337321 load_func = json .load
@@ -340,54 +324,75 @@ def reinit_config_from_file(file_name: str, complete_config: bool = False) -> bo
340324 case _:
341325 message = f"Unsupported config file type: { file .suffix } "
342326 logger .error (message )
343- return False
327+ return None
344328
345329 try :
346330 with open (file , "rb" ) as f :
347331 data = load_func (f )
348332
349333 except FileNotFoundError as e :
350- message = f"Can not open config file[{ file } ]!"
351- logger .error (message )
352- logger .error (e )
353- # return False
354- raise DAVExceptionConfigFileNotFound (e )
334+ logger .error (f"Can not open config file[{ file } ]!, { e } " )
335+ return None
355336
356337 except (json .JSONDecodeError , tomllib .TOMLDecodeError ) as e :
357338 message = f"Load config from file[{ file } ] failed!"
358339 logger .error (message )
359340 logger .error (e )
360- return False
341+ return None
361342
362- reinit_config_from_dict (data , complete_config )
343+ config = generate_config_from_dict (data , complete_config )
363344 logger .info (f"Load config from file: [{ file } ] success!" )
364- return True
345+ return config
365346
366347
367- def reinit_config_from_file_multi_suffix (
368- file_name : str , complete_config : bool = False
369- ) -> bool :
348+ def generate_config_from_file_with_multi_suffix (
349+ file : Path | str , complete_config : bool = False
350+ ) -> Config | None :
370351 """help users in switching from .json to .toml configuration files."""
371352
372- try :
373- return reinit_config_from_file (file_name , complete_config )
374- except DAVExceptionConfigFileNotFound :
375- logger .warning (f"Can not found config file: { file_name } !" )
353+ if not isinstance (file , Path ):
354+ file = Path (file )
355+
356+ config = generate_config_from_file (file , complete_config )
357+ if config is not None :
358+ return config
376359
377360 # try other suffix
378- file = Path (file_name )
379361 stem = file .stem
380362 suffix = file .suffix
381363
382364 suffixs = {".json" , ".toml" }
383- suffixs .remove (suffix )
365+ try :
366+ suffixs .remove (suffix )
367+ except KeyError as e :
368+ logger .warning (f"Wrong file extension: { suffix } , { e } " )
384369
385370 for suffix in suffixs :
386- file_name = file .parent .joinpath (f"{ stem } { suffix } " ).as_posix ()
387- logger .warning (f"Try load config file: { file_name } !" )
388- try :
389- return reinit_config_from_file (file_name , complete_config )
390- except DAVExceptionConfigFileNotFound :
391- logger .warning (f"Can not found config file: { file_name } !" )
392-
393- return False
371+ file = file .parent .joinpath (f"{ stem } { suffix } " )
372+ logger .warning (f"Try load config file: { file } !" )
373+
374+ config = generate_config_from_file (file , complete_config )
375+ if config is None :
376+ logger .warning (f"Can not found config file: { file } !" )
377+ else :
378+ # loaded
379+ return config
380+
381+ # all failed
382+ return None
383+
384+
385+ _config : Config = Config ()
386+
387+
388+ def get_config () -> Config :
389+ return _config
390+
391+
392+ def reinit_global_config (config : Config | None = None ) -> None :
393+ global _config
394+
395+ if config is None :
396+ config = Config ()
397+
398+ _config = config
0 commit comments