]> granicus.if.org Git - graphviz/commitdiff
port brewer_lib generation script to Python
authorMatthew Fernandez <matthew.fernandez@gmail.com>
Mon, 11 Apr 2022 00:44:19 +0000 (17:44 -0700)
committerMatthew Fernandez <matthew.fernandez@gmail.com>
Mon, 2 May 2022 14:34:28 +0000 (07:34 -0700)
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 [deleted file]
lib/common/CMakeLists.txt
lib/common/Makefile.am
lib/common/make_brewer_lib.py [new file with mode: 0644]
lib/gvc.vcxproj

diff --git a/awk/brewer.awk b/awk/brewer.awk
deleted file mode 100644 (file)
index f3821b8..0000000
+++ /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); 
-}
index 8b050df525c812f16939fd63bacf5ea8679ff249..fef1d353f8b94283757e10a19a3fc62fc6faad6d 100644 (file)
@@ -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(
index 8cdf4b45e8346234416c253f2589cd741af5ae44..7ea1d7238693ac4063a9a065f7fa59cf24de48d6 100644 (file)
@@ -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 (file)
index 0000000..7e32684
--- /dev/null
@@ -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))
index 6c7e6410b58b139140690b78bb2fa8e375aa4e90..31100ae4c11d07345215bb0bed8f2653bf276186 100644 (file)
@@ -73,7 +73,7 @@
     <PreBuildEvent>
       <Command>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 &gt; 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 &gt; color_lib
 awk -f $(SolutionDir)awk\colortbl.awk color_lib &gt; common\colortbl.h</Command>
     </PreBuildEvent>
@@ -104,7 +104,7 @@ awk -f $(SolutionDir)awk\colortbl.awk color_lib &gt; common\colortbl.h</Command>
     <PreBuildEvent>
       <Command>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 &gt; 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 &gt; color_lib
 awk -f $(SolutionDir)awk\colortbl.awk color_lib &gt; common\colortbl.h</Command>
     </PreBuildEvent>