update to tiramisu-rc2 + leader/followers support

This commit is contained in:
2019-03-05 08:26:58 +01:00
parent 13dc94a687
commit ba548c8c80
614 changed files with 746 additions and 484 deletions

View File

@ -65,9 +65,21 @@ class TiramisuOptionOption:
def isoptiondescription(self):
return self.schema['type'] in ['object', 'array']
def ismasterslaves(self):
def isleadership(self):
return self.schema['type'] == 'array'
def isleader(self):
if '.' in self._path:
parent_schema = self.config.get_schema(self._path.rsplit('.', 1)[0])
leader = next(iter(parent_schema['properties'].keys()))
if parent_schema['type'] == 'array' and \
leader == self._path:
return True
return False
def isfollower(self):
return self.config.isfollower(self._path)
def issymlinkoption(self) -> bool:
return self.schema['type'] == 'symlink'
@ -75,27 +87,15 @@ class TiramisuOptionOption:
return self.schema.get('isMulti', False)
def type(self) -> str:
if self.ismasterslaves():
return 'masterslaves'
if self.isleadership():
return 'leadership'
if self.isoptiondescription():
return 'optiondescription'
types = {'number': 'int',
'choice': 'choice',
'boolean': 'bool',
'password': 'password',
'date': 'date',
'domain': 'domainname',
'url': 'url',
'username': 'username',
'string': 'str',
'symlink': 'symlink'}
if self.schema['type'] in types:
return types[self.schema['type']]
raise Exception('unsupported type {}'.format(self.schema['type']))
return self.schema['type']
def properties(self) -> List[str]:
model = self.model.get(self._path, {})
return self.config.get_properties(self.model, self._path)
return self.config.get_properties(self.model, self._path, None)
def requires(self) -> None:
# FIXME
@ -106,17 +106,20 @@ class TiramisuOptionProperty:
def __init__(self,
config: 'Config',
path: str,
index: Optional[int],
model: Dict) -> None:
self.config = config
self.path = path
self.index = index
self.model = model
def get(self, only_raises=False):
if not only_raises:
props = self.config.get_properties(self.model, self.path, only_raises)
props = self.config.get_properties(self.model, self.path, self.index, only_raises)
else:
props = []
if self.config.get_hidden(self.path):
if self.config.get_hidden(self.path,
self.index):
props.append('hidden')
return props
@ -127,9 +130,8 @@ class _Value:
schema: Dict,
root: str,
fullpath: bool,
withwarning: bool,
parent_key: str=None) -> None:
len_master = None
withwarning: bool) -> None:
leadership_len = None
for key, option in schema['properties'].items():
hidden = self.temp.get(key, {}).get('hidden', None)
model_display = not self.model.get(key, {}).get('hidden', False) and \
@ -137,19 +139,29 @@ class _Value:
if hidden is False or (hidden is None and model_display is True):
if option['type'] in ['object', 'array']:
# optiondescription or leadership
self._dict_walk(ret,
option,
root,
fullpath,
withwarning,
key)
withwarning)
elif schema.get('type') == 'array' and leadership_len is not None:
# followers
values = []
for index in range(leadership_len):
value = self.config.get_value(key,
index)
self._display_warnings(key, value, option['type'], option['name'], withwarning)
values.append(value)
ret[key] = values
else:
value = self.config.get_value(key, len_master)
if parent_key is not None and schema.get('type') == 'array' and len_master is None:
len_master = len(value)
value = self.config.get_value(key)
self._display_warnings(key, value, option['type'], option['name'], withwarning)
ret[key] = value
elif parent_key is not None and schema.get('type') == 'array' and len_master is None:
if schema.get('type') == 'array':
leadership_len = len(value)
elif schema.get('type') == 'array' and leadership_len is None:
# if leader is hidden, followers are hidden too
break
def dict(self,
@ -201,10 +213,10 @@ class TiramisuOptionOwner:
self.index = index
def isdefault(self) -> Any:
return self.config.get_owner(self.path) == 'default'
return self.config.get_owner(self.path, self.index) == 'default'
def get(self) -> str:
return self.config.get_owner(self.path)
return self.config.get_owner(self.path, self.index)
class TiramisuOptionValue(_Value):
@ -226,6 +238,14 @@ class TiramisuOptionValue(_Value):
self.index = index
def get(self) -> Any:
if self.config.isfollower(self.path):
if self.index is None:
raise APIError(_('index must be set with the follower option "{}"').format(self.path))
value = self.config.get_value(self.path, self.index)
self._display_warnings(self.path, value, self.schema['type'], self.schema['name'])
return value
if self.index is not None:
raise APIError(_('index must be only with a follower option, not for "{}"').format(self.path))
value = self.config.get_value(self.path)
self._display_warnings(self.path, value, self.schema['type'], self.schema['name'])
return value
@ -387,10 +407,9 @@ class TiramisuOption:
self.path,
self.index)
if subfunc == 'property':
if self.index != None:
raise NotImplementedError()
return TiramisuOptionProperty(self.config,
self.path,
self.index,
self.model.get(self.path, {}))
raise APIError(_('please specify a valid sub function ({})').format(subfunc))
@ -503,10 +522,14 @@ class Config:
if 'index' in option:
if key not in ret:
ret[key] = copy(option)
ret[key]['value'] = {ret[key]['index']: (ret[key]['value'], ret[key]['owner'])}
ret[key]['value'] = {}
del ret[key]['index']
del ret[key]['owner']
elif option.get('hidden') is not True:
if 'hidden' in ret[key]:
del ret[key]['hidden']
if option.get('hidden') is True:
ret[key]['value'][option['index']] = ()
else:
ret[key]['value'][option['index']] = (option['value'], option['owner'])
else:
ret[key] = option
@ -582,6 +605,7 @@ class Config:
def get_properties(self,
model,
path,
index,
only_raises=True):
props = model.get('properties', [])[:]
if model.get('required'):
@ -593,7 +617,8 @@ class Config:
props.append('mandatory')
if model.get('readOnly'):
props.append('frozen')
if only_raises and self.get_hidden(path):
if only_raises and self.get_hidden(path,
index):
props.append('hidden')
if self.form.get(path, {}).get('clearable'):
props.append('clearable')
@ -619,19 +644,34 @@ class Config:
schema = schema['properties'][root_path]
return schema
def get_hidden(self,
def isfollower(self,
path: str) -> bool:
if '.' in path:
parent_schema = self.get_schema(path.rsplit('.', 1)[0])
leader = next(iter(parent_schema['properties'].keys()))
if parent_schema['type'] == 'array' and \
leader != path:
return True
return False
def get_hidden(self,
path: str,
index: Optional[int]) -> bool:
property_ = 'hidden'
if property_ in self.temp.get(path, {}):
value = self.temp[path][property_]
else:
value = self.model.get(path, {}).get(property_)
if index is None:
value = self.model.get(path, {}).get(property_, False)
else:
value = self.model.get(path, {}).get(property_, False) or \
self.model.get(path, {}).get('value', {}).get(index) == ()
return value
def get_value(self,
path: str,
len_master: int=None) -> Any:
if len_master is None:
index: int=None) -> Any:
if index is None:
if 'value' in self.temp.get(path, {}):
value = self.temp[path]['value']
else:
@ -639,27 +679,39 @@ class Config:
if value is None and self.get_schema(path).get('isMulti', False):
value = []
else:
value = self.temp.get(path)
if value is None:
value = self.model.get(path)
if value is None:
value = [None] * len_master
else:
val = []
for index in range(len_master):
if index in value['value']:
val.append(value['value'][index][0])
model = self.model.get(path, {})
value = model.get('value')
if value is not None:
if index in value:
if len(value[index]):
value = value[index][0]
else:
val.append(None)
value = val
value = PropertiesOptionError(None, None, None, opt_type='option')
else:
value = None
else:
value = None
return value
def get_owner(self,
path: str) -> str:
if 'owner' in self.temp.get(path, {}):
owner = self.temp[path]['owner']
path: str,
index: int) -> str:
if not self.isfollower(path):
if 'owner' in self.temp.get(path, {}):
owner = self.temp[path]['owner']
else:
owner = self.model.get(path, {}).get('owner', 'default')
else:
owner = self.model.get(path, {}).get('owner', 'default')
if 'value' in self.temp.get(path, {}):
value = self.temp[path]['value']
else:
value = self.model.get(path, {}).get('value', {})
if index in value:
if not value[index]:
raise PropertiesOptionError(None, None, None, opt_type='option')
owner = value[index][1]
else:
owner = 'default'
return owner
def updates_value(self,
@ -668,7 +720,7 @@ class Config:
index: Optional[int],
value: Optional[Any],
remote: bool,
masterslaves: Optional[str]) -> None:
leadership: Optional[str]) -> None:
update_last_action = False
if self.updates:
last_body = self.updates[-1]
@ -680,8 +732,8 @@ class Config:
update_last_action = True
elif index == None and action == 'delete':
for update in reversed(self.updates):
if masterslaves is None and update['name'] == path or \
masterslaves and path.startswith(masterslaves + '.'):
if leadership is None and update['name'] == path or \
leadership and path.startswith(leadership + '.'):
del self.updates[-1]
else:
break
@ -852,7 +904,8 @@ class Config:
copy = self.form.get(path, {}).get('copy')
if copy:
for opt in copy:
owner = self.get_owner(opt)
# FIXME follower!
owner = self.get_owner(opt, None)
if owner == 'default':
# do not change in this.temp, it's default value
self.model[opt]['value'] = value