#!/usr/local/bin/python # Quick hack to generate HTML code for the quarter-mile times from a simple # plain-text format. # Copyright (C) A J Computing, 2005. # You may use, modify, and re-distribute this code without restriction, # provided that the above copyright is maintained in all copies (or that # appropriate credit is otherwise given). No warranty - use entirely at # your own risk. # This script only outputs only partial html files; include it in your # document with your preferred inclusion mechanism (we use SSI). import sys def verify_dict(dictionary, keys): for i in keys: if not dictionary.has_key(i): return False return True def stderr_print(text): sys.stderr.write("%s: %s\n" % (sys.argv[0], text)) def bomb(message): stderr_print(message) sys.exit(1) def parse(fd): fields = ["Car", "Year", "Power", "Time", "Owner", "URL", "Excuses"] indexkey = "Time" times = {} currentcar = {} lineno = 0 while 1: line = fd.readline().strip() lineno = lineno + 1 if not line: break if line[0] == "#": # it's a comment. move along now, nothing to see here.. continue if line[0] == "%": rv = verify_dict(currentcar, fields) if not rv: bomb("got an invalid entry ending on %s; fields missing." % ( lineno)) keyname = currentcar[indexkey].split("@")[0] keyname = keyname.replace("S", "").replace("s", "").strip() keyname = "%06.3f" % (float(keyname)) if not times.has_key(keyname): times[keyname] = [] times[keyname].append(currentcar) currentcar = {} continue parts = line.split(":", 1) if len(parts) < 2: bomb("invalid syntax on line %s: %s" % (lineno, line)) keyname = parts[0] value = parts[1].strip() if not keyname in fields: bomb("unknown field '%s' on line %s" % (keyname, lineno)) currentcar[keyname] = value return times def format(tdict): lines = ['
| Car | Year | Power | Time | Name | Excuses | ", "
|---|---|---|---|---|---|
| %s | %s | ' "%s | %s | %s | %s |