Line Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
from Cheetah.Template import Template
from getopt import getopt
from sys import argv, exit

def usage():
  print("Usage: " + argv[0] + " -d histo_map_file -t template_file")
  print("  Arguments:")
  print("    -d, --data=    : file of histogram map")
  print("    -t, --tmpl=    : template file")


def main():

  data = None
  tmpl = None

  try:
    opts, args = getopt(argv[1:], "d:t:", ["data=","tmpl="])
  except getopt.GetoptError as err:
    print(str(err))
    usage()
    exit(1)

  for o, a in opts:
    if o in ("-h", "--help"):
      usage()
      exit()
    elif o in ("-d", "--data"):
      data = a
    elif o in ("-t", "--tmpl"):
      tmpl = a
    else:
      print("unhandled option: " + o)
      usage()
      exit(2)

  if data == None or tmpl == None:
    usage()
    exit(3)

  # Taking data into hashmap
  f=open(data,'r')
  map = {}
  for l in f:
    a = l.strip().split()
    value = 0
    if len(a) > 1:
      value = a[1].strip()
    map[a[0].strip()] = value
  f.close()

  # Process stuff
  t = Template(file=tmpl)  
  t.datamap = map
  print(t)

if __name__ == "__main__":
  main()