This commit is contained in:
Emmanuel Garette 2020-12-26 17:06:56 +01:00
parent 13b1e9bf54
commit a6bafd89bd
15 changed files with 78 additions and 104 deletions

View file

@ -1,4 +1,3 @@
from typing import List
from importlib.machinery import SourceFileLoader
from .variable import CONVERT_OPTION
@ -105,13 +104,13 @@ class ConstrainteAnnotator:
del self.objectspace.space.constraints.check[idx]
def check_replace_text(self):
for check_idx, check in enumerate(self.objectspace.space.constraints.check):
for check in self.objectspace.space.constraints.check:
namespace = check.namespace
if hasattr(check, 'param'):
for idx, param in enumerate(check.param):
for param in check.param:
if param.type == 'variable':
param.text = self.objectspace.paths.get_variable_path(param.text, namespace)
check.is_in_leadership = self.objectspace.paths.get_leader(check.target) != None
check.is_in_leadership = self.objectspace.paths.get_leader(check.target) is not None
# let's replace the target by the path
check.target = self.objectspace.paths.get_variable_path(check.target, namespace)
@ -169,9 +168,9 @@ class ConstrainteAnnotator:
raise DictConsistencyError(_(f'target "{target.type}" not allow in condition "{condition.name}" in {xmlfiles}'), 10)
def filter_targets(self): # pylint: disable=C0111
for condition_idx, condition in enumerate(self.objectspace.space.constraints.condition):
for condition in self.objectspace.space.constraints.condition:
namespace = condition.namespace
for idx, target in enumerate(condition.target):
for target in condition.target:
if target.type == 'variable':
if condition.source == target.name:
raise DictConsistencyError(_('target name and source name must be different: {}').format(condition.source), 11)
@ -191,7 +190,7 @@ class ConstrainteAnnotator:
def convert_xxxlist_to_variable(self): # pylint: disable=C0111
# transform *list to variable or family
for condition_idx, condition in enumerate(self.objectspace.space.constraints.condition):
for condition in self.objectspace.space.constraints.condition:
new_targets = []
remove_targets = []
for target_idx, target in enumerate(condition.target):
@ -240,13 +239,13 @@ class ConstrainteAnnotator:
remove_targets = []
# optional
for idx, target in enumerate(condition.target):
for index, target in enumerate(condition.target):
if target.optional is True and not self.objectspace.paths.path_is_defined(target.name):
remove_targets.append(idx)
remove_targets.append(index)
remove_targets = list(set(remove_targets))
remove_targets.sort(reverse=True)
for idx in remove_targets:
condition.target.pop(idx)
for index in remove_targets:
condition.target.pop(index)
remove_conditions = list(set(remove_conditions))
remove_conditions.sort(reverse=True)
for idx in remove_conditions:
@ -255,11 +254,11 @@ class ConstrainteAnnotator:
def _get_condition_actions(self, condition_name):
if condition_name.startswith('disabled_if_'):
return ['disabled']
elif condition_name.startswith('hidden_if_'):
if condition_name.startswith('hidden_if_'):
return ['hidden', 'frozen', 'force_default_on_freeze']
elif condition_name.startswith('mandatory_if_'):
if condition_name.startswith('mandatory_if_'):
return ['mandatory']
elif condition_name == 'auto_hidden_if_not_in':
if condition_name == 'auto_hidden_if_not_in':
return ['auto_frozen']
def check_choice_option_condition(self):
@ -275,7 +274,6 @@ class ConstrainteAnnotator:
if suffix:
xmlfiles = self.objectspace.display_xmlfiles(condition.xmlfiles)
raise DictConsistencyError(_(f'the source "{condition.source}" in condition cannot be a dynamic variable in {xmlfiles}'), 20)
src_variable = self.objectspace.paths.get_variable_obj(condition.source)
valid_enum = None
# FIXME only string?
if condition.source in self.valid_enums and self.valid_enums[condition.source]['type'] == 'string':

View file

