ppbbww

Pillar Point boats, birds, and waves watcher
git clone git@abtrout.com:ppbbww.git
Log | Files | Refs | README | LICENSE

archive.py (1020B)


      1 import logging
      2 import sqlite3
      3 
      4 from contextlib import closing
      5 
      6 
      7 INIT_TABLES_SQL = """
      8 CREATE TABLE IF NOT EXISTS matches (
      9     ts,
     10     filename,
     11     label,
     12     score,
     13     x0, y0, x1, y1
     14 )"""
     15 
     16 ADD_MATCH_SQL = """
     17 INSERT INTO matches (ts, filename, label, score, x0, y0, x1, y1)
     18 VALUES (?, ?, ?, ?, ?, ?, ?, ?)"""
     19 
     20 LIST_MATCHES_SQL = """
     21 SELECT ts, filename, label, score, x0, y0, x1, y1
     22 FROM matches ORDER BY ts DESC"""
     23 
     24 
     25 class Archive:
     26     def __init__(self, db_file):
     27         self.con = sqlite3.connect(db_file)
     28         with closing(self.con.cursor()) as cur:
     29             cur.executescript(INIT_TABLES_SQL)
     30             self.con.commit()
     31 
     32     def add_match(self, *, ts, filename, label, score, box):
     33         with closing(self.con.cursor()) as cur:
     34             res = cur.execute(ADD_MATCH_SQL, (ts, filename, label, score, *box))
     35             self.con.commit()
     36             return res
     37 
     38     def list_matches(self):
     39         with closing(self.con.cursor()) as cur:
     40             return cur.execute(LIST_MATCHES_SQL).fetchall()
     41