]> www.average.org Git - loctrkd.git/blob - gps303/evstore.py
Accept various arg formats for `Out` construction
[loctrkd.git] / gps303 / evstore.py
1 """ sqlite event store """
2
3 from sqlite3 import connect
4
5 __all__ = "initdb", "stow"
6
7 DB = None
8
9 SCHEMA = """create table if not exists events (
10     tstamp real not null,
11     imei text,
12     peeraddr text not null,
13     proto int not null,
14     packet blob
15 )"""
16
17
18 def initdb(dbname):
19     global DB
20     DB = connect(dbname)
21     DB.execute(SCHEMA)
22
23
24 def stow(**kwargs):
25     assert DB is not None
26     parms = {
27         k: kwargs[k] if k in kwargs else v
28         for k, v in (
29             ("peeraddr", None),
30             ("when", 0.0),
31             ("imei", None),
32             ("proto", -1),
33             ("packet", b""),
34         )
35     }
36     assert len(kwargs) <= len(parms)
37     DB.execute(
38         """insert or ignore into events
39                 (tstamp, imei, peeraddr, proto, packet)
40                 values
41                 (:when, :imei, :peeraddr, :proto, :packet)
42         """,
43         parms,
44     )
45     DB.commit()