make_brewer_lib.py 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. #!/usr/bin/python3
  2. """
  3. brewer_colors → brewer_lib generator
  4. """
  5. import argparse
  6. import sys
  7. from typing import List
  8. def main(args: List[str]) -> int:
  9. """entry point"""
  10. # parse command line arguments
  11. parser = argparse.ArgumentParser(description=__doc__)
  12. parser.add_argument(
  13. "input", type=argparse.FileType("rt"), help="input Brewer CSV data"
  14. )
  15. parser.add_argument(
  16. "output", type=argparse.FileType("wt"), help="output color table entries"
  17. )
  18. options = parser.parse_args(args[1:])
  19. name = None
  20. for line in options.input:
  21. # skip comments and empty lines
  22. if line.startswith("#") or line.strip() == "":
  23. continue
  24. # split the line into columns
  25. items = line.split(",")
  26. assert len(items) == 10, f"unexpected line {line}"
  27. # do we have a new name on this line?
  28. if items[0] != "":
  29. # derive the name from the first two columns
  30. name = "".join(items[:2]).replace('"', "")
  31. assert name is not None, "first line contained no name"
  32. # write this as a color table entry
  33. options.output.write(
  34. f"/{name}/{items[4]} {items[6]} {items[7]} {items[8]} 255\n"
  35. )
  36. return 0
  37. if __name__ == "__main__":
  38. sys.exit(main(sys.argv))