Guarantee close on store factory close

This commit is contained in:
lash 2022-04-20 15:24:32 +00:00
parent d133832e73
commit f00cb9564d
Signed by: lash
GPG Key ID: 21D2E7BB88C2A746
4 changed files with 29 additions and 4 deletions

15
shep/store/base.py Normal file
View File

@ -0,0 +1,15 @@
re_processedname = r'^_?[A-Z,\.]*$'
class StoreFactory:
def __del__(self):
self.close()
def add(self, k):
raise NotImplementedError()
def close(self):
pass

View File

@ -3,7 +3,10 @@ import os
import re
# local imports
from .base import re_processedname
from .base import (
re_processedname,
StoreFactory,
)
class SimpleFileStore:
@ -124,7 +127,7 @@ class SimpleFileStore:
pass
class SimpleFileStoreFactory:
class SimpleFileStoreFactory(StoreFactory):
"""Provide a method to instantiate SimpleFileStore instances that provide persistence for individual states.
:param path: Filesystem path as base path for states

View File

@ -4,6 +4,9 @@ import datetime
# external imports
import redis
# local imports
from .base import StoreFactory
class RedisStore:
@ -86,7 +89,7 @@ class RedisStore:
self.redis.set(k)
class RedisStoreFactory:
class RedisStoreFactory(StoreFactory):
def __init__(self, host='localhost', port=6379, db=0, binary=False):
self.redis = redis.Redis(host=host, port=port, db=db)

View File

@ -4,6 +4,10 @@ import datetime
# external imports
import rocksdb
# local imports
from .base import StoreFactory
class RocksDbStore:
def __init__(self, path, db, binary=False):
@ -109,7 +113,7 @@ class RocksDbStore:
self.db.set(k)
class RocksDbStoreFactory:
class RocksDbStoreFactory(StoreFactory):
def __init__(self, path, binary=False):
self.db = rocksdb.DB(path, rocksdb.Options(create_if_missing=True))