From d73e2b6a53280fe32b01f0b56f4937b5844892b9 Mon Sep 17 00:00:00 2001 From: Sergey Vartanov Date: Fri, 21 May 2021 01:28:22 +0300 Subject: [PATCH] Add configuration to shape extractor. --- roentgen.py | 11 +++++++---- roentgen/grid.py | 5 ++++- roentgen/icon.py | 7 +++++-- roentgen/scheme.py | 6 ++++-- test/test_icons.py | 5 ++++- 5 files changed, 24 insertions(+), 10 deletions(-) diff --git a/roentgen.py b/roentgen.py index 091a94c..a328e25 100644 --- a/roentgen.py +++ b/roentgen.py @@ -6,6 +6,7 @@ Author: Sergey Vartanov (me@enzet.ru). import argparse import os import sys +from pathlib import Path import numpy as np import svgwrite @@ -14,7 +15,7 @@ from roentgen import ui from roentgen.constructor import Constructor from roentgen.flinger import Flinger from roentgen.grid import draw_all_icons -from roentgen.icon import ShapeExtractor +from roentgen.icon import ShapeExtractor, ShapeConfiguration from roentgen.mapper import ( AUTHOR_MODE, CREATION_TIME_MODE, ICONS_FILE_NAME, Painter, TAGS_FILE_NAME, check_level_number, check_level_overground @@ -82,7 +83,9 @@ def main(argv) -> None: svg: svgwrite.Drawing = ( svgwrite.Drawing(options.output_file_name, size=size)) - icon_extractor: ShapeExtractor = ShapeExtractor(ICONS_FILE_NAME) + icon_extractor: ShapeExtractor = ShapeExtractor( + ICONS_FILE_NAME, Path("icons/config.json") + ) def check_level(x) -> bool: """ Draw objects on all levels. """ @@ -128,8 +131,8 @@ def draw_element(target: str, tags_description: str): """ tags = dict([x.split("=") for x in tags_description.split(",")]) scheme = Scheme("scheme/default.yml") - icon_extractor = ShapeExtractor("icons/icons.svg") - icon, priority = scheme.get_icon(icon_extractor, tags) + extractor = ShapeExtractor("icons/icons.svg", Path("icons/config.json")) + icon, priority = scheme.get_icon(extractor, tags) is_for_node: bool = target == "node" labels = scheme.construct_text(tags, True) point = Point( diff --git a/roentgen/grid.py b/roentgen/grid.py index 697a5c0..617e2fb 100644 --- a/roentgen/grid.py +++ b/roentgen/grid.py @@ -4,6 +4,7 @@ Icon grid drawing. Author: Sergey Vartanov (me@enzet.ru). """ from os.path import join +from pathlib import Path from typing import Any, Dict, List, Set import numpy as np @@ -36,7 +37,9 @@ def draw_all_icons( icons: List[Icon] = [] icons_file_name: str = "icons/icons.svg" - extractor: ShapeExtractor = ShapeExtractor(icons_file_name) + extractor: ShapeExtractor = ShapeExtractor( + icons_file_name, Path("icons/config.json") + ) def add() -> None: """ diff --git a/roentgen/icon.py b/roentgen/icon.py index edb5c75..d7e970f 100644 --- a/roentgen/icon.py +++ b/roentgen/icon.py @@ -81,7 +81,8 @@ class ShapeConfiguration: """ def __init__(self, file_name: Path): - content = json.load(file_name) + with file_name.open() as input_file: + content = json.load(input_file) self.right_directed: Set[str] = set(content["right_directed"]) @@ -92,7 +93,7 @@ class ShapeExtractor: Shape is a single path with "id" attribute that aligned to 16×16 grid. """ - def __init__(self, svg_file_name: str): + def __init__(self, svg_file_name: str, configuration_file_name: Path): """ :param svg_file_name: input SVG file name with icons. File may contain any other irrelevant graphics. @@ -108,6 +109,8 @@ class ShapeExtractor: if isinstance(node, Element): self.parse(node) + self.configuration = ShapeConfiguration(configuration_file_name) + def parse(self, node: Element) -> None: """ Extract icon paths into a map. diff --git a/roentgen/scheme.py b/roentgen/scheme.py index cfa9604..9d51309 100644 --- a/roentgen/scheme.py +++ b/roentgen/scheme.py @@ -173,8 +173,10 @@ class Scheme: return False def get_icon( - self, icon_extractor: ShapeExtractor, tags: Dict[str, Any], - for_: str = "node" + self, + icon_extractor: ShapeExtractor, + tags: Dict[str, Any], + for_: str = "node", ) -> Tuple[IconSet, int]: """ Construct icon set. diff --git a/test/test_icons.py b/test/test_icons.py index 28701f3..53fd381 100644 --- a/test/test_icons.py +++ b/test/test_icons.py @@ -4,6 +4,7 @@ Test icon generation for nodes. Author: Sergey Vartanov (me@enzet.ru). """ from os import makedirs +from pathlib import Path from typing import Dict from roentgen.grid import draw_all_icons @@ -19,7 +20,9 @@ def test_icons() -> None: def get_icon(tags: Dict[str, str]): scheme = Scheme("scheme/default.yml") - icon_extractor = ShapeExtractor("icons/icons.svg") + icon_extractor = ShapeExtractor( + "icons/icons.svg", Path("icons/config.json") + ) icon, _ = scheme.get_icon(icon_extractor, tags) return icon