# vim: expandtab tabstop=4 import re import pickle from os import mkdir from binascii import crc32 # Set this to where you want the poll state to be stored polldir = "/var/state/trac/polls" def title2label(title): return hex(crc32(title))[2:] class Poll: def __init__(self, path, title, options): self.path = path self.label = title2label(title) self.title = title self.options = [] # Set defaults for option in options: self.options.append([option, {}]) # Restore on-disk representation first try: merge = pickle.load(open(self.path, "r")) for i, option in enumerate(merge): self.options[i][1] = merge[i][1] except: pass # Then "overwrite" for i, option in enumerate(options): self.options[i][0] = option def render(self, hdf): commit = 0 html = "
\n" if commit: try: pickle.dump(self.options, open(self.path, "w")) except: pass return html def execute(hdf, txt, env): args = re.split('\s*;\s*', txt) path = "%s/%s" % (polldir, title2label(hdf.getValue("project.name.encoded", "default"))) try: mkdir(path) except: pass path += "/%s.p" % title2label(args[0]) poll = Poll(path, args.pop(0), args) return poll.render(hdf)# + "" + hdf.dump() + ""