From d31e3acee18b6b3d4b78f9a8235b9bd85b58e11a Mon Sep 17 00:00:00 2001 From: Matthew Fernandez Date: Sun, 10 Apr 2022 17:44:19 -0700 Subject: [PATCH] port brewer_lib generation script to Python Note that this does not introduce an extra build dependency in any of the three build systems: 1. Autotools: these steps are done during construction of the portable source tarball, during which Python 3 is already required (see autogen.sh). 2. CMake: Python 3 is already required by and used in the top level CMakeLists.txt. 3. MS Build: Python 3 is already used in version generation in lib/version/version.vcxproj. This change is motivated by the goal of removing a dependency on Awk. Gitlab: #2118 --- awk/brewer.awk | 25 ------------------ lib/common/CMakeLists.txt | 8 +++--- lib/common/Makefile.am | 7 ++--- lib/common/make_brewer_lib.py | 48 +++++++++++++++++++++++++++++++++++ lib/gvc.vcxproj | 4 +-- 5 files changed, 58 insertions(+), 34 deletions(-) delete mode 100644 awk/brewer.awk create mode 100644 lib/common/make_brewer_lib.py diff --git a/awk/brewer.awk b/awk/brewer.awk deleted file mode 100644 index f3821b832..000000000 --- a/awk/brewer.awk +++ /dev/null @@ -1,25 +0,0 @@ -# -# /************************************************************************* -# * Copyright (c) 2011 AT&T Intellectual Property -# * All rights reserved. This program and the accompanying materials -# * are made available under the terms of the Eclipse Public License v1.0 -# * which accompanies this distribution, and is available at -# * http://www.eclipse.org/legal/epl-v10.html -# * -# * Contributors: Details at http://www.graphviz.org/ -# *************************************************************************/ -# -# Convert Brewer data to same RGBA format used in color_names. -# See brewer_colors for input format. -# -# All colors assumed opaque, so A = 255 in all colors -BEGIN { - FS = "," -} -/^[^#]/{ - if ($1 != "") { - name = $1 $2; - gsub ("\"","",name); - } - printf ("/%s/%s %s %s %s 255\n", name, $5, $7, $8, $9); -} diff --git a/lib/common/CMakeLists.txt b/lib/common/CMakeLists.txt index 8b050df52..fef1d353f 100644 --- a/lib/common/CMakeLists.txt +++ b/lib/common/CMakeLists.txt @@ -15,11 +15,11 @@ add_custom_command( ) add_custom_command( OUTPUT brewer_lib - DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/brewer_colors - ${CMAKE_SOURCE_DIR}/awk/brewer.awk - COMMAND ${AWK_EXECUTABLE} -f ${CMAKE_SOURCE_DIR}/awk/brewer.awk + MAIN_DEPENDENCY ${CMAKE_CURRENT_SOURCE_DIR}/brewer_colors + DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/make_brewer_lib.py + COMMAND ${Python3_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/make_brewer_lib.py ${CMAKE_CURRENT_SOURCE_DIR}/brewer_colors - >${CMAKE_CURRENT_BINARY_DIR}/brewer_lib + ${CMAKE_CURRENT_BINARY_DIR}/brewer_lib COMMENT "generate Brewer color library" ) configure_file( diff --git a/lib/common/Makefile.am b/lib/common/Makefile.am index 8cdf4b45e..7ea1d7238 100644 --- a/lib/common/Makefile.am +++ b/lib/common/Makefile.am @@ -43,8 +43,9 @@ colortbl.h : color_lib color_lib : brewer_lib svgcolor_lib $(top_srcdir)/lib/common/color_names cat brewer_lib svgcolor_lib $(top_srcdir)/lib/common/color_names | LC_ALL=C $(SORT) > color_lib -brewer_lib : $(top_srcdir)/lib/common/brewer_colors $(top_srcdir)/awk/brewer.awk - $(AWK) -f $(top_srcdir)/awk/brewer.awk $(top_srcdir)/lib/common/brewer_colors > brewer_lib +brewer_lib: $(top_srcdir)/lib/common/brewer_colors \ + $(top_srcdir)/lib/common/make_brewer_lib.py + python3 $(top_srcdir)/lib/common/make_brewer_lib.py "$<" "$@" svgcolor_lib: $(top_srcdir)/lib/common/svgcolor_names \ $(top_srcdir)/lib/common/make_svgcolor_lib.py @@ -64,4 +65,4 @@ EXTRA_DIST = README.imap \ entities.html entities.tcl \ brewer_colors brewer_lib svgcolor_names svgcolor_lib \ color_names color_lib colortbl.h \ - make_svgcolor_lib.py + make_brewer_lib.py make_svgcolor_lib.py diff --git a/lib/common/make_brewer_lib.py b/lib/common/make_brewer_lib.py new file mode 100644 index 000000000..7e3268424 --- /dev/null +++ b/lib/common/make_brewer_lib.py @@ -0,0 +1,48 @@ +#!/usr/bin/python3 + +""" +brewer_colors → brewer_lib generator +""" + +import argparse +import sys +from typing import List + +def main(args: List[str]) -> int: + """entry point""" + + # parse command line arguments + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument("input", type=argparse.FileType("rt"), + help="input Brewer CSV data") + parser.add_argument("output", type=argparse.FileType("wt"), + help="output color table entries") + options = parser.parse_args(args[1:]) + + name = None + + for line in options.input: + + # skip comments and empty lines + if line.startswith("#") or line.strip() == "": + continue + + # split the line into columns + items = line.split(",") + assert len(items) == 10, f"unexpected line {line}" + + # do we have a new name on this line? + if items[0] != "": + # derive the name from the first two columns + name = "".join(items[:2]).replace('"', "") + + assert name is not None, "first line contained no name" + + # write this as a color table entry + options.output.write(f"/{name}/{items[4]} {items[6]} {items[7]} {items[8]} 255\n") + + return 0 + + +if __name__ == "__main__": + sys.exit(main(sys.argv)) diff --git a/lib/gvc.vcxproj b/lib/gvc.vcxproj index 6c7e6410b..31100ae4c 100644 --- a/lib/gvc.vcxproj +++ b/lib/gvc.vcxproj @@ -73,7 +73,7 @@ win_bison -dy -Wno-yacc common\htmlparse.y -o common\htmlparse.c python common\make_svgcolor_lib.py common\svgcolor_names common\svgcolor_lib -awk -f $(SolutionDir)awk\brewer.awk common\brewer_colors > common\brewer_lib +python common\make_brewer_lib.py common\brewer_colors common\brewer_lib type common\brewer_lib common\svgcolor_lib common\color_names | sort /L C > color_lib awk -f $(SolutionDir)awk\colortbl.awk color_lib > common\colortbl.h @@ -104,7 +104,7 @@ awk -f $(SolutionDir)awk\colortbl.awk color_lib > common\colortbl.h win_bison -dy -Wno-yacc common\htmlparse.y -o common\htmlparse.c python common\make_svgcolor_lib.py common\svgcolor_names common\svgcolor_lib -awk -f $(SolutionDir)awk\brewer.awk common\brewer_colors > common\brewer_lib +python common\make_brewer_lib.py common\brewer_colors common\brewer_lib type common\brewer_lib common\svgcolor_lib common\color_names | sort /L C > color_lib awk -f $(SolutionDir)awk\colortbl.awk color_lib > common\colortbl.h -- 2.40.0