support of config.option(path).option(path)

This commit is contained in:
Emmanuel Garette 2019-07-26 08:57:10 +02:00
parent 4e797674ef
commit 61dbcea61d
1 changed files with 31 additions and 76 deletions

View File

@ -51,15 +51,28 @@ class Option:
class TiramisuOptionOption: class TiramisuOptionOption:
# config.option(path).option # config.option(path).option
def __call__(self,
path: str,
index: Optional[int]=None) -> 'TiramisuOption':
# config.option(path).option(path)
path = self._path + '.' + path
schema = self.config.get_schema(path)
if schema['type'] in ['object', 'array']:
return TiramisuOptionDescription(self.config,
schema,
path)
return TiramisuOption(self.config,
schema,
path,
index)
def __init__(self, def __init__(self,
config: 'Config', config: 'Config',
path: str, path: str,
schema: Dict, schema: Dict) -> None:
form: Dict) -> None:
self.config = config self.config = config
self._path = path self._path = path
self.schema = schema self.schema = schema
self.form = form
def doc(self): def doc(self):
if self.issymlinkoption(): if self.issymlinkoption():
@ -122,8 +135,8 @@ class TiramisuOptionOption:
# return None # return None
def pattern(self): def pattern(self):
if self._path in self.form: if self._path in self.config.form:
return self.form[self._path].get('pattern') return self.config.form[self._path].get('pattern')
else: else:
return None return None
@ -221,14 +234,10 @@ class TiramisuOptionOwner:
def __init__(self, def __init__(self,
config: 'Config', config: 'Config',
schema: Dict, schema: Dict,
form: List[Dict],
temp: List[Dict],
path: str, path: str,
index: int) -> None: index: int) -> None:
self.config = config self.config = config
self.schema = schema self.schema = schema
self.form = form
self.temp = temp
self.path = path self.path = path
self.index = index self.index = index
@ -244,14 +253,10 @@ class TiramisuOptionDescriptionValue(_Value):
def __init__(self, def __init__(self,
config: 'Config', config: 'Config',
schema: Dict, schema: Dict,
form: List[Dict],
temp: List[Dict],
path: str, path: str,
index: int) -> None: index: int) -> None:
self.config = config self.config = config
self.schema = schema self.schema = schema
self.form = form
self.temp = temp
self.path = path self.path = path
self.index = index self.index = index
@ -276,14 +281,10 @@ class TiramisuOptionValue(_Value):
def __init__(self, def __init__(self,
config: 'Config', config: 'Config',
schema: Dict, schema: Dict,
form: List[Dict],
temp: List[Dict],
path: str, path: str,
index: int) -> None: index: int) -> None:
self.config = config self.config = config
self.schema = schema self.schema = schema
self.form = form
self.temp = temp
self.path = path self.path = path
self.index = index self.index = index
@ -323,7 +324,7 @@ class TiramisuOptionValue(_Value):
raise PropertiesOptionError(None, {'disabled'}, None, opt_type='option') raise PropertiesOptionError(None, {'disabled'}, None, opt_type='option')
if self.config.isleader(self.path): if self.config.isleader(self.path):
leader_old_value = self.config.get_value(self.path) leader_old_value = self.config.get_value(self.path)
remote = self.form.get(self.path, {}).get('remote', False) remote = self.config.form.get(self.path, {}).get('remote', False)
if self.index is None and self.schema.get('isMulti', False): if self.index is None and self.schema.get('isMulti', False):
if not isinstance(value, list): if not isinstance(value, list):
raise ValueError('value must be a list') raise ValueError('value must be a list')
@ -339,12 +340,12 @@ class TiramisuOptionValue(_Value):
self._validate(type_, val) self._validate(type_, val)
else: else:
self._validate(type_, value) self._validate(type_, value)
if self.path in self.temp: if self.path in self.config.temp:
obj = None obj = None
if self.index is None: if self.index is None:
obj = self.temp[self.path] obj = self.config.temp[self.path]
elif str(self.index) in self.temp[self.path]: elif str(self.index) in self.config.temp[self.path]:
obj = self.temp[self.path][str(self.index)] obj = self.config.temp[self.path][str(self.index)]
if obj is not None: if obj is not None:
for attr in ['error', 'warnings', 'invalid', 'hasWarnings']: for attr in ['error', 'warnings', 'invalid', 'hasWarnings']:
if attr in obj: if attr in obj:
@ -449,20 +450,14 @@ class _Option:
if type in ['all', 'optiondescription']: if type in ['all', 'optiondescription']:
yield TiramisuOptionDescription(self.config, yield TiramisuOptionDescription(self.config,
subschema, subschema,
self.form,
self.temp,
path) path)
if recursive: if recursive:
yield from TiramisuOptionDescription(self.config, yield from TiramisuOptionDescription(self.config,
subschema, subschema,
self.form,
self.temp,
path).list(type, recursive) path).list(type, recursive)
elif type in ['all', 'option']: elif type in ['all', 'option']:
yield TiramisuOption(self.config, yield TiramisuOption(self.config,
subschema, subschema,
self.form,
self.temp,
path, path,
self.index) self.index)
elif self.schema.get('type') == 'array' and idx_path == 0: elif self.schema.get('type') == 'array' and idx_path == 0:
@ -476,13 +471,9 @@ class TiramisuOptionDescription(_Option):
def __init__(self, def __init__(self,
config: 'Config', config: 'Config',
schema: Dict, schema: Dict,
form: List[Dict],
temp: List[Dict],
path: str) -> None: path: str) -> None:
self.config = config self.config = config
self.schema = schema self.schema = schema
self.form = form
self.temp = temp
self.path = path self.path = path
self.index = None self.index = None
@ -491,8 +482,7 @@ class TiramisuOptionDescription(_Option):
if subfunc == 'option': if subfunc == 'option':
return TiramisuOptionOption(self.config, return TiramisuOptionOption(self.config,
self.path, self.path,
self.schema, self.schema)
self.form)
if subfunc == 'property': if subfunc == 'property':
return TiramisuOptionProperty(self.config, return TiramisuOptionProperty(self.config,
self.path, self.path,
@ -500,8 +490,6 @@ class TiramisuOptionDescription(_Option):
if subfunc == 'value': if subfunc == 'value':
return TiramisuOptionDescriptionValue(self.config, return TiramisuOptionDescriptionValue(self.config,
self.schema, self.schema,
self.form,
self.temp,
self.path, self.path,
self.index) self.index)
raise APIError(_('please specify a valid sub function ({})').format(subfunc)) raise APIError(_('please specify a valid sub function ({})').format(subfunc))
@ -527,14 +515,10 @@ class TiramisuOption:
def __init__(self, def __init__(self,
config: 'Config', config: 'Config',
schema: Dict, schema: Dict,
form: List[Dict],
temp: List[Dict],
path: str, path: str,
index: Optional[int]) -> None: index: Optional[int]) -> None:
self.config = config self.config = config
self.schema = schema self.schema = schema
self.form = form
self.temp = temp
self.path = path self.path = path
self.index = index self.index = index
@ -545,8 +529,7 @@ class TiramisuOption:
raise NotImplementedError() raise NotImplementedError()
return TiramisuOptionOption(self.config, return TiramisuOptionOption(self.config,
self.path, self.path,
self.schema, self.schema)
self.form)
if subfunc == 'value': if subfunc == 'value':
if self.config.isleader(self.path): if self.config.isleader(self.path):
obj = TiramisuLeadershipValue obj = TiramisuLeadershipValue
@ -554,15 +537,11 @@ class TiramisuOption:
obj = TiramisuOptionValue obj = TiramisuOptionValue
return obj(self.config, return obj(self.config,
self.schema, self.schema,
self.form,
self.temp,
self.path, self.path,
self.index) self.index)
if subfunc == 'owner': if subfunc == 'owner':
return TiramisuOptionOwner(self.config, return TiramisuOptionOwner(self.config,
self.schema, self.schema,
self.form,
self.temp,
self.path, self.path,
self.index) self.index)
if subfunc == 'property': if subfunc == 'property':
@ -586,13 +565,9 @@ class ContextOption(_Option):
# config.option # config.option
def __init__(self, def __init__(self,
config: 'Config', config: 'Config',
form: Dict, schema: Dict) -> None:
schema: Dict,
temp: Dict) -> None:
self.config = config self.config = config
self.form = form
self.schema = {'properties': schema} self.schema = {'properties': schema}
self.temp = temp
self.index = None self.index = None
def __call__(self, def __call__(self,
@ -602,13 +577,9 @@ class ContextOption(_Option):
if schema['type'] in ['object', 'array']: if schema['type'] in ['object', 'array']:
return TiramisuOptionDescription(self.config, return TiramisuOptionDescription(self.config,
schema, schema,
self.form,
self.temp,
path) path)
return TiramisuOption(self.config, return TiramisuOption(self.config,
schema, schema,
self.form,
self.temp,
path, path,
index) index)
@ -617,13 +588,9 @@ class ContextOwner:
# config.owner # config.owner
def __init__(self, def __init__(self,
config: 'Config', config: 'Config',
form: Dict, schema: Dict) -> None:
schema: Dict,
temp: Dict) -> None:
self.config = config self.config = config
self.form = form
self.schema = {'properties': schema} self.schema = {'properties': schema}
self.temp = temp
self.index = None self.index = None
def get(self): def get(self):
@ -634,21 +601,15 @@ class ContextValue(_Value):
# config.value # config.value
def __init__(self, def __init__(self,
config: 'Config', config: 'Config',
form: Dict, schema: Dict) -> None:
schema: Dict,
temp: Dict) -> None:
self.config = config self.config = config
self.form = form
first = next(iter(schema.keys())) first = next(iter(schema.keys()))
self.path = first.rsplit('.', 1)[0] self.path = first.rsplit('.', 1)[0]
self.schema = {'properties': schema} self.schema = {'properties': schema}
self.temp = temp
def __call__(self) -> TiramisuOptionValue: def __call__(self) -> TiramisuOptionValue:
return TiramisuOptionValue(self.config, return TiramisuOptionValue(self.config,
self.schema, self.schema,
self.form,
self.temp,
path, path,
index) index)
@ -708,19 +669,13 @@ class Config:
return TiramisuContextProperty(self) return TiramisuContextProperty(self)
if subfunc == 'option': if subfunc == 'option':
return ContextOption(self, return ContextOption(self,
self.form, self.schema)
self.schema,
self.temp)
if subfunc == 'value': if subfunc == 'value':
return ContextValue(self, return ContextValue(self,
self.form, self.schema)
self.schema,
self.temp)
if subfunc == 'owner': if subfunc == 'owner':
return ContextOwner(self, return ContextOwner(self,
self.form, self.schema)
self.schema,
self.temp)
raise APIError(_('please specify a valid sub function ({})').format(subfunc)) raise APIError(_('please specify a valid sub function ({})').format(subfunc))
def add_value(self, def add_value(self,