From 8d0218ee312f197a0d512aef4a97471e82e31a5b Mon Sep 17 00:00:00 2001 From: Callahan Kovacs Date: Thu, 31 Aug 2023 08:41:51 -0500 Subject: [PATCH] refactor(parts): separate yaml parsing from base errors Signed-off-by: Callahan Kovacs --- snapcraft/parts/yaml_utils.py | 42 ++++++++++++++++++++++++----------- 1 file changed, 29 insertions(+), 13 deletions(-) diff --git a/snapcraft/parts/yaml_utils.py b/snapcraft/parts/yaml_utils.py index ac3b0af2795..6d54de21bcf 100644 --- a/snapcraft/parts/yaml_utils.py +++ b/snapcraft/parts/yaml_utils.py @@ -18,7 +18,7 @@ from dataclasses import dataclass from pathlib import Path -from typing import Any, Dict, List, TextIO +from typing import Any, Dict, List, TextIO, Optional import yaml import yaml.error @@ -97,32 +97,48 @@ def __init__(self, *args, **kwargs): ) -def load(filestream: TextIO) -> Dict[str, Any]: - """Load and parse a YAML-formatted file. +def get_base(filestream: TextIO) -> Optional[str]: + """Get the effective base from a snapcraft.yaml file. :param filename: The YAML file to load. - :raises SnapcraftError: if loading didn't succeed. - :raises LegacyFallback: if the project's base is a legacy base. + :returns: Effective base of the project or None if the base cannot be determined. + + :raises SnapcraftError: If the yaml could not be loaded. """ try: data = yaml.safe_load(filestream) - build_base = utils.get_effective_base( + return utils.get_effective_base( base=data.get("base"), build_base=data.get("build-base"), project_type=data.get("type"), name=data.get("name"), ) - - if build_base is None: - raise errors.LegacyFallback("no base defined") - if build_base in _ESM_BASES: - raise errors.MaintenanceBase(build_base) - if build_base in _LEGACY_BASES: - raise errors.LegacyFallback(f"base is {build_base}") except yaml.error.YAMLError as err: raise errors.SnapcraftError(f"snapcraft.yaml parsing error: {err!s}") from err + +def load(filestream: TextIO) -> Dict[str, Any]: + """Load and parse a YAML-formatted file. + + :param filename: The YAML file to load. + + :returns: A dictionary of the yaml data. + + :raises SnapcraftError: if loading didn't succeed. + :raises LegacyFallback: if the project's base is a legacy base. + :raises MaintenanceBase: if the base is not supported. + """ + + build_base = get_base(filestream) + + if build_base is None: + raise errors.LegacyFallback("no base defined") + if build_base in _ESM_BASES: + raise errors.MaintenanceBase(build_base) + if build_base in _LEGACY_BASES: + raise errors.LegacyFallback(f"base is {build_base}") + filestream.seek(0) try: