Allow autogenerated alias in all list

This commit is contained in:
lash 2022-11-07 00:21:48 +00:00
parent 40a73be7eb
commit 6349ce2519
Signed by: lash
GPG Key ID: 21D2E7BB88C2A746
3 changed files with 22 additions and 12 deletions

View File

@ -4,7 +4,6 @@ import datetime
# local imports # local imports
from .state import ( from .state import (
State, State,
to_elements,
) )
from .error import ( from .error import (
StateItemExists, StateItemExists,
@ -149,7 +148,7 @@ class PersistedState(State):
return to_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. """Reload resources for a single state in memory from the persisted state store.
:param state: State to load :param state: State to load
@ -160,7 +159,7 @@ class PersistedState(State):
states_numeric = [] states_numeric = []
if state == None: if state == None:
states_numeric = list(self.all(numeric=True)) states_numeric = list(self.all(numeric=True, ignore_auto=ignore_auto))
else: else:
states_numeric = [state] states_numeric = [state]
@ -250,6 +249,5 @@ class PersistedState(State):
def alias(self, key, *args): def alias(self, key, *args):
v = to_elements(key)
self.__ensure_store(key) self.__ensure_store(key)
super(PersistedState, self).alias(key, *args) super(PersistedState, self).alias(key, *args)

View File

@ -15,8 +15,16 @@ from shep.error import (
re_name = r'^[a-zA-Z_\.]+$' re_name = r'^[a-zA-Z_\.]+$'
def to_elements(states): def join_elements(states):
return '_' + '.'.join(states) return '_' + '__'.join(states)
def split_elements(s):
if len(s) == 0:
return []
if s[0] == '_':
s = s[1:]
return s.split('__')
class State: class State:
@ -212,6 +220,7 @@ class State:
v = self.__check_limit(v | a, pure=False) v = self.__check_limit(v | a, pure=False)
if self.is_pure(v): if self.is_pure(v):
raise ValueError('use add to add pure values') raise ValueError('use add to add pure values')
k = k.replace('.', '__')
return self.__set(k, v) return self.__set(k, v)
@ -231,7 +240,7 @@ class State:
return self.__alias(k, *args) 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. """Return list of all unique atomic and alias state strings.
:rtype: list of ints :rtype: list of ints
@ -240,7 +249,7 @@ class State:
l = [] l = []
for k in dir(self): for k in dir(self):
state = None state = None
if k[0] == '_': if k[0] == '_' and ignore_auto:
continue continue
if k.upper() != k: if k.upper() != k:
continue continue
@ -264,7 +273,7 @@ class State:
return self.base_state_name return self.base_state_name
c = 1 c = 1
for i in range(self.__bits): for i in range(self.__bits):
if v & c > 0: if (v & c) > 0:
if numeric: if numeric:
r.append(c) r.append(c)
else: else:
@ -274,14 +283,17 @@ class State:
if numeric or not as_string: if numeric or not as_string:
return r 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): def from_elements(self, k):
r = 0 r = 0
if k[0] != '_': if k[0] != '_':
raise ValueError('elements string must start with underscore (_), got {}'.format(k)) 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) r |= self.from_name(v)
return r return r

View File

@ -124,7 +124,7 @@ class TestState(unittest.TestCase):
states.set('abcd', states.BAZ) states.set('abcd', states.BAZ)
v = states.state('abcd') v = states.state('abcd')
s = states.name(v) s = states.name(v)
self.assertEqual(s, '_FOO.BAZ') self.assertEqual(s, '_FOO__BAZ')
def test_peek(self): def test_peek(self):