pylint variable.py
This commit is contained in:
parent
d5ed39c1f3
commit
85ab952882
|
@ -67,8 +67,8 @@ class GroupAnnotator:
|
||||||
leader_name,
|
leader_name,
|
||||||
variable,
|
variable,
|
||||||
group,
|
group,
|
||||||
leader_fullname,
|
|
||||||
)
|
)
|
||||||
|
leader_space.path = leader_fullname
|
||||||
has_a_leader = True
|
has_a_leader = True
|
||||||
else:
|
else:
|
||||||
xmlfiles = self.objectspace.display_xmlfiles(variable.xmlfiles)
|
xmlfiles = self.objectspace.display_xmlfiles(variable.xmlfiles)
|
||||||
|
@ -85,7 +85,6 @@ class GroupAnnotator:
|
||||||
leader_name: str,
|
leader_name: str,
|
||||||
variable: 'Variable',
|
variable: 'Variable',
|
||||||
group: 'Group',
|
group: 'Group',
|
||||||
leader_fullname: str,
|
|
||||||
) -> None:
|
) -> None:
|
||||||
"""manage leader's variable
|
"""manage leader's variable
|
||||||
"""
|
"""
|
||||||
|
@ -121,9 +120,8 @@ class GroupAnnotator:
|
||||||
self.objectspace.paths.set_leader(namespace,
|
self.objectspace.paths.set_leader(namespace,
|
||||||
leader_family_name,
|
leader_family_name,
|
||||||
leader_name,
|
leader_name,
|
||||||
leader_name,
|
leadership_name,
|
||||||
)
|
)
|
||||||
leader_space.path = leader_fullname
|
|
||||||
return leader_is_hidden
|
return leader_is_hidden
|
||||||
|
|
||||||
def manage_follower(self,
|
def manage_follower(self,
|
||||||
|
|
|
@ -1,3 +1,5 @@
|
||||||
|
"""Annotate variable
|
||||||
|
"""
|
||||||
from ..i18n import _
|
from ..i18n import _
|
||||||
from ..utils import normalize_family
|
from ..utils import normalize_family
|
||||||
from ..error import DictConsistencyError
|
from ..error import DictConsistencyError
|
||||||
|
@ -15,14 +17,19 @@ CONVERT_OPTION = {'number': dict(opttype="IntOption", func=int),
|
||||||
'date': dict(opttype="DateOption"),
|
'date': dict(opttype="DateOption"),
|
||||||
'unix_user': dict(opttype="UsernameOption"),
|
'unix_user': dict(opttype="UsernameOption"),
|
||||||
'ip': dict(opttype="IPOption", initkwargs={'allow_reserved': True}),
|
'ip': dict(opttype="IPOption", initkwargs={'allow_reserved': True}),
|
||||||
'local_ip': dict(opttype="IPOption", initkwargs={'private_only': True, 'warnings_only': True}),
|
'local_ip': dict(opttype="IPOption", initkwargs={'private_only': True,
|
||||||
|
'warnings_only': True}),
|
||||||
'netmask': dict(opttype="NetmaskOption"),
|
'netmask': dict(opttype="NetmaskOption"),
|
||||||
'network': dict(opttype="NetworkOption"),
|
'network': dict(opttype="NetworkOption"),
|
||||||
'broadcast': dict(opttype="BroadcastOption"),
|
'broadcast': dict(opttype="BroadcastOption"),
|
||||||
'netbios': dict(opttype="DomainnameOption", initkwargs={'type': 'netbios', 'warnings_only': True}),
|
'netbios': dict(opttype="DomainnameOption", initkwargs={'type': 'netbios',
|
||||||
'domain': dict(opttype="DomainnameOption", initkwargs={'type': 'domainname', 'allow_ip': False}),
|
'warnings_only': True}),
|
||||||
'hostname': dict(opttype="DomainnameOption", initkwargs={'type': 'hostname', 'allow_ip': False}),
|
'domain': dict(opttype="DomainnameOption", initkwargs={'type': 'domainname',
|
||||||
'web_address': dict(opttype="URLOption", initkwargs={'allow_ip': True, 'allow_without_dot': True}),
|
'allow_ip': False}),
|
||||||
|
'hostname': dict(opttype="DomainnameOption", initkwargs={'type': 'hostname',
|
||||||
|
'allow_ip': False}),
|
||||||
|
'web_address': dict(opttype="URLOption", initkwargs={'allow_ip': True,
|
||||||
|
'allow_without_dot': True}),
|
||||||
'port': dict(opttype="PortOption", initkwargs={'allow_private': True}),
|
'port': dict(opttype="PortOption", initkwargs={'allow_private': True}),
|
||||||
'mac': dict(opttype="MACOption"),
|
'mac': dict(opttype="MACOption"),
|
||||||
'cidr': dict(opttype="IPOption", initkwargs={'cidr': True}),
|
'cidr': dict(opttype="IPOption", initkwargs={'cidr': True}),
|
||||||
|
@ -41,102 +48,106 @@ RENAME_ATTIBUTES = {'description': 'doc'}
|
||||||
|
|
||||||
|
|
||||||
class VariableAnnotator:
|
class VariableAnnotator:
|
||||||
|
"""Annotate variable
|
||||||
|
"""
|
||||||
def __init__(self,
|
def __init__(self,
|
||||||
objectspace,
|
objectspace,
|
||||||
):
|
):
|
||||||
self.objectspace = objectspace
|
self.objectspace = objectspace
|
||||||
self.convert_variable()
|
if hasattr(self.objectspace.space, 'variables'):
|
||||||
self.convert_separators()
|
self.convert_variable()
|
||||||
|
self.convert_separators()
|
||||||
|
|
||||||
|
def _convert_variable(self,
|
||||||
|
namespace: str,
|
||||||
|
variable,
|
||||||
|
variable_type: str,
|
||||||
|
) -> None:
|
||||||
|
if not hasattr(variable, 'type'):
|
||||||
|
variable.type = 'string'
|
||||||
|
if variable.type != 'symlink' and not hasattr(variable, 'description'):
|
||||||
|
variable.description = variable.name
|
||||||
|
if hasattr(variable, 'value'):
|
||||||
|
for value in variable.value:
|
||||||
|
if not hasattr(value, 'type'):
|
||||||
|
value.type = variable.type
|
||||||
|
value.name = CONVERT_OPTION.get(value.type, {}).get('func', str)(value.name)
|
||||||
|
for key, value in RENAME_ATTIBUTES.items():
|
||||||
|
setattr(variable, value, getattr(variable, key))
|
||||||
|
setattr(variable, key, None)
|
||||||
|
if variable_type == 'follower':
|
||||||
|
if variable.multi is True:
|
||||||
|
variable.multi = 'submulti'
|
||||||
|
else:
|
||||||
|
variable.multi = True
|
||||||
|
self._convert_valid_enum(namespace,
|
||||||
|
variable,
|
||||||
|
)
|
||||||
|
self._valid_type(variable)
|
||||||
|
|
||||||
|
def _convert_valid_enum(self,
|
||||||
|
namespace,
|
||||||
|
variable,
|
||||||
|
):
|
||||||
|
"""some types are, in fact, choices
|
||||||
|
convert this kind of variables into choice
|
||||||
|
"""
|
||||||
|
if variable.type in FORCE_CHOICE:
|
||||||
|
if not hasattr(self.objectspace.space, 'constraints'):
|
||||||
|
xmlfiles = variable.xmlfiles
|
||||||
|
self.objectspace.space.constraints = self.objectspace.constraints(xmlfiles)
|
||||||
|
self.objectspace.space.constraints.namespace = namespace
|
||||||
|
if not hasattr(self.objectspace.space.constraints, 'check'):
|
||||||
|
self.objectspace.space.constraints.check = []
|
||||||
|
check = self.objectspace.check(variable.xmlfiles)
|
||||||
|
check.name = 'valid_enum'
|
||||||
|
check.target = variable.path
|
||||||
|
check.namespace = namespace
|
||||||
|
check.param = []
|
||||||
|
for value in FORCE_CHOICE[variable.type]:
|
||||||
|
param = self.objectspace.param(variable.xmlfiles)
|
||||||
|
param.text = value
|
||||||
|
check.param.append(param)
|
||||||
|
self.objectspace.space.constraints.check.append(check)
|
||||||
|
variable.type = 'string'
|
||||||
|
|
||||||
|
def _valid_type(self,
|
||||||
|
variable
|
||||||
|
) -> None:
|
||||||
|
if variable.type not in CONVERT_OPTION:
|
||||||
|
xmlf = self.objectspace.display_xmlfiles(variable.xmlfiles)
|
||||||
|
msg = _(f'unvalid type "{variable.type}" for variable "{variable.name}" in {xmlf}')
|
||||||
|
raise DictConsistencyError(msg, 36)
|
||||||
|
|
||||||
def convert_variable(self):
|
def convert_variable(self):
|
||||||
def _convert_variable(variable,
|
"""convert variable
|
||||||
variable_type,
|
"""
|
||||||
):
|
|
||||||
if not hasattr(variable, 'type'):
|
|
||||||
variable.type = 'string'
|
|
||||||
if variable.type != 'symlink' and not hasattr(variable, 'description'):
|
|
||||||
variable.description = variable.name
|
|
||||||
if hasattr(variable, 'value'):
|
|
||||||
for value in variable.value:
|
|
||||||
if not hasattr(value, 'type'):
|
|
||||||
value.type = variable.type
|
|
||||||
value.name = CONVERT_OPTION.get(value.type, {}).get('func', str)(value.name)
|
|
||||||
for key, value in RENAME_ATTIBUTES.items():
|
|
||||||
setattr(variable, value, getattr(variable, key))
|
|
||||||
setattr(variable, key, None)
|
|
||||||
if variable_type == 'follower':
|
|
||||||
if variable.multi is True:
|
|
||||||
variable.multi = 'submulti'
|
|
||||||
else:
|
|
||||||
variable.multi = True
|
|
||||||
|
|
||||||
def _convert_valid_enum(namespace,
|
|
||||||
variable,
|
|
||||||
path,
|
|
||||||
):
|
|
||||||
if variable.type in FORCE_CHOICE:
|
|
||||||
check = self.objectspace.check(variable.xmlfiles)
|
|
||||||
check.name = 'valid_enum'
|
|
||||||
check.target = path
|
|
||||||
check.namespace = namespace
|
|
||||||
check.param = []
|
|
||||||
for value in FORCE_CHOICE[variable.type]:
|
|
||||||
param = self.objectspace.param(variable.xmlfiles)
|
|
||||||
param.text = value
|
|
||||||
check.param.append(param)
|
|
||||||
if not hasattr(self.objectspace.space, 'constraints'):
|
|
||||||
self.objectspace.space.constraints = self.objectspace.constraints(variable.xmlfiles)
|
|
||||||
self.objectspace.space.constraints.namespace = namespace
|
|
||||||
if not hasattr(self.objectspace.space.constraints, 'check'):
|
|
||||||
self.objectspace.space.constraints.check = []
|
|
||||||
self.objectspace.space.constraints.check.append(check)
|
|
||||||
variable.type = 'string'
|
|
||||||
|
|
||||||
def _valid_type(variable):
|
|
||||||
if variable.type not in CONVERT_OPTION:
|
|
||||||
xmlfiles = self.objectspace.display_xmlfiles(variable.xmlfiles)
|
|
||||||
raise DictConsistencyError(_(f'unvalid type "{variable.type}" for variable "{variable.name}" in {xmlfiles}'), 36)
|
|
||||||
|
|
||||||
if not hasattr(self.objectspace.space, 'variables'):
|
|
||||||
return
|
|
||||||
for families in self.objectspace.space.variables.values():
|
for families in self.objectspace.space.variables.values():
|
||||||
namespace = families.name
|
if not hasattr(families, 'family'):
|
||||||
if hasattr(families, 'family'):
|
continue
|
||||||
families.doc = families.name
|
families.doc = families.name
|
||||||
for family in families.family.values():
|
for family in families.family.values():
|
||||||
family.doc = family.name
|
family.doc = family.name
|
||||||
family.name = normalize_family(family.name)
|
family.name = normalize_family(family.name)
|
||||||
if hasattr(family, 'variable'):
|
if not hasattr(family, 'variable'):
|
||||||
for variable in family.variable.values():
|
continue
|
||||||
if isinstance(variable, self.objectspace.leadership):
|
for variable in family.variable.values():
|
||||||
for idx, follower in enumerate(variable.variable):
|
if isinstance(variable, self.objectspace.leadership):
|
||||||
if idx == 0:
|
# first variable is a leader, others are follower
|
||||||
variable_type = 'master'
|
variable_type = 'leader'
|
||||||
else:
|
for follower in variable.variable:
|
||||||
variable_type = 'follower'
|
self._convert_variable(families.name,
|
||||||
path = '{}.{}.{}.{}'.format(namespace, normalize_family(family.name), variable.name, follower.name)
|
follower,
|
||||||
_convert_variable(follower,
|
variable_type,
|
||||||
variable_type,
|
)
|
||||||
)
|
variable_type = 'follower'
|
||||||
_convert_valid_enum(namespace,
|
else:
|
||||||
follower,
|
self._convert_variable(families.name,
|
||||||
path,
|
variable,
|
||||||
)
|
'variable',
|
||||||
_valid_type(follower)
|
)
|
||||||
else:
|
|
||||||
path = '{}.{}.{}'.format(namespace, normalize_family(family.name), variable.name)
|
|
||||||
_convert_variable(variable,
|
|
||||||
'variable',
|
|
||||||
)
|
|
||||||
_convert_valid_enum(namespace,
|
|
||||||
variable,
|
|
||||||
path,
|
|
||||||
)
|
|
||||||
_valid_type(variable)
|
|
||||||
|
|
||||||
def convert_separators(self): # pylint: disable=C0111,R0201
|
def convert_separators(self): # pylint: disable=C0111,R0201
|
||||||
if not hasattr(self.objectspace.space, 'variables'):
|
|
||||||
return
|
|
||||||
for family in self.objectspace.space.variables.values():
|
for family in self.objectspace.space.variables.values():
|
||||||
if not hasattr(family, 'separators'):
|
if not hasattr(family, 'separators'):
|
||||||
continue
|
continue
|
||||||
|
@ -144,10 +155,8 @@ class VariableAnnotator:
|
||||||
for separator in family.separators.separator:
|
for separator in family.separators.separator:
|
||||||
option = self.objectspace.paths.get_variable_obj(separator.name)
|
option = self.objectspace.paths.get_variable_obj(separator.name)
|
||||||
if hasattr(option, 'separator'):
|
if hasattr(option, 'separator'):
|
||||||
subpath = self.objectspace.paths.get_variable_path(separator.name,
|
|
||||||
separator.namespace,
|
|
||||||
)
|
|
||||||
xmlfiles = self.objectspace.display_xmlfiles(separator.xmlfiles)
|
xmlfiles = self.objectspace.display_xmlfiles(separator.xmlfiles)
|
||||||
raise DictConsistencyError(_(f'{subpath} already has a separator in {xmlfiles}'), 35)
|
msg = _(f'{separator.name} already has a separator in {xmlfiles}')
|
||||||
|
raise DictConsistencyError(msg, 35)
|
||||||
option.separator = separator.text
|
option.separator = separator.text
|
||||||
del family.separators
|
del family.separators
|
||||||
|
|
|
@ -79,6 +79,7 @@ class Path:
|
||||||
new_path = namespace + '.' + leader_family_name + '.' + leader_name + '.' + name
|
new_path = namespace + '.' + leader_family_name + '.' + leader_name + '.' + name
|
||||||
self.variables[new_path] = self.variables.pop(old_path)
|
self.variables[new_path] = self.variables.pop(old_path)
|
||||||
self.variables[new_path]['leader'] = leader_name
|
self.variables[new_path]['leader'] = leader_name
|
||||||
|
self.variables[new_path]['variableobj'].path = new_path
|
||||||
if namespace == Config['variable_namespace']:
|
if namespace == Config['variable_namespace']:
|
||||||
self.full_paths_variables[name] = new_path
|
self.full_paths_variables[name] = new_path
|
||||||
|
|
||||||
|
@ -94,11 +95,13 @@ class Path:
|
||||||
variableobj,
|
variableobj,
|
||||||
) -> str: # pylint: disable=C0111
|
) -> str: # pylint: disable=C0111
|
||||||
if '.' not in name:
|
if '.' not in name:
|
||||||
full_name = '.'.join([namespace, family, name])
|
full_path = '.'.join([namespace, family, name])
|
||||||
self.full_paths_variables[name] = full_name
|
if namespace == Config['variable_namespace']:
|
||||||
|
self.full_paths_variables[name] = full_path
|
||||||
else:
|
else:
|
||||||
full_name = name
|
full_path = name
|
||||||
self.variables[full_name] = dict(name=name,
|
variableobj.path = full_path
|
||||||
|
self.variables[full_path] = dict(name=name,
|
||||||
family=family,
|
family=family,
|
||||||
namespace=namespace,
|
namespace=namespace,
|
||||||
leader=None,
|
leader=None,
|
||||||
|
@ -107,12 +110,12 @@ class Path:
|
||||||
)
|
)
|
||||||
|
|
||||||
def get_variable_name(self,
|
def get_variable_name(self,
|
||||||
name,
|
name: str,
|
||||||
): # pylint: disable=C0111
|
): # pylint: disable=C0111
|
||||||
return self._get_variable(name)['name']
|
return self._get_variable(name)['name']
|
||||||
|
|
||||||
def get_variable_obj(self,
|
def get_variable_obj(self,
|
||||||
name:str,
|
name: str,
|
||||||
) -> 'Variable': # pylint: disable=C0111
|
) -> 'Variable': # pylint: disable=C0111
|
||||||
return self._get_variable(name)['variableobj']
|
return self._get_variable(name)['variableobj']
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue