From 596910fe014be744088c2b5399329f64f5c36818 Mon Sep 17 00:00:00 2001 From: Sergey Vartanov Date: Mon, 15 Aug 2022 10:44:42 +0300 Subject: [PATCH] Issue #140: find scheme by identifier or path. --- map_machine/workspace.py | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/map_machine/workspace.py b/map_machine/workspace.py index d7bfa24..36bf159 100644 --- a/map_machine/workspace.py +++ b/map_machine/workspace.py @@ -4,6 +4,8 @@ __author__ = "Sergey Vartanov" __email__ = "me@enzet.ru" +from typing import Optional + HERE: Path = Path(__file__).parent @@ -40,6 +42,33 @@ def __init__(self, output_path: Path) -> None: self._mapcss_path: Path = output_path / "map_machine_mapcss" self._tile_path: Path = output_path / "tiles" + def find_scheme_path(self, identifier: str) -> Optional[Path]: + """ + Find map scheme file by its identifier. + + :param identifier: scheme identifier or file path. + :returns: + - default scheme file `default.yml` if identifier is not specified, + - `.yml` from the default scheme directory (`scheme`) if + exists, + - path if identifier is a relative or absolute path to a scheme file. + - `None` otherwise. + + See `Scheme`. + """ + if not identifier: + return self.DEFAULT_SCHEME_PATH + + path: Path = self.SCHEME_PATH / (identifier + ".yml") + if path.is_file(): + return path + + path = Path(identifier) + if path.is_file(): + return path + + return None + def get_icons_by_id_path(self) -> Path: """Directory for the icon files named by identifiers.""" return check_and_create(self._icons_by_id_path)