2021-01-17 18:00:29 +01:00
|
|
|
"""Fill annotator
|
|
|
|
"""
|
|
|
|
from importlib.machinery import SourceFileLoader
|
|
|
|
|
|
|
|
from ..i18n import _
|
|
|
|
|
|
|
|
from ..error import DictConsistencyError
|
|
|
|
|
|
|
|
|
|
|
|
class FillAnnotator:
|
|
|
|
"""Fill annotator
|
|
|
|
"""
|
|
|
|
def __init__(self,
|
|
|
|
objectspace,
|
|
|
|
eosfunc_file,
|
|
|
|
):
|
|
|
|
self.objectspace = objectspace
|
|
|
|
if not hasattr(objectspace.space, 'constraints') or \
|
|
|
|
not hasattr(self.objectspace.space.constraints, 'fill'):
|
|
|
|
return
|
|
|
|
eosfunc = SourceFileLoader('eosfunc', eosfunc_file).load_module()
|
|
|
|
self.functions = dir(eosfunc)
|
|
|
|
self.convert_fill()
|
|
|
|
|
|
|
|
def convert_fill(self) -> None:
|
|
|
|
"""valid and manage <fill>
|
|
|
|
"""
|
|
|
|
targets = []
|
|
|
|
for fill in self.objectspace.space.constraints.fill:
|
|
|
|
# test if it's redefined calculation
|
|
|
|
if fill.target in targets:
|
|
|
|
xmlfiles = self.objectspace.display_xmlfiles(fill.xmlfiles)
|
|
|
|
msg = _(f'A fill already exists for the target of "{fill.target}" created '
|
|
|
|
f'in {xmlfiles}')
|
|
|
|
raise DictConsistencyError(msg, 24)
|
|
|
|
targets.append(fill.target)
|
|
|
|
|
|
|
|
# test if the function exists
|
|
|
|
if fill.name not in self.functions:
|
|
|
|
xmlfiles = self.objectspace.display_xmlfiles(fill.xmlfiles)
|
|
|
|
msg = _(f'cannot find fill function "{fill.name}" in {xmlfiles}')
|
|
|
|
raise DictConsistencyError(msg, 25)
|
|
|
|
|
|
|
|
# let's replace the target by the path
|
|
|
|
fill.target, suffix = self.objectspace.paths.get_variable_path(fill.target,
|
|
|
|
fill.namespace,
|
|
|
|
)
|
|
|
|
if suffix is not None:
|
|
|
|
xmlfiles = self.objectspace.display_xmlfiles(fill.xmlfiles)
|
|
|
|
msg = _(f'Cannot add fill function to "{fill.target}" only '
|
|
|
|
f'for the suffix "{suffix}" in {xmlfiles}')
|
|
|
|
raise DictConsistencyError(msg, 26)
|
|
|
|
|
|
|
|
# get the target variable
|
2021-01-17 18:04:49 +01:00
|
|
|
variable = self.objectspace.paths.get_variable(fill.target)
|
2021-01-17 18:00:29 +01:00
|
|
|
|
|
|
|
# create an object value
|
|
|
|
value = self.objectspace.value(fill.xmlfiles)
|
|
|
|
value.type = 'calculation'
|
|
|
|
value.name = fill.name
|
|
|
|
variable.value = [value]
|
|
|
|
|
|
|
|
# manage params
|
|
|
|
if not hasattr(fill, 'param'):
|
|
|
|
continue
|
|
|
|
self.convert_fill_param(fill)
|
|
|
|
if fill.param:
|
|
|
|
value.param = fill.param
|
|
|
|
|
|
|
|
def convert_fill_param(self,
|
|
|
|
fill: "self.objectspace.fill",
|
|
|
|
) -> None:
|
|
|
|
""" valid and convert fill's param
|
|
|
|
"""
|
|
|
|
param_to_delete = []
|
|
|
|
for param_idx, param in enumerate(fill.param):
|
|
|
|
if param.type == 'string' and not hasattr(param, 'text'):
|
|
|
|
param.text = None
|
|
|
|
if param.type == 'suffix':
|
|
|
|
if hasattr(param, 'text'):
|
|
|
|
xmlfiles = self.objectspace.display_xmlfiles(fill.xmlfiles)
|
|
|
|
msg = _(f'"{param.type}" variables must not have a value in order '
|
|
|
|
f'to calculate "{fill.target}" in {xmlfiles}')
|
|
|
|
raise DictConsistencyError(msg, 28)
|
|
|
|
if not self.objectspace.paths.variable_is_dynamic(fill.target):
|
|
|
|
xmlfiles = self.objectspace.display_xmlfiles(fill.xmlfiles)
|
|
|
|
msg = _('Cannot set suffix target to the none dynamic variable '
|
|
|
|
f'"{fill.target}" in {xmlfiles}')
|
|
|
|
raise DictConsistencyError(msg, 53)
|
|
|
|
elif not hasattr(param, 'text'):
|
|
|
|
xmlfiles = self.objectspace.display_xmlfiles(fill.xmlfiles)
|
|
|
|
msg = _(f'All "{param.type}" variables must have a value in order '
|
|
|
|
f'to calculate "{fill.target}" in {xmlfiles}')
|
|
|
|
raise DictConsistencyError(msg, 27)
|
|
|
|
if param.type == 'variable':
|
|
|
|
try:
|
|
|
|
path, suffix = self.objectspace.paths.get_variable_path(param.text,
|
|
|
|
fill.namespace,
|
|
|
|
)
|
2021-01-17 18:04:49 +01:00
|
|
|
param.text = self.objectspace.paths.get_variable(path)
|
2021-01-17 18:00:29 +01:00
|
|
|
if suffix:
|
|
|
|
param.suffix = suffix
|
2021-01-18 17:46:21 +01:00
|
|
|
family_path = self.objectspace.paths.get_variable_family_path(path)
|
|
|
|
param.family = self.objectspace.paths.get_family(family_path,
|
|
|
|
param.text.namespace,
|
|
|
|
)
|
2021-01-17 18:00:29 +01:00
|
|
|
except DictConsistencyError as err:
|
|
|
|
if err.errno != 42 or not param.optional:
|
|
|
|
raise err
|
|
|
|
param_to_delete.append(param_idx)
|
|
|
|
param_to_delete.sort(reverse=True)
|
|
|
|
for param_idx in param_to_delete:
|
|
|
|
fill.param.pop(param_idx)
|