From: Aleš Mrázek Date: Tue, 18 Jul 2023 11:44:15 +0000 (+0200) Subject: manager: config file can be also in JSON X-Git-Tag: v6.0.2~28^2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=b105a78709887a5286f85f3ecd73a8f2e4f83ff0;p=thirdparty%2Fknot-resolver.git manager: config file can be also in JSON First try parsing as JSON, then as YAML. --- diff --git a/manager/knot_resolver_manager/server.py b/manager/knot_resolver_manager/server.py index 58e22e97c..6803f5c2d 100644 --- a/manager/knot_resolver_manager/server.py +++ b/manager/knot_resolver_manager/server.py @@ -37,7 +37,7 @@ from knot_resolver_manager.utils.async_utils import readfile from knot_resolver_manager.utils.etag import structural_etag from knot_resolver_manager.utils.functional import Result from knot_resolver_manager.utils.modeling.exceptions import DataParsingError, DataValidationError -from knot_resolver_manager.utils.modeling.parsing import DataFormat, parse_yaml +from knot_resolver_manager.utils.modeling.parsing import DataFormat, try_to_parse from knot_resolver_manager.utils.modeling.query import query from knot_resolver_manager.utils.modeling.types import NoneType from knot_resolver_manager.utils.systemd_notify import systemd_notify @@ -118,7 +118,7 @@ class Server: else: try: data = await readfile(self._config_path) - config = KresConfig(parse_yaml(data)) + config = KresConfig(try_to_parse(data)) await self.config_store.update(config) logger.info("Configuration file successfully reloaded") except FileNotFoundError: @@ -364,7 +364,7 @@ async def _load_raw_config(config: Union[Path, Dict[str, Any]]) -> Dict[str, Any ) else: logger.info("Loading initial configuration from %s", config) - config = parse_yaml(await readfile(config)) + config = try_to_parse(await readfile(config)) # validate the initial configuration assert isinstance(config, dict) diff --git a/manager/knot_resolver_manager/utils/modeling/parsing.py b/manager/knot_resolver_manager/utils/modeling/parsing.py index 32d2a2ea4..e33cca3a1 100644 --- a/manager/knot_resolver_manager/utils/modeling/parsing.py +++ b/manager/knot_resolver_manager/utils/modeling/parsing.py @@ -81,11 +81,12 @@ def parse_json(data: str) -> Any: def try_to_parse(data: str) -> Any: - """Attempt to parse the data as a YAML or JSON string.""" + """Attempt to parse the data as a JSON or YAML string.""" + try: - return parse_yaml(data) - except yaml.YAMLError as ye: + return parse_json(data) + except json.JSONDecodeError as je: try: - return parse_json(data) - except json.JSONDecodeError as je: - raise DataParsingError(f"failed to parse data, YAML: {ye}, JSON: {je}") + return parse_yaml(data) + except yaml.YAMLError as ye: + raise DataParsingError(f"failed to parse data, JSON: {je}, YAML: {ye}")