def load_app_config(config_path: PathLike) -> None: """Loads the configuration of the application. Args: config_path (PathLike): The path to config file. Returns: None Raises: SystemExit: Config file is unreadable. """ TypeCheck.ensure_path_like(config_path, "config_path") try: with open(config_path) as fp: # De-serialise JSON to Python's dict and update config = Config.from_dict(json.load(fp)) config.apply_core_patch(DefaultCoreConfig) make_config_global(config) except FileNotFoundError: logger.warning("Config file not found! Auto-configuring...", exc_info=True) make_config_global(Config()) except OSError: logger.exception("Unable to load config file due to unknown reason(s)!") sys.exit(1)
def test_ensure_path_like(): assert TypeCheck.ensure_path_like("") is None assert TypeCheck.ensure_path_like(b"") is None from pathlib import Path assert TypeCheck.ensure_path_like(Path("hello")) is None with pytest.raises(TypeError): TypeCheck.ensure_path_like(1)
def save_app_config(config: Config, config_path: PathLike) -> None: """Saves the configuration of the application. Args: config (Config): The global Config instance config_path (PathLike): The path to config file Returns: None Raises: SystemExit: Config file is not writable. """ TypeCheck.ensure_path_like(config_path, "config_path") try: with open(config_path, "w") as fp: json.dump(config.__dict__(), fp) except OSError: logger.exception("Unable to save config file due to unknown reason(s)!") sys.exit(1)