find in group or meta should return GroupConfig (and not KerneleGroupConfig) and remote 'first' attributs

This commit is contained in:
Emmanuel Garette 2018-10-07 16:29:56 +02:00
parent 5b5f06a612
commit e4362e9b09
3 changed files with 52 additions and 47 deletions

View File

@ -248,26 +248,26 @@ def test_meta_meta_set():
assert meta.config('meta.conf1').option('od1.i1').value.get() == meta.config('meta.conf2').option('od1.i1').value.get() == 7 assert meta.config('meta.conf1').option('od1.i1').value.get() == meta.config('meta.conf2').option('od1.i1').value.get() == 7
# #
dconfigs = [] dconfigs = []
for conf in meta.config.find('i1', value=7, first=True).config.list(): for conf in meta.config.find('i1', value=7).config.list():
dconfigs.append(conf._config_bag.context) dconfigs.append(conf._config_bag.context)
assert [conf1, conf2] == dconfigs assert [conf1, conf2] == dconfigs
meta.config('meta.conf1').option('od1.i1').value.set(8) meta.config('meta.conf1').option('od1.i1').value.set(8)
# #
dconfigs = [] dconfigs = []
for conf in meta.config.find('i1', first=True).config.list(): for conf in meta.config.find('i1').config.list():
dconfigs.append(conf._config_bag.context) dconfigs.append(conf._config_bag.context)
assert [conf1, conf2] == dconfigs assert [conf1, conf2] == dconfigs
assert conf2 == list(meta.config.find('i1', value=7, first=True).config.list())[0]._config_bag.context assert conf2 == list(meta.config.find('i1', value=7).config.list())[0]._config_bag.context
assert conf1 == list(meta.config.find('i1', value=8, first=True).config.list())[0]._config_bag.context assert conf1 == list(meta.config.find('i1', value=8).config.list())[0]._config_bag.context
# #
dconfigs = [] dconfigs = []
for conf in meta.config.find('i5', value=2, first=True).config.list(): for conf in meta.config.find('i5', value=2).config.list():
dconfigs.append(conf._config_bag.context) dconfigs.append(conf._config_bag.context)
assert [conf1, conf2] == dconfigs assert [conf1, conf2] == dconfigs
# #
raises(AttributeError, "meta.config.find('i1', value=10, first=True)") raises(AttributeError, "meta.config.find('i1', value=10)")
raises(AttributeError, "meta.config.find('not', value=10, first=True)") raises(AttributeError, "meta.config.find('not', value=10)")
raises(AttributeError, "meta.config.find('i6', first=True)") raises(AttributeError, "meta.config.find('i6')")
raises(ValueError, "meta.value.set('od1.i6', 7, only_config=True, force_default=True)") raises(ValueError, "meta.value.set('od1.i6', 7, only_config=True, force_default=True)")
raises(ValueError, "meta.value.set('od1.i6', 7, only_config=True, force_default_if_same=True)") raises(ValueError, "meta.value.set('od1.i6', 7, only_config=True, force_default_if_same=True)")
raises(ValueError, "meta.value.set('od1.i6', 7, only_config=True, force_dont_change_value=True)") raises(ValueError, "meta.value.set('od1.i6', 7, only_config=True, force_dont_change_value=True)")
@ -303,9 +303,17 @@ def test_group_find_firsts():
conf1 = Config(od2, session_id='conf1') conf1 = Config(od2, session_id='conf1')
conf2 = Config(od2, session_id='conf2') conf2 = Config(od2, session_id='conf2')
grp = GroupConfig([conf1, conf2]) grp = GroupConfig([conf1, conf2])
itr = grp.config.find('i1', first=True).config.list() itr = grp.config.find('i1').config.list()
conf1._config_bag.context == next(itr)._config_bag.context conf1._config_bag.context == next(itr)._config_bag.context
conf2._config_bag.context == next(itr)._config_bag.context conf2._config_bag.context == next(itr)._config_bag.context
try:
next(itr)
except StopIteration:
pass
except:
raise Exception('no')
else:
raise Exception('no')
def test_group_group(): def test_group_group():
@ -352,19 +360,19 @@ def test_meta_master_slaves():
conf2 = Config(od, session_id='conf2') conf2 = Config(od, session_id='conf2')
meta = MetaConfig([conf1, conf2]) meta = MetaConfig([conf1, conf2])
meta.property.read_only() meta.property.read_only()
itr = meta.config.find('ip_admin_eth0', first=True).config.list() itr = meta.config.find('ip_admin_eth0').config.list()
assert conf1._config_bag.context == next(itr)._config_bag.context assert conf1._config_bag.context == next(itr)._config_bag.context
assert conf2._config_bag.context == next(itr)._config_bag.context assert conf2._config_bag.context == next(itr)._config_bag.context
itr = meta.config.find('netmask_admin_eth0', first=True).config.list() itr = meta.config.find('netmask_admin_eth0').config.list()
assert conf1._config_bag.context == next(itr)._config_bag.context assert conf1._config_bag.context == next(itr)._config_bag.context
assert conf2._config_bag.context == next(itr)._config_bag.context assert conf2._config_bag.context == next(itr)._config_bag.context
meta.property.read_write() meta.property.read_write()
raises(AttributeError, "meta.config.find('netmask_admin_eth0', first=True)") raises(AttributeError, "meta.config.find('netmask_admin_eth0')")
itr = meta.unrestraint.config.find('netmask_admin_eth0', first=True).config.list() itr = meta.unrestraint.config.find('netmask_admin_eth0').config.list()
assert conf1._config_bag.context == next(itr)._config_bag.context assert conf1._config_bag.context == next(itr)._config_bag.context
assert conf2._config_bag.context == next(itr)._config_bag.context assert conf2._config_bag.context == next(itr)._config_bag.context
meta.property.read_only() meta.property.read_only()
itr = meta.config.find('netmask_admin_eth0', first=True).config.list() itr = meta.config.find('netmask_admin_eth0').config.list()
assert conf1._config_bag.context == next(itr)._config_bag.context assert conf1._config_bag.context == next(itr)._config_bag.context
assert conf2._config_bag.context == next(itr)._config_bag.context assert conf2._config_bag.context == next(itr)._config_bag.context

