5 Commits

Author SHA1 Message Date
lash
ee6820ef60 Handle missing files in filesystem store list 2022-05-05 15:44:41 +00:00
lash
1951fcda8a Ensure atomicity of fs lock 2022-05-05 15:10:05 +00:00
lash
440fab9e70 Bump version 2022-05-04 05:38:51 +00:00
lash
b53b729ea1 Handle missing branch for sync with no not-state filter 2022-05-02 19:59:22 +00:00
lash
714bf79d22 WIP selective state sync 2022-05-02 11:21:07 +00:00
6 changed files with 80 additions and 21 deletions

View File

@@ -1,3 +1,9 @@
- 0.2.7
* Handle missing files in fs readdir list
- 0.2.6
* Ensure atomic state lock in fs
- 0.2.5
* Correct handling of persistent sync when no not-state filter has been set
- 0.2.4
* Add optional concurrency lock for persistence store, implemented for file store
- 0.2.3

View File

@@ -1,6 +1,6 @@
[metadata]
name = shep
version = 0.2.4
version = 0.2.7
description = Multi-state key stores using bit masks
author = Louis Holbrook
author_email = dev@holbrook.no

View File

@@ -144,7 +144,7 @@ class PersistedState(State):
return to_state
def sync(self, state=None):
def sync(self, state=None, not_state=None):
"""Reload resources for a single state in memory from the persisted state store.
:param state: State to load
@@ -153,11 +153,19 @@ class PersistedState(State):
# :todo: if sync state is none, sync all
"""
states = []
states_numeric = []
if state == None:
states = list(self.all())
states_numeric = list(self.all(numeric=True))
else:
states = [self.name(state)]
states_numeric = [state]
states = []
for state in states_numeric:
if not_state != None:
if state & not_state == 0:
states.append(self.name(state))
else:
states.append(self.name(state))
ks = []
for k in states:

View File

@@ -217,14 +217,15 @@ class State:
return self.__alias(k, *args)
def all(self, pure=False):
"""Return list of all unique atomic and alias states.
def all(self, pure=False, numeric=False):
"""Return list of all unique atomic and alias state strings.
:rtype: list of ints
:return: states
"""
l = []
for k in dir(self):
state = None
if k[0] == '_':
continue
if k.upper() != k:
@@ -233,7 +234,12 @@ class State:
state = self.from_name(k)
if not self.__is_pure(state):
continue
l.append(k)
if numeric:
if state == None:
state = self.from_name(k)
l.append(state)
else:
l.append(k)
l.sort()
return l
@@ -628,3 +634,23 @@ class State:
statemask = ~statemask
statemask &= self.__limit
return statemask
def purge(self, key):
state = self.state(key)
state_name = self.name(state)
v = self.__keys.get(state)
v.remove(key)
del self.__keys_reverse[key]
try:
del self.__contents[key]
except KeyError:
pass
try:
del self.modified_last[key]
except KeyError:
pass

View File

@@ -28,22 +28,17 @@ class SimpleFileStore:
os.makedirs(lock_path, exist_ok=True)
def __is_locked(self, k):
if self.__lock_path == None:
return False
for v in os.listdir(self.__lock_path):
if k == v:
return True
return False
def __lock(self, k):
if self.__lock_path == None:
return
if self.__is_locked(k):
raise StateLockedKey(k)
fp = os.path.join(self.__lock_path, k)
f = open(fp, 'w')
f = None
try:
f = open(fp, 'x')
except FileExistsError:
pass
if f == None:
raise StateLockedKey(k)
f.close()
@@ -120,7 +115,11 @@ class SimpleFileStore:
files = []
for p in os.listdir(self.__path):
fp = os.path.join(self.__path, p)
f = open(fp, self.__m[0])
f = None
try:
f = open(fp, self.__m[0])
except FileNotFoundError:
continue
r = f.read()
f.close()
if len(r) == 0:

View File

@@ -7,6 +7,7 @@ from shep import State
from shep.error import (
StateExists,
StateInvalid,
StateItemNotFound,
)
logging.basicConfig(level=logging.DEBUG)
@@ -250,5 +251,24 @@ class TestState(unittest.TestCase):
self.assertEqual(mask, states.ALL)
def test_remove(self):
states = State(1)
states.add('foo')
states.put('xyzzy', contents='plugh')
v = states.get('xyzzy')
self.assertEqual(v, 'plugh')
states.next('xyzzy')
v = states.state('xyzzy')
self.assertEqual(states.FOO, v)
states.purge('xyzzy')
with self.assertRaises(StateItemNotFound):
states.state('xyzzy')
if __name__ == '__main__':
unittest.main()