@ -6,7 +6,7 @@ from ..error import DictConsistencyError
modes_level = ('basic', 'normal', 'expert')
class Mode(object):
class Mode:
def __init__(self, name, level):
self.name = name
self.level = level
@ -40,8 +40,8 @@ class FamilyAnnotator:
if hasattr(family, 'family'):
space = family.family
removed_families = []
for family_name, family in space.items():
if not hasattr(family, 'variable') or len(family.variable) == 0:
for family_name, sfamily in space.items():
if not hasattr(sfamily, 'variable') or len(sfamily.variable) == 0:
removed_families.append(family_name)
for family_name in removed_families:
del space[family_name]
@ -51,9 +51,9 @@ class FamilyAnnotator:
return
for family in self.objectspace.space.variables.values():
if hasattr(family, 'family'):
for family in family.family.values():
for vfamily in family.family.values():
mode = modes_level[-1]
for variable in family.variable.values():
for variable in vfamily.variable.values():
if isinstance(variable, self.objectspace.leadership):
variable_mode = variable.variable[0].mode
variable.variable[0].mode = None
@ -62,24 +62,28 @@ class FamilyAnnotator:
variable_mode = variable.mode
if variable_mode is not None and modes[mode] > modes[variable_mode]:
mode = variable_mode
family.mode = mode
vfamily.mode = mode
def dynamic_families(self): # pylint: disable=C0111
if not hasattr(self.objectspace.space, 'variables'):
return
for family in self.objectspace.space.variables.values():
if hasattr(family, 'family'):
for family in family.family.values():
if 'dynamic' in vars(family):
namespace = self.objectspace.paths.get_variable_namespace(family.dynamic)
varpath = self.objectspace.paths.get_variable_path(family.dynamic, namespace)
for vfamily in family.family.values():
if 'dynamic' in vars(vfamily):
namespace = self.objectspace.paths.get_variable_namespace(vfamily.dynamic)
varpath = self.objectspace.paths.get_variable_path(vfamily.dynamic, namespace)
obj = self.objectspace.paths.get_variable_obj(varpath)
if not obj.multi:
xmlfiles = self.objectspace.display_xmlfiles(family.xmlfiles)
raise DictConsistencyError(_(f'dynamic family "{family.name}" must be linked to multi variable in {xmlfiles}'), 16)
family.dynamic = varpath
xmlfiles = self.objectspace.display_xmlfiles(vfamily.xmlfiles)
raise DictConsistencyError(_(f'dynamic family "{vfamily.name}" must be linked to multi variable in {xmlfiles}'), 16)
vfamily.dynamic = varpath
def annotate_variable(self, variable, family_mode, path, is_follower=False):
def annotate_variable(self,
variable,
family_mode: str,
is_follower=False,
) -> None:
# if the variable is mandatory and doesn't have any value
# then the variable's mode is set to 'basic'
if not hasattr(variable, 'value') and variable.type == 'boolean':
@ -92,21 +96,16 @@ class FamilyAnnotator:
for value in variable.value:
if value.type == 'calculation':
has_value = False
has_variable = False
if hasattr(value, 'param'):
for param in value.param:
if param.type == 'variable':
has_variable = True
break
#if not has_variable:
# # if one parameter is a variable, let variable choice if it's mandatory
# variable.mandatory = True
if has_value:
# if has value but without any calculation
variable.mandatory = True
if variable.mandatory is True and (not hasattr(variable, 'value') or is_follower):
variable.mode = modes_level[0]
if variable.mode != None and modes[variable.mode] < modes[family_mode] and (not is_follower or variable.mode != modes_level[0]):
if variable.mode is not None and modes[variable.mode] < modes[family_mode] and (not is_follower or variable.mode != modes_level[0]):
variable.mode = family_mode
if variable.hidden is True:
variable.frozen = True
@ -117,7 +116,6 @@ class FamilyAnnotator:
if not hasattr(self.objectspace.space, 'variables'):
return
for variables in self.objectspace.space.variables.values():
namespace = variables.name
if hasattr(variables, 'family'):
for family in variables.family.values():
family_mode = family.mode
@ -125,7 +123,6 @@ class FamilyAnnotator:
for variable in family.variable.values():
if isinstance(variable, self.objectspace.leadership):
mode = modes_level[-1]
for idx, follower in enumerate(variable.variable):
if follower.auto_save is True:
xmlfiles = self.objectspace.display_xmlfiles(variable.xmlfiles)
@ -134,8 +131,10 @@ class FamilyAnnotator:
xmlfiles = self.objectspace.display_xmlfiles(variable.xmlfiles)
raise DictConsistencyError(_(f'leader/followers "{follower.name}" could not be auto_freeze in {xmlfiles}'), 30)
is_follower = idx != 0
path = '{}.{}.{}'.format(family.path, variable.name, follower.name)
self.annotate_variable(follower, family_mode, path, is_follower)
self.annotate_variable(follower,
family_mode,
is_follower,
)
# leader's mode is minimum level
if modes[variable.variable[0].mode] > modes[follower.mode]:
follower.mode = variable.variable[0].mode
@ -147,6 +146,6 @@ class FamilyAnnotator:
# auto_freeze's variable is set in 'basic' mode if its mode is 'normal'
if variable.auto_freeze is True and variable.mode != modes_level[-1]:
variable.mode = modes_level[0]
path = '{}.{}'.format(family.path, variable.name)
self.annotate_variable(variable, family_mode, path)
self.annotate_variable(variable,
family_mode,
)

