Issue #140: check for an empty scheme file.

This commit is contained in:
Sergey Vartanov 2022-08-19 10:56:09 +03:00
parent a6955c7d22
commit cb1bdceedb
2 changed files with 12 additions and 5 deletions

View file

@ -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)

View file

@ -363,15 +363,20 @@ class Scheme:
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: