From cb1bdceedb81bff9eb867e5bfe0fd802553038d4 Mon Sep 17 00:00:00 2001 From: Sergey Vartanov Date: Fri, 19 Aug 2022 10:56:09 +0300 Subject: [PATCH] Issue #140: check for an empty scheme file. --- map_machine/mapper.py | 4 +++- map_machine/scheme.py | 13 +++++++++---- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/map_machine/mapper.py b/map_machine/mapper.py index 3af9a96..628d867 100644 --- a/map_machine/mapper.py +++ b/map_machine/mapper.py @@ -267,7 +267,9 @@ def render_map(arguments: argparse.Namespace) -> None: if scheme_path is None: fatal(f"Scheme `{arguments.scheme}` not found.") - scheme: Scheme = Scheme.from_file(scheme_path) + scheme: Optional[Scheme] = Scheme.from_file(scheme_path) + if scheme is None: + fatal(f"Failed to load scheme from `{arguments.scheme}`.") configuration: MapConfiguration = MapConfiguration.from_options( scheme, arguments, float(arguments.zoom) diff --git a/map_machine/scheme.py b/map_machine/scheme.py index fe370d0..1ec8e56 100644 --- a/map_machine/scheme.py +++ b/map_machine/scheme.py @@ -363,15 +363,20 @@ def __init__(self, content: dict[str, Any]) -> None: self.cache: dict[str, tuple[IconSet, int]] = {} @classmethod - def from_file(cls, file_name: Path) -> "Scheme": + def from_file(cls, file_name: Path) -> Optional["Scheme"]: """ :param file_name: name of the scheme file with tags, colors, and tag key specification """ with file_name.open(encoding="utf-8") as input_file: - content: dict[str, Any] = yaml.load( - input_file.read(), Loader=yaml.FullLoader - ) + try: + content: dict[str, Any] = yaml.load( + input_file.read(), Loader=yaml.FullLoader + ) + except yaml.YAMLError: + return None + if not content: + return cls({}) return cls(content) def get_color(self, color: str) -> Color: