#!/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 = ['
', "", '', "", ""] currenttype = 1 keys = tdict.keys() keys.sort() countar = 0 for r in keys: for plz in tdict[r]: countar += 1 (car, year, power, time, owner, url, e) = (plz["Car"], plz["Year"], plz["Power"], plz["Time"], plz["Owner"], plz["URL"], plz["Excuses"]) if not url == "": car = '%s' % (url, car) if owner == "": # :( owner = " " #if "x" in flags: # cellclass = 3 cellclass = currenttype lines.append('' "" % ( cellclass, car, year, power, time, owner, e)) if currenttype == 1: currenttype = 2 else: currenttype = 1 lines.append("
CarYearPowerTimeNameExcuses
%s%s%s%s%s%s
") lines.append("
") stderr_print("%s entries in the quarter mile table" % countar) return "\n".join(lines) if len(sys.argv) > 1: print format(parse(open(sys.argv[1])))