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