View File

@ -1108,15 +1108,11 @@ class _TiramisuContextGroupConfig(TiramisuContext):
def find(self, def find(self,
name: str, name: str,
value=undefined, value=undefined):
first: bool=False): """Find an or a list of config with finding option"""
"""Find an or a list of options""" return GroupConfig(self._config_bag.context.find_group(byname=name,
if first:
return Config(self._config_bag.context.find_firsts(byname=name,
byvalue=value, byvalue=value,
config_bag=self._config_bag)) config_bag=self._config_bag))
else:
raise APIError('not implemented yet')
def __call__(self, def __call__(self,
path: Optional[str]): path: Optional[str]):
@ -1258,7 +1254,10 @@ class GroupConfig(TiramisuAPI):
session_id: Union[str, None]=None) -> None: session_id: Union[str, None]=None) -> None:
_children = [] _children = []
for child in children: for child in children:
if isinstance(child, TiramisuAPI):
_children.append(child._config_bag.context) _children.append(child._config_bag.context)
else:
_children.append(child)
config = KernelGroupConfig(_children, config = KernelGroupConfig(_children,
session_id=session_id) session_id=session_id)

View File

@ -871,7 +871,7 @@ class KernelGroupConfig(_CommonConfig):
return ret return ret
def find_firsts(self, def find_group(self,
config_bag, config_bag,
byname=None, byname=None,
bypath=undefined, bypath=undefined,
@ -898,7 +898,7 @@ class KernelGroupConfig(_CommonConfig):
ret = [] ret = []
for child in self._impl_children: for child in self._impl_children:
if isinstance(child, KernelGroupConfig): if isinstance(child, KernelGroupConfig):
ret.extend(child.find_firsts(byname=byname, ret.extend(child.find_group(byname=byname,
bypath=bypath, bypath=bypath,
byoption=byoption, byoption=byoption,
byvalue=byvalue, byvalue=byvalue,
@ -919,12 +919,10 @@ class KernelGroupConfig(_CommonConfig):
ret.append(child) ret.append(child)
except StopIteration: except StopIteration:
pass pass
if _sub: if not _sub:
return ret
else:
self._find_return_results(ret != [], self._find_return_results(ret != [],
raise_if_not_found) raise_if_not_found)
return KernelGroupConfig(ret) return ret
def impl_getname(self): def impl_getname(self):
return self._impl_name return self._impl_name