simplify api

This commit is contained in:
Emmanuel Garette 2018-08-02 23:35:07 +02:00
parent a1cb60237a
commit fdbb0d83ff
1 changed files with 29 additions and 55 deletions

View File

@ -109,19 +109,20 @@ class CommonTiramisu(TiramisuHelp):
option = self.option_bag.option
if option is None:
option = self.subconfig.cfgimpl_get_description().impl_getchild(self._name,
self.config_bag,
self.option_bag.config_bag,
self.subconfig)
self.option_bag.set_option(option,
self._path,
self.index,
self.config_bag)
if self.index is not None:
self.option_bag.path,
self.option_bag.index,
self.option_bag.config_bag)
index = self.option_bag.index
if index is not None:
if option.impl_is_optiondescription() or not option.impl_is_master_slaves('slave'):
raise APIError('index must be set only with a slave option')
self._length = self.subconfig.cfgimpl_get_length_slave(self.option_bag)
if self.index >= self._length:
if index >= self._length:
raise SlaveError(_('index "{}" is higher than the master length "{}" '
'for option "{}"').format(self.index,
'for option "{}"').format(index,
self._length,
option.impl_get_display_name()))
if not self.allow_optiondescription and option.impl_is_optiondescription():
@ -135,27 +136,21 @@ class CommonTiramisuOption(CommonTiramisu):
def __init__(self,
name: str,
path: str,
index: int,
subconfig: Union[Config, SubConfig],
config_bag: ConfigBag,
option_bag: OptionBag) -> None:
self._path = path
self.index = index
self.config_bag = config_bag
self.option_bag = option_bag
self._name = name
self.subconfig = subconfig
self._get_option()
if config_bag is not None and self.slave_need_index:
if option_bag.config_bag is not None and self.slave_need_index:
self._test_slave_index()
def _test_slave_index(self) -> None:
option = self.option_bag.option
if not option.impl_is_optiondescription():
if self.index is None and option.impl_is_master_slaves('slave'):
if self.option_bag.index is None and option.impl_is_master_slaves('slave'):
raise APIError('index must be set with a slave option')
elif self.index is not None and not option.impl_is_master_slaves('slave'):
elif self.option_bag.index is not None and not option.impl_is_master_slaves('slave'):
raise APIError('index must be set only with a slave option')
def __getattr__(self, name):
@ -206,13 +201,11 @@ class TiramisuOptionOption(CommonTiramisuOption):
def name(self):
"""get option name"""
option = self.option_bag.option
return self._name
def path(self) -> str:
"""get option path"""
option = self.option_bag.option
return self._path
return self.option_bag.path
def _default(self):
"""get default value for an option (not for optiondescription)"""
@ -246,7 +239,7 @@ class TiramisuOptionOption(CommonTiramisuOption):
def __getattr__(self, name: str) -> Callable:
option = self.option_bag.option
if not option.impl_is_optiondescription():
if not option.impl_is_optiondescription() and not name.startswith('_'):
return getattr(self, '_' + name)
raise APIError(_('{} is unknown').format(name))
@ -261,20 +254,13 @@ class TiramisuOptionOwner(CommonTiramisuOption):
def __init__(self,
name: str,
path: str,
index: int,
subconfig: Union[Config, SubConfig],
config_bag: ConfigBag,
option_bag: OptionBag) -> None:
super().__init__(name,
path,
index,
subconfig,
config_bag,
option_bag)
if config_bag:
self.values = self.config_bag.context.cfgimpl_get_values()
self.values = self.option_bag.config_bag.context.cfgimpl_get_values()
def get(self):
"""get owner for a specified option"""
@ -305,19 +291,13 @@ class TiramisuOptionProperty(CommonTiramisuOption):
def __init__(self,
name: str,
path: str,
index: int,
subconfig: Union[Config, SubConfig],
config_bag: ConfigBag,
option_bag: OptionBag) -> None:
super().__init__(name,
path,
index,
subconfig,
config_bag,
option_bag)
if config_bag:
self.settings = config_bag.context.cfgimpl_get_settings()
if option_bag.config_bag:
self.settings = option_bag.config_bag.context.cfgimpl_get_settings()
def get(self, apply_requires=True):
"""get properties for an option"""
@ -337,7 +317,7 @@ class TiramisuOptionProperty(CommonTiramisuOption):
' '.join(prop)))
props = self.settings.getproperties(self.option_bag,
apply_requires=False)
self.settings.setproperties(self._path,
self.settings.setproperties(self.option_bag.path,
props | {prop},
self.option_bag)
@ -346,7 +326,7 @@ class TiramisuOptionProperty(CommonTiramisuOption):
option = self.option_bag.option
props = self.settings.getproperties(self.option_bag,
apply_requires=False)
self.settings.setproperties(self._path,
self.settings.setproperties(self.option_bag.path,
props - {prop},
self.option_bag)
@ -363,24 +343,18 @@ class TiramisuOptionPermissive(CommonTiramisuOption):
def __init__(self,
name: str,
path: str,
index: int,
subconfig: Union[Config, SubConfig],
config_bag: ConfigBag,
option_bag: OptionBag) -> None:
super().__init__(name,
path,
index,
subconfig,
config_bag,
option_bag)
if config_bag:
self.settings = config_bag.context.cfgimpl_get_settings()
if option_bag.config_bag:
self.settings = option_bag.config_bag.context.cfgimpl_get_settings()
def get(self):
"""get permissives value"""
option = self.option_bag.option
return self.settings.getpermissive(option, self._path)
return self.settings.getpermissive(option, self.option_bag.path)
def set(self, permissives):
"""set permissives value"""
@ -429,7 +403,7 @@ class TiramisuOptionValue(CommonTiramisuOption):
"""set a value for a specified option"""
option = self.option_bag.option
self._test_slave_index()
values = self.config_bag.context.cfgimpl_get_values()
values = self.option_bag.config_bag.context.cfgimpl_get_values()
if isinstance(value, list):
while undefined in value:
idx = value.index(undefined)
@ -437,7 +411,7 @@ class TiramisuOptionValue(CommonTiramisuOption):
option_bag.set_option(self.option_bag.option,
self.option_bag.path,
idx,
self.config_bag)
self.option_bag.config_bag)
value[idx] = values.getdefaultvalue(option_bag)
else:
if value == undefined:
@ -449,7 +423,7 @@ class TiramisuOptionValue(CommonTiramisuOption):
"""pop value for a master option (only for master option)"""
if self.option_bag.option.impl_is_symlinkoption():
raise TypeError(_("can't delete a SymLinkOption"))
self.config_bag.context.cfgimpl_get_values().reset_master(index,
self.option_bag.config_bag.context.cfgimpl_get_values().reset_master(index,
self.option_bag,
self.subconfig)
@ -519,16 +493,16 @@ class TiramisuOption(CommonTiramisu):
self.option_bag = option_bag
else:
self.option_bag = OptionBag()
self.option_bag.path = self._path
self.option_bag.index = self.index
self.option_bag.config_bag = self.config_bag
if not self.registers:
registers(self.registers, self.__class__.__name__)
def __getattr__(self, subfunc: str) -> Any:
if subfunc in self.registers:
return self.registers[subfunc](self._name,
self._path,
self.index,
self.subconfig,
self.config_bag,
self.option_bag)
elif self._get_option().impl_is_optiondescription():
return getattr(self, '_' + subfunc)