10 from optparse import OptionParser
12 def ev_create_stmt(heads):
13 create = 'create table if not exists ev (id int, time int, timeev int'
15 create += (', %s real' % (x,))
19 def ctx_create_stmt(heads):
20 create = 'create table if not exists ctx (id int unique on conflict ignore'
22 create += (', %s text' % (x,))
29 for line in fileinput.input(file):
30 items = re.split('\s+', line)
32 ev_heads.add(items[2])
34 ctx_heads.add(items[2])
35 return (ev_heads, ctx_heads)
37 def fill_tables(conn, file):
42 for line in fileinput.input(file):
43 items = re.split('\s+', line)
54 for i in xrange(0,len(keystack)):
56 dig.update(keystack[i])
58 dig.update(valstack[i])
68 stmt = 'insert into ctx (id'
71 stmt += ') values (' + str(id)
73 stmt += ', \'' + x + '\''
86 t = (id, val, time, timeev)
87 stmt = 'insert into ev (id, %s, time, timeev) values (?, ?, ?, ?)' % (key,)
88 conn.execute(stmt, (id, val, time, timeev))
92 parser = OptionParser('usage: %prog [options] <sqlite3 database> <event file>')
93 parser.add_option("-c", "--clean", action="store_true", help="clean existing data base before adding data")
94 (options, args) = parser.parse_args()
103 if not os.path.isfile(file):
104 print "cannot find input file %s" % (file, )
108 if os.path.isfile(db):
112 print "database %s already exists (use different name or use -c)" % (db, )
115 (ev_heads, ctx_heads) = find_heads(file)
117 conn = sqlite3.connect(db)
119 conn.execute("drop table ctx")
120 conn.execute("drop table ev")
122 conn.execute(ev_create_stmt(ev_heads))
123 conn.execute(ctx_create_stmt(ctx_heads))
124 fill_tables(conn, file)
127 if __name__ == "__main__":