class Lockable: _lock -> _locked, lock() -> _lock()
This commit is contained in:
parent
f1b427fd9c
commit
5dac884a39
5 changed files with 27 additions and 21 deletions
|
|
@ -30,7 +30,7 @@ class AttrCtrlMeta(type):
|
|||
def __call__(cls,*args,**kwargs):
|
||||
instance = super().__call__(*args,**kwargs)
|
||||
if instance._autolock:
|
||||
instance.lock()
|
||||
instance._lock()
|
||||
return instance
|
||||
|
||||
class AttrCtrl(metaclass=AttrCtrlMeta):
|
||||
|
|
@ -46,24 +46,24 @@ class AttrCtrl(metaclass=AttrCtrlMeta):
|
|||
instead of raising AttributeError.
|
||||
"""
|
||||
_autolock = True
|
||||
_lock = False
|
||||
_locked = False
|
||||
_use_class_attr = False
|
||||
_default_to_none = False
|
||||
_skip_type_check = ()
|
||||
|
||||
def lock(self):
|
||||
self._lock = True
|
||||
def _lock(self):
|
||||
self._locked = True
|
||||
|
||||
def __getattr__(self,name):
|
||||
if self._lock and self._default_to_none:
|
||||
if self._locked and self._default_to_none:
|
||||
return None
|
||||
else:
|
||||
raise AttributeError(f'{type(self).__name__} object has no attribute {name!r}')
|
||||
|
||||
def __setattr__(self,name,value):
|
||||
|
||||
if self._lock:
|
||||
assert name != '_lock', 'lock can be set only once'
|
||||
if self._locked:
|
||||
assert name != '_locked', 'lock can be set only once'
|
||||
|
||||
def do_error(name,value,ref_val):
|
||||
raise AttributeError(
|
||||
|
|
@ -103,15 +103,15 @@ class Lockable(AttrCtrl):
|
|||
_set_ok = ()
|
||||
_reset_ok = ()
|
||||
|
||||
def lock(self):
|
||||
def _lock(self):
|
||||
for name in ('_set_ok','_reset_ok'):
|
||||
for attr in getattr(self,name):
|
||||
assert hasattr(self,attr), (
|
||||
f'attribute {attr!r} in {name!r} not found in {type(self).__name__} object {id(self)}' )
|
||||
super().lock()
|
||||
super()._lock()
|
||||
|
||||
def __setattr__(self,name,value):
|
||||
if self._lock and (name in self.__dict__ or hasattr(type(self),name)):
|
||||
if self._locked and (name in self.__dict__ or hasattr(type(self),name)):
|
||||
val = getattr(self,name)
|
||||
if name not in (self._set_ok + self._reset_ok):
|
||||
raise AttributeError(f'attribute {name!r} of {type(self).__name__} object is read-only')
|
||||
|
|
|
|||
|
|
@ -26,7 +26,7 @@ from .util import fmt_list,die
|
|||
class ClassFlags(AttrCtrl):
|
||||
_name = 'flags'
|
||||
_desc = 'flag'
|
||||
reserved_attrs = ('lock',)
|
||||
reserved_attrs = ()
|
||||
|
||||
def __init__(self,parent,arg):
|
||||
self._parent = parent
|
||||
|
|
@ -59,7 +59,7 @@ class ClassFlags(AttrCtrl):
|
|||
|
||||
def __setattr__(self,name,val):
|
||||
|
||||
if self._lock:
|
||||
if self._locked:
|
||||
|
||||
if name not in self._available:
|
||||
self.not_available_error(name)
|
||||
|
|
|
|||
|
|
@ -400,7 +400,7 @@ def init(
|
|||
cfg._uopts = po.user_opts
|
||||
cfg._args = po.cmd_args
|
||||
|
||||
cfg.lock()
|
||||
cfg._lock()
|
||||
|
||||
if need_proto:
|
||||
from .protocol import warn_trustlevel,init_proto_from_cfg
|
||||
|
|
|
|||
|
|
@ -12,6 +12,9 @@ class unit_test(object):
|
|||
|
||||
def run_test(self,name,ut):
|
||||
|
||||
class MyClassOpts(ClassOpts):
|
||||
reserved_attrs = ('foo',)
|
||||
|
||||
class cls1:
|
||||
avail_opts = ()
|
||||
avail_flags = ()
|
||||
|
|
@ -27,7 +30,10 @@ class unit_test(object):
|
|||
avail_opts = ('_foo',)
|
||||
|
||||
class cls4(cls1):
|
||||
avail_opts = ('lock',)
|
||||
avail_opts = ('foo',)
|
||||
def __init__(self,opts=None,flags=None):
|
||||
self.opt = MyClassOpts(self,opts)
|
||||
self.flag = MyClassFlags(self,flags)
|
||||
|
||||
def test_flags():
|
||||
def gen():
|
||||
|
|
|
|||
|
|
@ -20,7 +20,7 @@ class unit_test(object):
|
|||
_autolock = False
|
||||
foo = 'fooval'
|
||||
ac = MyAttrCtrl()
|
||||
ac.lock()
|
||||
ac._lock()
|
||||
|
||||
ac.foo = 'new fooval'
|
||||
ac.foo = 'new fooval2'
|
||||
|
|
@ -34,7 +34,7 @@ class unit_test(object):
|
|||
foo = 'fooval'
|
||||
bar = None
|
||||
acc = MyAttrCtrlClsCheck()
|
||||
acc.lock()
|
||||
acc._lock()
|
||||
|
||||
acc.foo = 'new_fooval'
|
||||
acc.foo = 'new_fooval2'
|
||||
|
|
@ -72,7 +72,7 @@ class unit_test(object):
|
|||
lc.delta = 0.0 # yes
|
||||
lc.epsilon = [] # no
|
||||
|
||||
lc.lock()
|
||||
lc._lock()
|
||||
|
||||
lc.foo = 'fooval2'
|
||||
lc.bar = 'barval2'
|
||||
|
|
@ -93,7 +93,7 @@ class unit_test(object):
|
|||
qux = 'quxval'
|
||||
|
||||
lcc = MyLockableClsCheck()
|
||||
lcc.lock()
|
||||
lcc._lock()
|
||||
|
||||
lcc.foo = 'fooval2' # class attribute foo is None, so can be set to any type
|
||||
lcc.bar = 2
|
||||
|
|
@ -110,7 +110,7 @@ class unit_test(object):
|
|||
|
||||
lca = MyLockableAutolock()
|
||||
assert lca._autolock == True
|
||||
assert lca._lock == True
|
||||
assert lca._locked == True
|
||||
assert lca.foo == True
|
||||
|
||||
class MyLockableAutolockDflNone(Lockable):
|
||||
|
|
@ -148,7 +148,7 @@ class unit_test(object):
|
|||
|
||||
def bad16(): lca.foo = None
|
||||
def bad17(): lb = MyLockableBad()
|
||||
def bad18(): aca.lock()
|
||||
def bad18(): aca._lock()
|
||||
|
||||
def bad19(): acdn.baz = None
|
||||
def bad20(): lcdn.foo = 1
|
||||
|
|
@ -175,7 +175,7 @@ class unit_test(object):
|
|||
("attr (can't reset)", 'AttributeError', 'reset', bad15 ),
|
||||
("attr (can't set)", 'AttributeError', 'read-only', bad16 ),
|
||||
("attr (bad _set_ok)", 'AssertionError', 'not found in',bad17 ),
|
||||
("call to lock()", 'AssertionError', 'only once', bad18 ),
|
||||
("call to _lock()", 'AssertionError', 'only once', bad18 ),
|
||||
))
|
||||
|
||||
qmsg('OK')
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue