def main(): """Set it off""" now = datetime.now() usage = "%prog [options] arg" version = "%prog .1" write_mode = 'a' parser = OptionParser(usage=usage, version=version, option_list=create_options_list()) (opts, args) = parser.parse_args() if os.path.isfile(opts.config): # parse config file config = SafeConfigParser() config.read(opts.config) else: sys.exit("Please specify a valid config file") database = "{0}vip_data.db".format(config.get('DataSource', 'db_dir')) setupdb(database, config) datastore = Datastore(database) cursor = datastore.connect() vip_file = "{0}vipFeed-{1}.xml".format(config.get('Main', 'output_dir'), config.get('Main', 'fips')) with open(vip_file, 'w') as w: w.write("""<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <vip_object xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://election-info-standard.googlecode.com/files/vip_spec_v2.3.xsd" schemaVersion="2.3"> """) create_header(w, cursor, now) create_election(w, cursor) create_election_admins(w, cursor) create_election_officials(w, cursor) create_localities(w, cursor, config) create_precincts(w, cursor, config) #create_precinct_splits(w, cursor, config) create_street_segments(w, cursor, config) create_polling_locations(w, cursor, config) w.write('</vip_object>')
def main(): """Set it off""" now = datetime.now() usage = "%prog [options] arg" version = "%prog .1" write_mode = "a" parser = OptionParser(usage=usage, version=version, option_list=create_options_list()) (opts, args) = parser.parse_args() if os.path.isfile(opts.config): # parse config file config = SafeConfigParser() config.read(opts.config) else: sys.exit("Please specify a valid config file") database = "{0}vip_data.db".format(config.get("DataSource", "db_dir")) setupdb(database, config) datastore = Datastore(database) cursor = datastore.connect() vip_file = "{0}vipFeed-{1}.xml".format(config.get("Main", "output_dir"), config.get("Main", "fips")) with open(vip_file, "w") as w: w.write( """<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <vip_object xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://election-info-standard.googlecode.com/files/vip_spec_v2.3.xsd" schemaVersion="2.3"> """ ) create_header(w, cursor, now) create_election(w, cursor) create_election_admins(w, cursor) create_election_officials(w, cursor) create_localities(w, cursor, config) create_precincts(w, cursor, config) # create_precinct_splits(w, cursor, config) create_street_segments(w, cursor, config) create_polling_locations(w, cursor, config) w.write("</vip_object>")
def create_precinct_splits(w, cursor, config): print "Creating precinct split elements..." mem = Datastore(":memory:") c = mem.connect() c.execute( """CREATE TABLE IF NOT EXISTS Split_Polling ( polling_location_id INTEGER, split_id INTEGER, PRIMARY KEY(polling_location_id, split_id) )""" ) cursor.execute( """SELECT polling_location_id AS polling_location_id, split_id AS precinct_split_id FROM Split_Polling""" ) for row in cursor: c.execute("INSERT INTO Split_Polling VALUES (?,?)", (list(row))) mem.commit() cursor.execute( """SELECT ps.id AS original_id, ?||ps.id AS id, ps.name, mail_only, ?||precinct_id AS precinct_id FROM Precinct_Split ps, Precinct p WHERE p.id=ps.precinct_id""", (config.get("Precinct_Split", "split_prefix"), config.get("Precinct", "precinct_prefix")), ) for row in cursor: root = ET.Element("precinct_split", id=unicode(row["id"])) name = ET.SubElement(root, "name") name.text = row["name"] precinct_id = ET.SubElement(root, "precinct_id") precinct_id.text = unicode(row["precinct_id"]) if row["mail_only"] is not None and len(row["mail_only"]) > 0: if row["mail_only"] == "Yes": w.write(ET.tostring(root)) continue else: c.execute( """SELECT ?||polling_location_id AS polling_location_id FROM Split_Polling WHERE split_id=?""", (config.get("Polling_Location", "polling_prefix"), row["original_id"]), ) for p in c: polling_location_id = ET.SubElement(root, "polling_location_id") polling_location_id.text = unicode(p["polling_location_id"]) w.write(ET.tostring(root))
def create_precincts(w, cursor, config): """Writes all election administration information""" print "Creating precinct elements..." mem = Datastore(":memory:") c = mem.connect() c.execute( """CREATE TABLE IF NOT EXISTS Precinct_Polling ( polling_location_id INTEGER, precinct_id INTEGER, PRIMARY KEY(polling_location_id, precinct_id) )""" ) cursor.execute( """SELECT polling_location_id AS polling_location_id, precinct_id AS precinct_id FROM Precinct_Polling""" ) for row in cursor: c.execute("INSERT INTO Precinct_Polling VALUES (?,?)", (list(row))) mem.commit() cursor.execute( """SELECT P.id AS original_id, ?||P.id AS id, P.name, ?||P.locality_id AS locality_id, P.mail_only FROM Precinct P, Locality L WHERE P.locality_id = L.id AND P.locality_id IS NOT NULL AND P.name IS NOT NULL""", (config.get("Precinct", "precinct_prefix"), config.get("Locality", "locality_prefix")), ) for row in cursor: root = ET.Element("precinct", id=unicode(row["id"])) name = ET.SubElement(root, "name") name.text = row["name"] locality_id = ET.SubElement(root, "locality_id") locality_id.text = unicode(row["locality_id"]) if row["mail_only"] is not None and len(row["mail_only"]) > 0: mail_only = ET.SubElement(root, "mail_only") mail_only.text = row["mail_only"] if row["mail_only"] == "Yes": w.write(ET.tostring(root)) continue else: c.execute( """SELECT ?||polling_location_id AS polling_location_id FROM Precinct_Polling WHERE precinct_id=?""", (config.get("Polling_Location", "polling_prefix"), row["original_id"]), ) for p in c: polling_location_id = ET.SubElement(root, "polling_location_id") polling_location_id.text = unicode(p["polling_location_id"]) w.write(ET.tostring(root))
def create_precinct_splits(w, cursor, config): print "Creating precinct split elements..." mem = Datastore(':memory:') c = mem.connect() c.execute("""CREATE TABLE IF NOT EXISTS Split_Polling ( polling_location_id INTEGER, split_id INTEGER, PRIMARY KEY(polling_location_id, split_id) )""") cursor.execute("""SELECT polling_location_id AS polling_location_id, split_id AS precinct_split_id FROM Split_Polling""") for row in cursor: c.execute("INSERT INTO Split_Polling VALUES (?,?)", (list(row))) mem.commit() cursor.execute( """SELECT ps.id AS original_id, ?||ps.id AS id, ps.name, mail_only, ?||precinct_id AS precinct_id FROM Precinct_Split ps, Precinct p WHERE p.id=ps.precinct_id""", ( config.get('Precinct_Split', 'split_prefix'), config.get('Precinct', 'precinct_prefix'), )) for row in cursor: root = ET.Element("precinct_split", id=unicode(row['id'])) name = ET.SubElement(root, "name") name.text = row['name'] precinct_id = ET.SubElement(root, "precinct_id") precinct_id.text = unicode(row['precinct_id']) if row['mail_only'] is not None and len(row['mail_only']) > 0: if row['mail_only'] == 'Yes': w.write(ET.tostring(root)) continue else: c.execute( """SELECT ?||polling_location_id AS polling_location_id FROM Split_Polling WHERE split_id=?""", ( config.get('Polling_Location', 'polling_prefix'), row['original_id'], )) for p in c: polling_location_id = ET.SubElement( root, "polling_location_id") polling_location_id.text = unicode( p['polling_location_id']) w.write(ET.tostring(root))
def create_precincts(w, cursor, config): """Writes all election administration information""" print "Creating precinct elements..." mem = Datastore(':memory:') c = mem.connect() c.execute("""CREATE TABLE IF NOT EXISTS Precinct_Polling ( polling_location_id INTEGER, precinct_id INTEGER, PRIMARY KEY(polling_location_id, precinct_id) )""") cursor.execute("""SELECT polling_location_id AS polling_location_id, precinct_id AS precinct_id FROM Precinct_Polling""") for row in cursor: c.execute("INSERT INTO Precinct_Polling VALUES (?,?)", (list(row))) mem.commit() cursor.execute( """SELECT P.id AS original_id, ?||P.id AS id, P.name, ?||P.locality_id AS locality_id, P.mail_only FROM Precinct P, Locality L WHERE P.locality_id = L.id AND P.locality_id IS NOT NULL AND P.name IS NOT NULL""", ( config.get('Precinct', 'precinct_prefix'), config.get('Locality', 'locality_prefix'), )) for row in cursor: root = ET.Element("precinct", id=unicode(row['id'])) name = ET.SubElement(root, "name") name.text = row['name'] locality_id = ET.SubElement(root, "locality_id") locality_id.text = unicode(row['locality_id']) if row['mail_only'] is not None and len(row['mail_only']) > 0: mail_only = ET.SubElement(root, "mail_only") mail_only.text = row['mail_only'] if row['mail_only'] == 'Yes': w.write(ET.tostring(root)) continue else: c.execute( """SELECT ?||polling_location_id AS polling_location_id FROM Precinct_Polling WHERE precinct_id=?""", ( config.get('Polling_Location', 'polling_prefix'), row['original_id'], )) for p in c: polling_location_id = ET.SubElement( root, "polling_location_id") polling_location_id.text = unicode( p['polling_location_id']) w.write(ET.tostring(root))