From 916e7c7bdee613d8d4d29ba33bb79cacf83be312 Mon Sep 17 00:00:00 2001 From: Sergey Vartanov Date: Mon, 2 Aug 2021 23:47:31 +0300 Subject: [PATCH] Issue #70: add workspace file. --- roentgen.py | 13 +++---- roentgen/grid.py | 13 +++---- roentgen/mapcss.py | 21 +++++------ roentgen/mapper.py | 3 -- roentgen/moire_manager.py | 7 ++-- roentgen/server.py | 10 +++--- roentgen/taginfo.py | 5 ++- roentgen/tile.py | 12 +++---- roentgen/workspace.py | 76 +++++++++++++++++++++++++++++++++++++++ 9 files changed, 113 insertions(+), 47 deletions(-) create mode 100644 roentgen/workspace.py diff --git a/roentgen.py b/roentgen.py index 10fae42..68a3908 100644 --- a/roentgen.py +++ b/roentgen.py @@ -12,6 +12,7 @@ import logging import numpy as np import svgwrite +from roentgen import workspace from roentgen import server, tile from roentgen.constructor import Constructor from roentgen.flinger import Flinger @@ -20,9 +21,7 @@ from roentgen.icon import ShapeExtractor from roentgen.mapper import ( AUTHOR_MODE, CREATION_TIME_MODE, - ICONS_FILE_NAME, Painter, - TAGS_FILE_NAME, check_level_number, check_level_overground, ) @@ -57,7 +56,7 @@ def main(options) -> None: sys.exit(1) input_file_names = [cache_path / f"{options.boundary_box}.osm"] - scheme: Scheme = Scheme(Path(TAGS_FILE_NAME)) + scheme: Scheme = Scheme(workspace.DEFAULT_SCHEME_PATH) min_: np.array max_: np.array map_: Map @@ -104,7 +103,7 @@ def main(options) -> None: options.output_file_name, size=size ) icon_extractor: ShapeExtractor = ShapeExtractor( - Path(ICONS_FILE_NAME), Path("icons/config.json") + workspace.ICONS_PATH, workspace.ICONS_CONFIG_PATH ) if options.level: @@ -160,10 +159,6 @@ def main(options) -> None: def draw_element(options): """ Draw single node, line, or area. - - :param target: node, line, or area. - :param tags_description: text description of tags, pair are separated by - comma, key from value is separated by equals sign. """ if options.node: target = "node" @@ -231,7 +226,7 @@ if __name__ == "__main__": elif options.command == "element": draw_element(options) elif options.command == "server": - server.ui(sys.argv[2:]) + server.ui() elif options.command == "taginfo": from roentgen.taginfo import write_taginfo_project_file diff --git a/roentgen/grid.py b/roentgen/grid.py index 02a5074..4d079c2 100644 --- a/roentgen/grid.py +++ b/roentgen/grid.py @@ -10,6 +10,7 @@ import numpy as np from colour import Color from svgwrite import Drawing +from roentgen import workspace from roentgen.icon import Icon, Shape, ShapeExtractor, ShapeSpecification from roentgen.scheme import NodeMatcher, Scheme @@ -198,19 +199,19 @@ 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" + out_path: Path = workspace.get_output_path() + icons_by_id_path: Path = workspace.get_icons_by_id_path() + icons_by_name_path: Path = workspace.get_icons_by_name_path() 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")) + scheme: Scheme = Scheme(workspace.DEFAULT_SCHEME_PATH) extractor: ShapeExtractor = ShapeExtractor( - Path("icons/icons.svg"), Path("icons/config.json") + workspace.ICONS_PATH, workspace.ICONS_CONFIG_PATH ) collection: IconCollection = IconCollection.from_scheme(scheme, extractor) - icon_grid_path: Path = out_path / "icon_grid.svg" + icon_grid_path: Path = workspace.get_icon_grid_path() collection.draw_grid(icon_grid_path) logging.info(f"Icon grid is written to {icon_grid_path}.") collection.draw_icons(icons_by_id_path) diff --git a/roentgen/mapcss.py b/roentgen/mapcss.py index fca9548..0ba82d6 100644 --- a/roentgen/mapcss.py +++ b/roentgen/mapcss.py @@ -7,6 +7,7 @@ from typing import List, Optional, Dict import logging from colour import Color +from roentgen import workspace from roentgen.grid import IconCollection from roentgen.icon import ShapeExtractor from roentgen.osm_reader import STAGES_OF_DECAY @@ -74,7 +75,7 @@ class MapCSSWriter: """ Construct icon selectors for MapCSS 0.2 scheme. """ - with Path("data/roentgen_icons_part.mapcss").open() as input_file: + with workspace.MAPCSS_PART_FILE_PATH.open() as input_file: output_file.write(input_file.read()) output_file.write("\n") @@ -107,27 +108,21 @@ def write_mapcss() -> None: """ Write MapCSS 0.2 scheme. """ - icon_directory_name: str = "icons" + directory: Path = workspace.get_mapcss_path() + icons_with_outline_path: Path = workspace.get_mapcss_icons_path() - out_path: Path = Path("out") - directory: Path = out_path / "roentgen_icons_mapcss" - directory.mkdir(exist_ok=True) - icons_with_outline_path: Path = directory / icon_directory_name - - icons_with_outline_path.mkdir(parents=True, exist_ok=True) - - scheme: Scheme = Scheme(Path("scheme/default.yml")) + scheme: Scheme = Scheme(workspace.DEFAULT_SCHEME_PATH) extractor: ShapeExtractor = ShapeExtractor( - Path("icons/icons.svg"), Path("icons/config.json") + workspace.ICONS_PATH, workspace.ICONS_CONFIG_PATH ) collection: IconCollection = IconCollection.from_scheme(scheme, extractor) collection.draw_icons( icons_with_outline_path, color=Color("black"), outline=True ) mapcss_writer: MapCSSWriter = MapCSSWriter( - scheme, icon_directory_name, True + scheme, workspace.MAPCSS_ICONS_DIRECTORY_NAME, True ) - with (directory / "roentgen_icons.mapcss").open("w+") as output_file: + with workspace.get_mapcss_file_path().open("w+") as output_file: mapcss_writer.write(output_file) logging.info(f"MapCSS 0.2 scheme is written to {directory}.") diff --git a/roentgen/mapper.py b/roentgen/mapper.py index 0ec638f..de05624 100644 --- a/roentgen/mapper.py +++ b/roentgen/mapper.py @@ -24,9 +24,6 @@ from roentgen.scheme import Scheme __author__ = "Sergey Vartanov" __email__ = "me@enzet.ru" -ICONS_FILE_NAME: str = "icons/icons.svg" -TAGS_FILE_NAME: str = "scheme/default.yml" - AUTHOR_MODE = "user-coloring" CREATION_TIME_MODE = "time" diff --git a/roentgen/moire_manager.py b/roentgen/moire_manager.py index 63ee8f0..d5c7706 100644 --- a/roentgen/moire_manager.py +++ b/roentgen/moire_manager.py @@ -7,6 +7,7 @@ from abc import ABC from moire.moire import Tag from moire.default import Default, DefaultHTML, DefaultMarkdown, DefaultWiki +from roentgen import workspace from roentgen.icon import ShapeExtractor from pathlib import Path from typing import Dict, List, Any @@ -91,7 +92,7 @@ class TestConfiguration: test_configuration: TestConfiguration = TestConfiguration( - Path(".github/workflows/test.yml") + workspace.GITHUB_TEST_PATH ) @@ -160,7 +161,7 @@ class RoentgenHTML(RoentgenMoire, DefaultHTML): size: str = self.clear(args[1]) if len(args) > 1 else 16 return ( f'' + f'src="icons_by_id/{self.clear(args[0])}.svg" />' ) @@ -173,7 +174,7 @@ class RoentgenOSMWiki(RoentgenMoire, DefaultWiki): images = {} extractor = ShapeExtractor( - Path("icons/icons.svg"), Path("icons/config.json") + workspace.ICONS_PATH, workspace.ICONS_CONFIG_PATH ) def osm(self, args: Arguments) -> str: diff --git a/roentgen/server.py b/roentgen/server.py index 6cbf51b..e24f5cc 100644 --- a/roentgen/server.py +++ b/roentgen/server.py @@ -3,6 +3,7 @@ from http.server import BaseHTTPRequestHandler, HTTPServer from pathlib import Path from typing import Optional +from roentgen import workspace from roentgen.raster import rasterize from roentgen.tile import Tile @@ -27,13 +28,14 @@ class Handler(BaseHTTPRequestHandler): zoom = int(parts[2]) x = int(parts[3]) y = int(parts[4]) - png_path = Path("tiles") / Path(f"tile_{zoom}_{x}_{y}.png") + tile_path: Path = workspace.get_tile_path() + png_path = tile_path / f"tile_{zoom}_{x}_{y}.png" if self.update_cache: - svg_path = Path("tiles") / Path(f"tile_{zoom}_{x}_{y}.svg") + svg_path = tile_path / f"tile_{zoom}_{x}_{y}.svg" if not png_path.exists(): if not svg_path.exists(): tile = Tile(x, y, zoom) - tile.draw(Path("tiles")) + tile.draw(tile_path) rasterize(svg_path, png_path) if zoom != 18: return @@ -46,7 +48,7 @@ class Handler(BaseHTTPRequestHandler): return -def ui(args): +def ui(): server: Optional[HTTPServer] = None try: port: int = 8080 diff --git a/roentgen/taginfo.py b/roentgen/taginfo.py index 43fd03c..52469e9 100644 --- a/roentgen/taginfo.py +++ b/roentgen/taginfo.py @@ -10,6 +10,7 @@ from typing import List import logging +from roentgen import workspace from roentgen import ( __doc_url__, __project__, @@ -78,9 +79,7 @@ class TaginfoProjectFile: def write_taginfo_project_file(scheme: Scheme) -> None: - out_path: Path = Path("out") - out_path.mkdir(exist_ok=True) - out_file: Path = out_path / "roentgen_taginfo.json" + out_file: Path = workspace.get_taginfo_file_path() logging.info(f"Write Röntgen project file for Taginfo to {out_file}...") taginfo_project_file: TaginfoProjectFile = TaginfoProjectFile( out_file, scheme diff --git a/roentgen/tile.py b/roentgen/tile.py index cc4f0fb..35bedc5 100644 --- a/roentgen/tile.py +++ b/roentgen/tile.py @@ -11,10 +11,11 @@ from typing import List, Optional, Tuple import numpy as np import svgwrite +from roentgen import workspace from roentgen.constructor import Constructor from roentgen.flinger import Flinger from roentgen.icon import ShapeExtractor -from roentgen.mapper import ICONS_FILE_NAME, Painter, TAGS_FILE_NAME +from roentgen.mapper import Painter from roentgen.osm_getter import get_osm from roentgen.osm_reader import Map, OSMReader from roentgen.scheme import Scheme @@ -97,7 +98,7 @@ class Tile: f"{min(lon1, lon2):.3f},{min(lat1, lat2):.3f}," f"{max(lon1, lon2):.3f},{max(lat1, lat2):.3f}" ) - content = get_osm(boundary_box, Path("cache")) + content = get_osm(boundary_box, cache_path) if not content: error("cannot download OSM data") return None @@ -141,9 +142,9 @@ class Tile: str(output_file_name), size=size ) icon_extractor: ShapeExtractor = ShapeExtractor( - Path(ICONS_FILE_NAME), Path("icons/config.json") + workspace.ICONS_PATH, workspace.ICONS_CONFIG_PATH ) - scheme: Scheme = Scheme(Path(TAGS_FILE_NAME)) + scheme: Scheme = Scheme(workspace.DEFAULT_SCHEME_PATH) constructor: Constructor = Constructor( map_, flinger, scheme, icon_extractor ) @@ -167,8 +168,7 @@ def ui(options) -> None: """ Simple user interface for tile generation. """ - directory: Path = Path("out/tiles") - directory.mkdir(parents=True, exist_ok=True) + directory: Path = workspace.get_tile_path() tile: Tile if options.c and options.s: diff --git a/roentgen/workspace.py b/roentgen/workspace.py new file mode 100644 index 0000000..d117e65 --- /dev/null +++ b/roentgen/workspace.py @@ -0,0 +1,76 @@ +""" +File and directory path in the project. +""" +from pathlib import Path + +# Project directories and files, that are the part of the repository. + +SCHEME_PATH: Path = Path("scheme") +DEFAULT_SCHEME_PATH: Path = SCHEME_PATH / "default.yml" +ICONS_PATH: Path = Path("icons/icons.svg") +ICONS_CONFIG_PATH: Path = Path("icons/config.json") +GITHUB_TEST_PATH: Path = Path(".github/workflows/test.yml") +DATA_PATH: Path = Path("data") +MAPCSS_PART_FILE_PATH: Path = DATA_PATH / "roentgen_icons_part.mapcss" + +# Generated directories and files. + +_OUTPUT_PATH: Path = Path("out") +_ICONS_BY_ID_PATH: Path = _OUTPUT_PATH / "icons_by_id" +_ICONS_BY_NAME_PATH: Path = _OUTPUT_PATH / "icons_by_name" +_MAPCSS_PATH: Path = _OUTPUT_PATH / "roentgen_icons_mapcss" +_TILE_PATH: Path = _OUTPUT_PATH / "tiles" + +MAPCSS_ICONS_DIRECTORY_NAME: str = "icons" + + +def check_and_create(directory: Path) -> Path: + """Create directory if it doesn't exist and return it.""" + if not directory.is_dir(): + directory.mkdir(parents=True, exist_ok=True) + return directory + + +def get_output_path() -> Path: + """Path for generated files.""" + return check_and_create(_OUTPUT_PATH) + + +def get_icons_by_id_path() -> Path: + """Directory for the icon files named by identifiers.""" + return check_and_create(_ICONS_BY_ID_PATH) + + +def get_icons_by_name_path() -> Path: + """Directory for the icon files named by human-readable names.""" + return check_and_create(_ICONS_BY_NAME_PATH) + + +def get_tile_path() -> Path: + """Directory for tiles.""" + return check_and_create(_TILE_PATH) + + +def get_mapcss_path() -> Path: + """Directory for MapCSS files.""" + return check_and_create(_MAPCSS_PATH) + + +def get_mapcss_file_path() -> Path: + """Directory for MapCSS files.""" + return check_and_create(_MAPCSS_PATH) / "roentgen_icons.mapcss" + + +def get_mapcss_icons_path() -> Path: + """Directory for icons used by MapCSS file.""" + return get_mapcss_path() / MAPCSS_ICONS_DIRECTORY_NAME + + +def get_icon_grid_path() -> Path: + """Icon grid path.""" + return get_output_path() / "icon_grid.svg" + + +def get_taginfo_file_path() -> Path: + """Path to file with project information for Taginfo.""" + return get_output_path() / "roentgen_taginfo.json"