Binary content option, sync all option

This commit is contained in:
lash 2022-03-17 19:16:33 +00:00
parent 57a9ea44ff
commit 8ccc89b4a5
Signed by: lash
GPG Key ID: 21D2E7BB88C2A746
6 changed files with 53 additions and 18 deletions

View File

@ -3,6 +3,8 @@
* Change method for atomic simultaneous set and unset * Change method for atomic simultaneous set and unset
* Optionally allow undefined composite states * Optionally allow undefined composite states
* Dynamic bits * Dynamic bits
* Optional binary contents
* Sync all if no state passed as argument
- 0.1.0 - 0.1.0
* Release version bump * Release version bump
- 0.0.19: - 0.0.19:

View File

@ -129,7 +129,7 @@ class PersistedState(State):
return to_state return to_state
def sync(self, state): def sync(self, state=None):
"""Reload resources for a single state in memory from the persisted state store. """Reload resources for a single state in memory from the persisted state store.
:param state: State to load :param state: State to load
@ -137,16 +137,24 @@ class PersistedState(State):
:raises StateItemExists: A content key is already recorded with a different state in memory than in persisted store. :raises StateItemExists: A content key is already recorded with a different state in memory than in persisted store.
# :todo: if sync state is none, sync all # :todo: if sync state is none, sync all
""" """
k = self.name(state) states = []
if state == None:
states = list(self.all())
else:
states = [self.name(state)]
self.__ensure_store(k) ks = []
for k in states:
ks.append(k)
for o in self.__stores[k].list(): for k in ks:
self.__ensure_store(k) self.__ensure_store(k)
try: for o in self.__stores[k].list():
super(PersistedState, self).put(o[0], state=state, contents=o[1]) state = self.from_name(k)
except StateItemExists: try:
pass super(PersistedState, self).put(o[0], state=state, contents=o[1])
except StateItemExists as e:
pass
def list(self, state): def list(self, state):

View File

@ -317,7 +317,7 @@ class State:
""" """
if state == None: if state == None:
state = getattr(self, self.base_state_name) state = getattr(self, self.base_state_name)
elif self.__reverse.get(state) == None: elif self.__reverse.get(state) == None and self.check_alias:
raise StateInvalid(state) raise StateInvalid(state)
self.__check_key(key) self.__check_key(key)
self.__add_state_list(state, key) self.__add_state_list(state, key)

View File

@ -8,10 +8,14 @@ class SimpleFileStore:
:param path: Filesystem base path for all state directory :param path: Filesystem base path for all state directory
:type path: str :type path: str
""" """
def __init__(self, path): def __init__(self, path, binary=False):
self.__path = path self.__path = path
os.makedirs(self.__path, exist_ok=True) os.makedirs(self.__path, exist_ok=True)
if binary:
self.__m = ['rb', 'wb']
else:
self.__m = ['r', 'w']
def add(self, k, contents=None): def add(self, k, contents=None):
"""Add a new key and optional contents """Add a new key and optional contents
@ -25,7 +29,7 @@ class SimpleFileStore:
if contents == None: if contents == None:
contents = '' contents = ''
f = open(fp, 'w') f = open(fp, self.__m[1])
f.write(contents) f.write(contents)
f.close() f.close()
@ -51,7 +55,7 @@ class SimpleFileStore:
:return: Contents :return: Contents
""" """
fp = os.path.join(self.__path, k) fp = os.path.join(self.__path, k)
f = open(fp, 'r') f = open(fp, self.__m[0])
r = f.read() r = f.read()
f.close() f.close()
return r return r
@ -66,7 +70,7 @@ class SimpleFileStore:
files = [] files = []
for p in os.listdir(self.__path): for p in os.listdir(self.__path):
fp = os.path.join(self.__path, p) fp = os.path.join(self.__path, p)
f = open(fp, 'r') f = open(fp, self.__m[0])
r = f.read() r = f.read()
f.close() f.close()
if len(r) == 0: if len(r) == 0:
@ -98,7 +102,7 @@ class SimpleFileStore:
""" """
fp = os.path.join(self.__path, k) fp = os.path.join(self.__path, k)
os.stat(fp) os.stat(fp)
f = open(fp, 'w') f = open(fp, self.__m[1])
r = f.write(contents) r = f.write(contents)
f.close() f.close()
@ -119,8 +123,9 @@ class SimpleFileStoreFactory:
:param path: Filesystem path as base path for states :param path: Filesystem path as base path for states
:type path: str :type path: str
""" """
def __init__(self, path): def __init__(self, path, binary=False):
self.__path = path self.__path = path
self.__binary = binary
def add(self, k): def add(self, k):
@ -133,4 +138,4 @@ class SimpleFileStoreFactory:
""" """
k = str(k) k = str(k)
store_path = os.path.join(self.__path, k) store_path = os.path.join(self.__path, k)
return SimpleFileStore(store_path) return SimpleFileStore(store_path, binary=self.__binary)

View File

@ -142,7 +142,7 @@ class TestStateReport(unittest.TestCase):
os.stat(fp) os.stat(fp)
def test_sync(self): def test_sync_one(self):
self.states.put('abcd', state=self.states.FOO, contents='foo') self.states.put('abcd', state=self.states.FOO, contents='foo')
self.states.put('xxx', state=self.states.FOO) self.states.put('xxx', state=self.states.FOO)
self.states.put('yyy', state=self.states.FOO) self.states.put('yyy', state=self.states.FOO)
@ -162,6 +162,25 @@ class TestStateReport(unittest.TestCase):
self.assertEqual(self.states.get('zzzz'), 'xyzzy') self.assertEqual(self.states.get('zzzz'), 'xyzzy')
def test_sync_all(self):
self.states.put('abcd', state=self.states.FOO)
self.states.put('xxx', state=self.states.BAR)
fp = os.path.join(self.d, 'FOO', 'abcd')
f = open(fp, 'w')
f.write('foofoo')
f.close()
fp = os.path.join(self.d, 'BAR', 'zzzz')
f = open(fp, 'w')
f.write('barbar')
f.close()
self.states.sync()
self.assertEqual(self.states.get('abcd'), None)
self.assertEqual(self.states.get('zzzz'), 'barbar')
def test_path(self): def test_path(self):
self.states.put('yyy', state=self.states.FOO) self.states.put('yyy', state=self.states.FOO)

View File

@ -80,5 +80,6 @@ class TestStateItems(unittest.TestCase):
self.assertIsNone(self.mockstore.v.get(item)) self.assertIsNone(self.mockstore.v.get(item))
if __name__ == '__main__': if __name__ == '__main__':
unittest.main() unittest.main()