shep

Multi-state key stores using bit masks for python3
git clone git://git.defalsify.org/shep.git
Log | Files | Refs | LICENSE

commit 6349ce2519b413d2232975716870a37831247acb
parent 40a73be7ebcc7b91ac313ece080ed8ba8bcc76a9
Author: lash <dev@holbrook.no>
Date:   Mon,  7 Nov 2022 00:21:48 +0000

Allow autogenerated alias in all list

Diffstat:
Mshep/persist.py | 6++----
Mshep/state.py | 26+++++++++++++++++++-------
Mtests/test_state.py | 2+-
3 files changed, 22 insertions(+), 12 deletions(-)

diff --git a/shep/persist.py b/shep/persist.py @@ -4,7 +4,6 @@ import datetime # local imports from .state import ( State, - to_elements, ) from .error import ( StateItemExists, @@ -149,7 +148,7 @@ class PersistedState(State): return to_state - def sync(self, state=None, not_state=None): + def sync(self, state=None, not_state=None, ignore_auto=True): """Reload resources for a single state in memory from the persisted state store. :param state: State to load @@ -160,7 +159,7 @@ class PersistedState(State): states_numeric = [] if state == None: - states_numeric = list(self.all(numeric=True)) + states_numeric = list(self.all(numeric=True, ignore_auto=ignore_auto)) else: states_numeric = [state] @@ -250,6 +249,5 @@ class PersistedState(State): def alias(self, key, *args): - v = to_elements(key) self.__ensure_store(key) super(PersistedState, self).alias(key, *args) diff --git a/shep/state.py b/shep/state.py @@ -15,8 +15,16 @@ from shep.error import ( re_name = r'^[a-zA-Z_\.]+$' -def to_elements(states): - return '_' + '.'.join(states) +def join_elements(states): + return '_' + '__'.join(states) + + +def split_elements(s): + if len(s) == 0: + return [] + if s[0] == '_': + s = s[1:] + return s.split('__') class State: @@ -212,6 +220,7 @@ class State: v = self.__check_limit(v | a, pure=False) if self.is_pure(v): raise ValueError('use add to add pure values') + k = k.replace('.', '__') return self.__set(k, v) @@ -231,7 +240,7 @@ class State: return self.__alias(k, *args) - def all(self, pure=False, numeric=False): + def all(self, pure=False, numeric=False, ignore_auto=True): """Return list of all unique atomic and alias state strings. :rtype: list of ints @@ -240,7 +249,7 @@ class State: l = [] for k in dir(self): state = None - if k[0] == '_': + if k[0] == '_' and ignore_auto: continue if k.upper() != k: continue @@ -264,7 +273,7 @@ class State: return self.base_state_name c = 1 for i in range(self.__bits): - if v & c > 0: + if (v & c) > 0: if numeric: r.append(c) else: @@ -274,14 +283,17 @@ class State: if numeric or not as_string: return r - return to_elements(r) #'_' + '.'.join(r) + if len(r) == 1: + return self.name(v) + + return join_elements(r) #'_' + '.'.join(r) def from_elements(self, k): r = 0 if k[0] != '_': raise ValueError('elements string must start with underscore (_), got {}'.format(k)) - for v in k[1:].split('.'): + for v in k[1:].split('__'): r |= self.from_name(v) return r diff --git a/tests/test_state.py b/tests/test_state.py @@ -124,7 +124,7 @@ class TestState(unittest.TestCase): states.set('abcd', states.BAZ) v = states.state('abcd') s = states.name(v) - self.assertEqual(s, '_FOO.BAZ') + self.assertEqual(s, '_FOO__BAZ') def test_peek(self):