From 5b22f7f78eb7dd2dfd830bf9aec4075b3602a06e Mon Sep 17 00:00:00 2001 From: Sergey Vartanov Date: Sun, 11 Jul 2021 02:42:32 +0300 Subject: [PATCH] Issue #63: refactor; add mapcss command. --- roentgen.py | 45 +++--------------------------------------- roentgen/grid.py | 51 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 54 insertions(+), 42 deletions(-) diff --git a/roentgen.py b/roentgen.py index 1f29ef8..1a3b67a 100644 --- a/roentgen.py +++ b/roentgen.py @@ -4,19 +4,17 @@ Röntgen entry point. Author: Sergey Vartanov (me@enzet.ru). """ import argparse -import os import sys from pathlib import Path from typing import List import numpy as np import svgwrite -from colour import Color from roentgen import server, tile from roentgen.constructor import Constructor from roentgen.flinger import Flinger -from roentgen.grid import IconCollection +from roentgen.grid import draw_icons, write_mapcss from roentgen.icon import ShapeExtractor from roentgen.mapper import ( AUTHOR_MODE, CREATION_TIME_MODE, ICONS_FILE_NAME, Painter, TAGS_FILE_NAME, @@ -172,50 +170,13 @@ def draw_element(target: str, tags_description: str): svg.write(open("test_icon.svg", "w+")) -def draw_icons() -> None: - """ - Draw all possible icon shapes combinations as grid in one SVG file and as - individual SVG files. - """ - out_path: Path = Path("out") - icons_by_id_path: Path = out_path / "icons_by_id" - icons_by_name_path: Path = out_path / "icons_by_name" - icons_with_outline_path: Path = out_path / "roentgen_icons" / "icons" - - for path in ( - out_path, icons_by_id_path, icons_by_name_path, icons_with_outline_path - ): - path.mkdir(parents=True, exist_ok=True) - - scheme: Scheme = Scheme(Path("scheme/default.yml")) - extractor: ShapeExtractor = ShapeExtractor( - Path("icons/icons.svg"), Path("icons/config.json") - ) - collection: IconCollection = IconCollection.from_scheme(scheme, extractor) - collection.draw_grid(out_path / "icon_grid.svg") - collection.draw_icons(icons_by_id_path) - collection.draw_icons(icons_by_name_path, by_name=True) - - collection.draw_icons( - icons_with_outline_path, color=Color("black"), outline=True - ) - (out_path / "roentgen_icons").mkdir(exist_ok=True) - with Path("data/roentgen_icons_part.mapcss").open() as input_file: - with ( - out_path / "roentgen_icons" / "roentgen_icons.mapcss" - ).open("w+") as output_file: - for line in input_file.readlines(): - if line == "%CONTENT%\n": - output_file.write(collection.get_mapcss_selectors()) - else: - output_file.write(line) - - if __name__ == "__main__": if len(sys.argv) == 3 and sys.argv[1] in ["node", "way", "area"]: draw_element(sys.argv[1], sys.argv[2]) elif len(sys.argv) == 2 and sys.argv[1] == "icons": draw_icons() + elif len(sys.argv) == 2 and sys.argv[1] == "mapcss": + write_mapcss() elif len(sys.argv) >= 2 and sys.argv[1] == "tile": tile.ui(sys.argv[2:]) elif len(sys.argv) >= 2 and sys.argv[1] == "server": diff --git a/roentgen/grid.py b/roentgen/grid.py index 8988352..87867ae 100644 --- a/roentgen/grid.py +++ b/roentgen/grid.py @@ -202,3 +202,54 @@ class IconCollection: def sort(self) -> None: """Sort icon list.""" self.icons = sorted(self.icons) + + +def draw_icons() -> None: + """ + Draw all possible icon shapes combinations as grid in one SVG file and as + individual SVG files. + """ + out_path: Path = Path("out") + icons_by_id_path: Path = out_path / "icons_by_id" + icons_by_name_path: Path = out_path / "icons_by_name" + + for path in ( + out_path, icons_by_id_path, icons_by_name_path + ): + path.mkdir(parents=True, exist_ok=True) + + scheme: Scheme = Scheme(Path("scheme/default.yml")) + extractor: ShapeExtractor = ShapeExtractor( + Path("icons/icons.svg"), Path("icons/config.json") + ) + collection: IconCollection = IconCollection.from_scheme(scheme, extractor) + collection.draw_grid(out_path / "icon_grid.svg") + collection.draw_icons(icons_by_id_path) + collection.draw_icons(icons_by_name_path, by_name=True) + + +def write_mapcss() -> None: + """ + Write MapCSS 0.2 scheme. + """ + out_path: Path = Path("out") + icons_with_outline_path: Path = out_path / "roentgen_icons" / "icons" + + scheme: Scheme = Scheme(Path("scheme/default.yml")) + extractor: ShapeExtractor = ShapeExtractor( + Path("icons/icons.svg"), Path("icons/config.json") + ) + collection: IconCollection = IconCollection.from_scheme(scheme, extractor) + collection.draw_icons( + icons_with_outline_path, color=Color("black"), outline=True + ) + (out_path / "roentgen_icons").mkdir(exist_ok=True) + with Path("data/roentgen_icons_part.mapcss").open() as input_file: + with ( + out_path / "roentgen_icons" / "roentgen_icons.mapcss" + ).open("w+") as output_file: + for line in input_file.readlines(): + if line == "%CONTENT%\n": + output_file.write(collection.get_mapcss_selectors()) + else: + output_file.write(line) \ No newline at end of file