old_contents: import os
  import csv
  
  def create_spec(scheme, key):
      ncolors = int(scheme[0]['NumOfColors'])
  
      spec = []
      for n in range(ncolors+1):
          indx = n*(1.0/ncolors)
  
          if n == 0:
              x = int(scheme[0][key])
              y = int(scheme[0][key])
          elif n == ncolors:
              x = int(scheme[ncolors-1][key])
              y = int(scheme[ncolors-1][key])
          else:
              x = int(scheme[n-1][key])
              y = int(scheme[n][key])
  
          spec.append((indx, x/255.0, y/255.0))
  
      return spec
  
  def write_palette(fp, scheme):
      pal_name = '_%s%s_data' % (scheme[0]['ColorName'],
                                 scheme[0]['NumOfColors'])
  
      red_spec = create_spec(scheme, 'R')
      green_spec = create_spec(scheme, 'G')
      blue_spec = create_spec(scheme, 'B')
  
      cdict = {'red': tuple(red_spec),
               'green': tuple(green_spec),
               'blue': tuple(blue_spec)
              }
  
      fp.write('%s = %s\n\n' % (pal_name, cdict))
  
  if __name__ == '__main__':
      header = """\
  # This file was autogenerated from the ColorBrewer palette using the
  # script extract_color_brewer_schemes.py found in the tools directory
  # of the SAHGutils source distribution. Please see that script for
  # further details and don't hand edit this file.
  
  
  """
  
      fin_str = 'Apache-Style Software License for ColorBrewer software and ColorBrewer Color Schemes'
  
      fp_out = open('../sahgutils/_color_brewer.py', 'w')
      fp_out.write(header)
  
      fp_in = open('ColorBrewer_all_schemes_RGBonly3.csv')
      reader = csv.DictReader(fp_in)
  
      try:
          scheme = []
          for row in reader:
              if (row['ColorName'] == fin_str) and (len(scheme) > 0):
                  write_palette(fp_out, scheme)
                  break
              else:
                  if (row['ColorName'] != '') and (len(scheme) > 0):
                      write_palette(fp_out, scheme)
                      scheme = []
                      scheme.append(row)
                  else:
                      scheme.append(row)
  
      finally:
          fp_in.close()
          fp_out.close()
  
new_contents: """
  Extract the RGB colour specifications from
  ColorBrewer_all_schemes_RGBonly3.csv and write them as lists in
  ../sahgutils/_color_brewer.py.
  
  """
  import os
  import csv
  
  def write_palette(fp, scheme):
      pal_name = '_%s_cat%s_data' % (scheme[0]['ColorName'],
                                     scheme[0]['NumOfColors'])
  
      ncolors = int(scheme[0]['NumOfColors'])
  
      clist = []
      for n in range(ncolors):
          clist.append([scheme[n]['R'], scheme[n]['G'], scheme[n]['B']])
  
      fp.write('%s = %s\n\n' % (pal_name, clist))
  
  if __name__ == '__main__':
      header = """\
  # This file was autogenerated from the ColorBrewer palette using the
  # script extract_color_brewer_schemes.py found in the tools directory
  # of the SAHGutils source distribution. Please see that script for
  # further details and don't hand edit this file.
  
  
  """
  
      fin_str = 'Apache-Style Software License for ColorBrewer software and ColorBrewer Color Schemes'
  
      fp_out = open('../sahgutils/_color_brewer.py', 'w')
      fp_out.write(header)
  
      fp_in = open('ColorBrewer_all_schemes_RGBonly3.csv')
      reader = csv.DictReader(fp_in)
  
      try:
          scheme = []
          for row in reader:
              if (row['ColorName'] == fin_str) and (len(scheme) > 0):
                  write_palette(fp_out, scheme)
                  break
              else:
                  if (row['ColorName'] != '') and (len(scheme) > 0):
                      write_palette(fp_out, scheme)
                      scheme = []
                      scheme.append(row)
                  else:
                      scheme.append(row)
  
      finally:
          fp_in.close()
          fp_out.close()
  
subject: Update the color-brewer extraction tool to generate lists of color specs