Add option to override Table factory in PersistentState

This commit is contained in:
simon987 2022-05-05 15:02:48 -04:00
parent a51ad2cbb4
commit 78c04ef6f3
2 changed files with 16 additions and 15 deletions

View File

@ -9,20 +9,6 @@ from psycopg2.errorcodes import UNIQUE_VIOLATION
from hexlib.env import get_redis
class PersistentState:
"""Quick and dirty persistent dict-like SQLite wrapper"""
def __init__(self, dbfile="state.db", logger=None, **dbargs):
self.dbfile = dbfile
self.logger = logger
if dbargs is None:
dbargs = {"timeout": 30000}
self.dbargs = dbargs
def __getitem__(self, table):
return Table(self, table)
class VolatileState:
"""Quick and dirty volatile dict-like redis wrapper"""
@ -228,6 +214,21 @@ def _deserialize(value, col_type):
return value
class PersistentState:
"""Quick and dirty persistent dict-like SQLite wrapper"""
def __init__(self, dbfile="state.db", logger=None, table_factory=Table, **dbargs):
self.dbfile = dbfile
self.logger = logger
if dbargs is None:
dbargs = {"timeout": 30000}
self.dbargs = dbargs
self._table_factory = table_factory
def __getitem__(self, table):
return self._table_factory(self, table)
def pg_fetch_cursor_all(cur, name, batch_size=1000):
while True:
cur.execute("FETCH FORWARD %d FROM %s" % (batch_size, name))

View File

@ -2,7 +2,7 @@ from setuptools import setup
setup(
name="hexlib",
version="1.78",
version="1.79",
description="Misc utility methods",
author="simon987",
author_email="me@simon987.net",