X-Git-Url: http://average.org/gitweb/?a=blobdiff_plain;f=gps303%2Fevstore.py;fp=gps303%2Fevstore.py;h=70a3ac6618bec9bd67f53ff7fa9a30a6d92484c4;hb=623ff0272e0344c26549bbed937d1d33b9822d18;hp=b1950210555adb532e0cf645941857ca95bc3131;hpb=80f2fab3fcdf6cd962b0d81f385169dbb7431076;p=loctrkd.git diff --git a/gps303/evstore.py b/gps303/evstore.py index b195021..70a3ac6 100644 --- a/gps303/evstore.py +++ b/gps303/evstore.py @@ -1,16 +1,17 @@ +""" sqlite event store """ + from sqlite3 import connect -__all__ = ("initdb", "stow") +__all__ = "initdb", "stow" DB = None SCHEMA = """create table if not exists events ( - timestamp real not null, + tstamp real not null, imei text, - clntaddr text not null, - length int, + peeraddr text not null, proto int not null, - payload blob + packet blob )""" @@ -20,19 +21,24 @@ def initdb(dbname): DB.execute(SCHEMA) -def stow(clntaddr, timestamp, imei, length, proto, payload): +def stow(**kwargs): assert DB is not None - parms = dict( - zip( - ("clntaddr", "timestamp", "imei", "length", "proto", "payload"), - (str(clntaddr), timestamp, imei, length, proto, payload), + parms = { + k: kwargs[k] if k in kwargs else v + for k, v in ( + ("peeraddr", None), + ("when", 0.0), + ("imei", None), + ("proto", -1), + ("packet", b""), ) - ) + } + assert len(kwargs) <= len(parms) DB.execute( """insert or ignore into events - (timestamp, imei, clntaddr, length, proto, payload) + (tstamp, imei, peeraddr, proto, packet) values - (:timestamp, :imei, :clntaddr, :length, :proto, :payload) + (:when, :imei, :peeraddr, :proto, :packet) """, parms, )