View file

@ -136,4 +136,3 @@ class GroupAnnotator:
variable.name,
leader_name,
)

View file

@ -18,7 +18,7 @@ class PropertyAnnotator:
properties = []
for prop in PROPERTIES:
if hasattr(variable, prop):
if getattr(variable, prop) == True:
if getattr(variable, prop) is True:
for subprop in CONVERT_PROPERTIES.get(prop, [prop]):
properties.append(subprop)
setattr(variable, prop, None)

View file

@ -45,7 +45,7 @@ class ServiceAnnotator:
self.objectspace.space.services.name = 'services'
self.objectspace.space.services.doc = 'services'
families = {}
for idx, service_name in enumerate(self.objectspace.space.services.service.keys()):
for service_name in self.objectspace.space.services.service.keys():
service = self.objectspace.space.services.service[service_name]
new_service = self.objectspace.service(service.xmlfiles)
for elttype, values in vars(service).items():

View file

@ -141,7 +141,7 @@ class VariableAnnotator:
if not hasattr(family, 'separators'):
continue
if hasattr(family.separators, 'separator'):
for idx, separator in enumerate(family.separators.separator):
for separator in family.separators.separator:
option = self.objectspace.paths.get_variable_obj(separator.name)
if hasattr(option, 'separator'):
subpath = self.objectspace.paths.get_variable_path(separator.name,
@ -151,4 +151,3 @@ class VariableAnnotator:
raise DictConsistencyError(_(f'{subpath} already has a separator in {xmlfiles}'), 35)
option.separator = separator.text
del family.separators

View file

@ -6,17 +6,15 @@ fichier de configuration pour rougail
from os.path import join, abspath, dirname
rougailroot = '/var/rougail'
dtddir = join(dirname(abspath(__file__)), 'data')
ROUGAILROOT = '/var/rougail'
DTDDIR = join(dirname(abspath(__file__)), 'data')
Config = {'rougailroot': rougailroot,
'patch_dir': join(rougailroot, 'patches'),
'manifests_dir': join(rougailroot, 'manifests'),
'templates_dir': join(rougailroot, 'templates'),
'dtdfilename': join(dtddir, 'rougail.dtd'),
'dtddir': dtddir,
# chemin du répertoire source des fichiers templates
'patch_dir': '/srv/rougail/patch',
Config = {'rougailroot': ROUGAILROOT,
'patch_dir': join(ROUGAILROOT, 'patches'),
'manifests_dir': join(ROUGAILROOT, 'manifests'),
'templates_dir': join(ROUGAILROOT, 'templates'),
'dtdfilename': join(DTDDIR, 'rougail.dtd'),
'dtddir': DTDDIR,
'variable_namespace': 'rougail',
}

View file

@ -11,7 +11,6 @@ class TemplateError(ConfigError):
class TemplateDisabled(TemplateError):
"""Template is disabled.
"""
pass
class OperationError(Exception):

View file

@ -1,6 +1,3 @@
from typing import List
from .i18n import _
from .xmlreflector import XMLReflector
from .utils import normalize_family
@ -111,7 +108,6 @@ class RougailObjSpace:
if name.endswith('_'):
name = name[:-1]
setattr(self, elt, type(name, (RootRougailObject,), dict()))
self.Leadership = self.leadership
def display_xmlfiles(self,
xmlfiles: list,
@ -165,8 +161,7 @@ class RougailObjSpace:
self.remove(child,
variableobj,
)
self.set_path(space,
child,
self.set_path(child,
namespace,
document,
variableobj,
@ -200,7 +195,7 @@ class RougailObjSpace:
child,
namespace,
)
elif Atom in obj.__mro__:
if Atom in obj.__mro__:
if child.tag in vars(space):
# Atom instance has to be a singleton here
# we do not re-create it, we reuse it
@ -368,7 +363,6 @@ class RougailObjSpace:
self.space.constraints.fill.pop(idx)
def set_path(self,
space,
child,
namespace,
document,
@ -382,7 +376,7 @@ class RougailObjSpace:
self.paths.add_variable(namespace,
child.attrib['name'],
family_name,
document.attrib.get('dynamic') != None,
document.attrib.get('dynamic') is not None,
variableobj,
)
if child.attrib.get('redefine', 'False') == 'True':

View file

@ -1,5 +1,4 @@
from .i18n import _
from .utils import normalize_family
from .error import OperationError, DictConsistencyError
from .config import Config
@ -142,7 +141,7 @@ class Path:
else:
dico = self._get_variable(name)
if not allow_source and dico['namespace'] not in [Config['variable_namespace'], 'services'] and current_namespace != dico['namespace']:
raise DictConsistencyError(_(f'A variable located in the "{dico["namespace"]}" namespace shall not be used in the "{current_namespace}" namespace'), 41)
raise DictConsistencyError(_(f'A variable located in the "{dico["namespace"]}" namespace shall not be used in the "{current_namespace}" namespace'), 41)
list_path = [dico['namespace'], dico['family']]
if dico['leader'] is not None:
list_path.append(dico['leader'])

View file

@ -18,7 +18,7 @@ from Cheetah.NameMapper import NotFound as CheetahNotFound
try:
from tiramisu3 import Config
from tiramisu3.error import PropertiesOptionError # pragma: no cover
except: # pragma: no cover
except ModuleNotFoundError: # pragma: no cover
from tiramisu import Config
from tiramisu.error import PropertiesOptionError
@ -212,8 +212,8 @@ class CreoleTemplateEngine:
if eosfunc_file is not None:
eosfunc = SourceFileLoader('eosfunc', eosfunc_file).load_module()
for func in dir(eosfunc):
if not func.startswith('_'):
eos[func] = getattr(eosfunc, func)
if not func.startswith('_'):
eos[func] = getattr(eosfunc, func)
self.eosfunc = eos
self.rougail_variables_dict = {}
@ -238,8 +238,8 @@ class CreoleTemplateEngine:
self.rougail_variables_dict[await option.option.name()] = await option.value.get()
async def load_eole_variables(self,
namespace,
optiondescription):
optiondescription,
):
families = {}
for family in await optiondescription.list('all'):
variables = {}
@ -257,9 +257,7 @@ class CreoleTemplateEngine:
)
variables[leader_name] = leader
else:
subfamilies = await self.load_eole_variables(await variable.option.name(),
variable,
)
subfamilies = await self.load_eole_variables(variable)
variables[await variable.option.name()] = subfamilies
else:
variables[await variable.option.name()] = await variable.value.get()
@ -301,7 +299,6 @@ class CreoleTemplateEngine:
source: str,
true_destfilename: str,
destfilename: str,
filevar: Dict,
variable: Any,
):
"""Process a cheetah template
@ -327,7 +324,6 @@ class CreoleTemplateEngine:
def instance_file(self,
filevar: Dict,
service_name: str,
tmp_dir: str,
dest_dir: str,
) -> None:
@ -355,8 +351,8 @@ class CreoleTemplateEngine:
self.process(source,
filename,
destfilename,
filevar,
var)
var,
)
else:
copy(source, destfilename)
@ -373,13 +369,11 @@ class CreoleTemplateEngine:
if namespace == Config['variable_namespace']:
await self.load_eole_variables_rougail(option)
else:
families = await self.load_eole_variables(namespace,
option)
families = await self.load_eole_variables(option)
self.rougail_variables_dict[namespace] = families
for template in listdir('.'):
self.prepare_template(template, tmp_dir, patch_dir)
for service_obj in await self.config.option('services').list('all'):
service_name = await service_obj.option.doc()
for fills in await service_obj.list('all'):
if await fills.option.name() in ['files', 'overrides']:
for fill_obj in await fills.list('all'):
@ -389,7 +383,6 @@ class CreoleTemplateEngine:
raise FileNotFound(_(f"File {filename} does not exist."))
if fill.get('activate', False):
self.instance_file(fill,
service_name,
tmp_dir,
dest_dir,
)

View file

@ -1,6 +1,6 @@
try:
from tiramisu3 import DynOptionDescription
except:
except ModuleNotFoundError:
from tiramisu import DynOptionDescription
from .utils import normalize_family

View file

@ -181,7 +181,6 @@ class ElementStorage:
class Common:
def __init__(self,
elt,
storage,
is_leader,
path,
@ -206,7 +205,7 @@ class Common:
self.attrib['properties'] += ', '
self.attrib['properties'] += prop
def get_attrib(self, attrib):
def get_attrib(self):
ret_list = []
for key, value in self.attrib.items():
if value is None:
@ -261,8 +260,7 @@ class Variable(Common):
is_leader,
path,
):
super().__init__(elt,
storage,
super().__init__(storage,
is_leader,
path,
)
@ -283,7 +281,7 @@ class Variable(Common):
self.attrib['opt'] = self.storage.get(self.attrib['opt']).get()
else:
self.parse_children()
attrib = self.get_attrib(self.attrib)
attrib = self.get_attrib()
self.option_name = self.storage.get_name(self.path)
self.storage.text.append(f'{self.option_name} = {self.object_type}({attrib})')
self.populate_informations()
@ -304,7 +302,7 @@ class Variable(Common):
self.attrib[key] = value
def parse_children(self):
if not 'default' in self.attrib or self.attrib['multi']:
if 'default' not in self.attrib or self.attrib['multi']:
self.attrib['default'] = []
if self.attrib['multi'] == 'submulti' and self.is_follower:
self.attrib['default_multi'] = []
@ -367,9 +365,9 @@ class Variable(Common):
):
if param.type == 'string':
return f'ParamValue("{param.text}")'
elif param.type == 'number':
if param.type == 'number':
return f'ParamValue({param.text})'
elif param.type == 'variable':
if param.type == 'variable':
value = {'option': param.text,
'notraisepropertyerror': param.notraisepropertyerror,
'todict': function in FUNC_TO_DICT,
@ -377,10 +375,10 @@ class Variable(Common):
if hasattr(param, 'suffix'):
value['suffix'] = param.suffix
return self.build_param(value)
elif param.type == 'information':
if param.type == 'information':
return f'ParamInformation("{param.text}", None)'
elif param.type == 'suffix':
return f'ParamSuffix()'
if param.type == 'suffix':
return 'ParamSuffix()'
raise LoaderError(_('unknown param type {}').format(param.type)) # pragma: no cover
def populate_value(self,
@ -418,8 +416,7 @@ class Family(Common):
is_leader,
path,
):
super().__init__(elt,
storage,
super().__init__(storage,
is_leader,
path,
)
@ -435,7 +432,7 @@ class Family(Common):
self.parse_children()
self.option_name = self.storage.get_name(self.path)
object_name = self.get_object_name()
attrib = self.get_attrib(self.attrib) + ', children=[' + ', '.join([child.get() for child in self.children]) + ']'
attrib = self.get_attrib() + ', children=[' + ', '.join([child.get() for child in self.children]) + ']'
self.storage.text.append(f'{self.option_name} = {object_name}({attrib})')
self.populate_informations()
return self.option_name
@ -461,6 +458,6 @@ class Family(Common):
def get_object_name(self):
if 'suffixes' in self.attrib:
return 'ConvertDynOptionDescription'
elif not self.is_leader:
if not self.is_leader:
return 'OptionDescription'
return 'Leadership'

View file

@ -2,7 +2,6 @@
utilitaires créole
"""
from unicodedata import normalize, combining
from .i18n import _
def normalize_family(family_name: str) -> str:

View file

@ -3,13 +3,13 @@ from typing import List
from os.path import join, isfile
from os import listdir
from lxml.etree import DTD, parse, tostring, XMLSyntaxError
from lxml.etree import DTD, parse, XMLSyntaxError
from .i18n import _
from .error import DictConsistencyError
class XMLReflector(object):
class XMLReflector:
"""Helper class for loading the Creole XML file,
parsing it, validating against the Creole DTD,
writing the xml result on the disk