some correction
This commit is contained in:
parent
c24ef61bff
commit
b0588768af
|
@ -66,8 +66,6 @@ class CheckAnnotator(TargetAnnotator, ParamAnnotator):
|
||||||
variable_type = None
|
variable_type = None
|
||||||
if obj.name == 'valid_enum':
|
if obj.name == 'valid_enum':
|
||||||
for target in obj.target:
|
for target in obj.target:
|
||||||
if variable_type and target.name.type != variable_type:
|
|
||||||
raise Exception('pfff')
|
|
||||||
variable_type = target.name.type
|
variable_type = target.name.type
|
||||||
return variable_type
|
return variable_type
|
||||||
|
|
||||||
|
|
|
@ -78,8 +78,6 @@ class FamilyAnnotator(Walk):
|
||||||
return True
|
return True
|
||||||
else:
|
else:
|
||||||
return True
|
return True
|
||||||
else:
|
|
||||||
return False
|
|
||||||
return False
|
return False
|
||||||
|
|
||||||
def remove_empty_families(self) -> None:
|
def remove_empty_families(self) -> None:
|
||||||
|
@ -99,8 +97,6 @@ class FamilyAnnotator(Walk):
|
||||||
for family in self.get_families():
|
for family in self.get_families():
|
||||||
if not hasattr(family, 'description'):
|
if not hasattr(family, 'description'):
|
||||||
family.description = family.name
|
family.description = family.name
|
||||||
if not hasattr(family, 'path'):
|
|
||||||
family.path = family.name
|
|
||||||
family.doc = family.description
|
family.doc = family.description
|
||||||
del family.description
|
del family.description
|
||||||
family.name = normalize_family(family.name)
|
family.name = normalize_family(family.name)
|
||||||
|
|
|
@ -69,7 +69,8 @@ class ParamAnnotator:
|
||||||
)
|
)
|
||||||
param.text = self.objectspace.paths.get_variable(path)
|
param.text = self.objectspace.paths.get_variable(path)
|
||||||
if variable_type and param.text.type != variable_type:
|
if variable_type and param.text.type != variable_type:
|
||||||
raise Exception('pfff', variable_type, param.text.type)
|
msg = _(f'"{obj.name}" has type "{variable_type}" but param has type "{param.text.type}"')
|
||||||
|
raise DictConsistencyError(msg, 26, param.xmlfiles)
|
||||||
if suffix:
|
if suffix:
|
||||||
param.suffix = suffix
|
param.suffix = suffix
|
||||||
family_path = self.objectspace.paths.get_variable_family_path(path)
|
family_path = self.objectspace.paths.get_variable_family_path(path)
|
||||||
|
@ -83,7 +84,7 @@ class ParamAnnotator:
|
||||||
elif variable_type:
|
elif variable_type:
|
||||||
if 'type' in vars(param) and variable_type != param.type:
|
if 'type' in vars(param) and variable_type != param.type:
|
||||||
msg = _(f'parameter has incompatible type "{param.type}" '
|
msg = _(f'parameter has incompatible type "{param.type}" '
|
||||||
f'with type "{variable_type}")')
|
f'with type "{variable_type}"')
|
||||||
raise DictConsistencyError(msg, 7, param.xmlfiles)
|
raise DictConsistencyError(msg, 7, param.xmlfiles)
|
||||||
try:
|
try:
|
||||||
param.text = CONVERT_OPTION[variable_type].get('func', str)(param.text)
|
param.text = CONVERT_OPTION[variable_type].get('func', str)(param.text)
|
||||||
|
|
|
@ -59,28 +59,38 @@ class RougailConvert:
|
||||||
"""Rougail object
|
"""Rougail object
|
||||||
"""
|
"""
|
||||||
def __init__(self) -> None:
|
def __init__(self) -> None:
|
||||||
self.xmlreflector = XMLReflector()
|
xmlreflector = XMLReflector()
|
||||||
self.xmlreflector.parse_dtd()
|
rougailobjspace = RougailObjSpace(xmlreflector)
|
||||||
self.rougailobjspace = RougailObjSpace(self.xmlreflector)
|
self._load_dictionaries(xmlreflector,
|
||||||
self._load_dictionaries(None, RougailConfig['dictionaries_dir'])
|
rougailobjspace,
|
||||||
for extra_name, extra_dir in RougailConfig['extra_dictionaries'].items():
|
RougailConfig['variable_namespace'],
|
||||||
self._load_dictionaries(extra_name, extra_dir)
|
RougailConfig['dictionaries_dir'],
|
||||||
|
)
|
||||||
def _load_dictionaries(self,
|
for namespace, extra_dir in RougailConfig['extra_dictionaries'].items():
|
||||||
namespace: str,
|
|
||||||
xmlfolders: List[str],
|
|
||||||
) -> List[str]:
|
|
||||||
if namespace in ['services', RougailConfig['variable_namespace']]:
|
if namespace in ['services', RougailConfig['variable_namespace']]:
|
||||||
msg = _(f'Namespace name "{namespace}" is not allowed')
|
msg = _(f'Namespace name "{namespace}" is not allowed')
|
||||||
raise DictConsistencyError(msg, 21, None)
|
raise DictConsistencyError(msg, 21, None)
|
||||||
if hasattr(self.rougailobjspace.space, 'variables') and \
|
self._load_dictionaries(xmlreflector,
|
||||||
namespace in self.rougailobjspace.space.variables:
|
rougailobjspace,
|
||||||
msg = _(f'Namespace "{namespace}" is already loader')
|
namespace,
|
||||||
raise DictConsistencyError(msg, 2, None)
|
extra_dir,
|
||||||
for xmlfile in self.xmlreflector.load_xml_from_folders(xmlfolders):
|
)
|
||||||
document = self.xmlreflector.parse_xmlfile(xmlfile)
|
functions_file = RougailConfig['functions_file']
|
||||||
self.rougailobjspace.redefine_variables = []
|
SpaceAnnotator(rougailobjspace,
|
||||||
self.rougailobjspace.xml_parse_document(xmlfile,
|
functions_file,
|
||||||
|
)
|
||||||
|
self.output = TiramisuReflector(rougailobjspace,
|
||||||
|
functions_file,
|
||||||
|
).get_text() + '\n'
|
||||||
|
|
||||||
|
def _load_dictionaries(self,
|
||||||
|
xmlreflector: XMLReflector,
|
||||||
|
rougailobjspace: RougailObjSpace,
|
||||||
|
namespace: str,
|
||||||
|
xmlfolders: List[str],
|
||||||
|
) -> List[str]:
|
||||||
|
for xmlfile, document in xmlreflector.load_xml_from_folders(xmlfolders):
|
||||||
|
rougailobjspace.xml_parse_document(xmlfile,
|
||||||
document,
|
document,
|
||||||
namespace,
|
namespace,
|
||||||
)
|
)
|
||||||
|
@ -90,16 +100,7 @@ class RougailConvert:
|
||||||
) -> str:
|
) -> str:
|
||||||
"""Return tiramisu object declaration as a string
|
"""Return tiramisu object declaration as a string
|
||||||
"""
|
"""
|
||||||
functions_file = RougailConfig['functions_file']
|
|
||||||
SpaceAnnotator(self.rougailobjspace,
|
|
||||||
functions_file,
|
|
||||||
)
|
|
||||||
tiramisu_objects = TiramisuReflector(self.rougailobjspace,
|
|
||||||
functions_file,
|
|
||||||
)
|
|
||||||
self.rougailobjspace = None
|
|
||||||
output = tiramisu_objects.get_text() + '\n'
|
|
||||||
if filename:
|
if filename:
|
||||||
with open(filename, 'w') as tiramisu:
|
with open(filename, 'w') as tiramisu:
|
||||||
tiramisu.write(output)
|
tiramisu.write(self.output)
|
||||||
return output
|
return self.output
|
||||||
|
|
|
@ -169,8 +169,6 @@ class RougailObjSpace:
|
||||||
):
|
):
|
||||||
"""Parses a Rougail XML file and populates the RougailObjSpace
|
"""Parses a Rougail XML file and populates the RougailObjSpace
|
||||||
"""
|
"""
|
||||||
if not namespace:
|
|
||||||
namespace = RougailConfig['variable_namespace']
|
|
||||||
redefine_variables = []
|
redefine_variables = []
|
||||||
self._xml_parse(xmlfile,
|
self._xml_parse(xmlfile,
|
||||||
document,
|
document,
|
||||||
|
@ -328,7 +326,8 @@ class RougailObjSpace:
|
||||||
tag = FORCE_TAG.get(child.tag, child.tag)
|
tag = FORCE_TAG.get(child.tag, child.tag)
|
||||||
if tag not in vars(space):
|
if tag not in vars(space):
|
||||||
setattr(space, tag, {})
|
setattr(space, tag, {})
|
||||||
return getattr(self, child.tag)(xmlfile, name)
|
obj = getattr(self, child.tag)(xmlfile, name)
|
||||||
|
return obj
|
||||||
|
|
||||||
def get_existed_obj(self,
|
def get_existed_obj(self,
|
||||||
name: str,
|
name: str,
|
||||||
|
@ -342,9 +341,6 @@ class RougailObjSpace:
|
||||||
name = normalize_family(name)
|
name = normalize_family(name)
|
||||||
if child.tag == 'variable': # pylint: disable=E1101
|
if child.tag == 'variable': # pylint: disable=E1101
|
||||||
if namespace != RougailConfig['variable_namespace']:
|
if namespace != RougailConfig['variable_namespace']:
|
||||||
if isinstance(space, self.variables):
|
|
||||||
name = space.name + '.' + name
|
|
||||||
else:
|
|
||||||
name = space.path + '.' + name
|
name = space.path + '.' + name
|
||||||
if not self.paths.path_is_defined(name):
|
if not self.paths.path_is_defined(name):
|
||||||
return None
|
return None
|
||||||
|
@ -469,30 +465,24 @@ class RougailObjSpace:
|
||||||
family_name = normalize_family(document.attrib['name'])
|
family_name = normalize_family(document.attrib['name'])
|
||||||
else:
|
else:
|
||||||
family_name = namespace
|
family_name = namespace
|
||||||
if isinstance(space, self.variables):
|
|
||||||
subpath = space.name
|
|
||||||
else:
|
|
||||||
subpath = space.path
|
|
||||||
|
|
||||||
self.paths.add_variable(namespace,
|
self.paths.add_variable(namespace,
|
||||||
normalize_family(variableobj.name),
|
normalize_family(variableobj.name),
|
||||||
subpath,
|
space.path,
|
||||||
document.attrib.get('dynamic') is not None,
|
document.attrib.get('dynamic') is not None,
|
||||||
variableobj,
|
variableobj,
|
||||||
)
|
)
|
||||||
elif isinstance(variableobj, self.family): # pylint: disable=E1101
|
elif isinstance(variableobj, self.family): # pylint: disable=E1101
|
||||||
if isinstance(space, self.variables):
|
|
||||||
subpath = space.name
|
|
||||||
else:
|
|
||||||
subpath = space.path
|
|
||||||
family_name = normalize_family(variableobj.name)
|
family_name = normalize_family(variableobj.name)
|
||||||
if namespace != RougailConfig['variable_namespace']:
|
if namespace != RougailConfig['variable_namespace']:
|
||||||
family_name = namespace + '.' + family_name
|
family_name = namespace + '.' + family_name
|
||||||
self.paths.add_family(namespace,
|
self.paths.add_family(namespace,
|
||||||
family_name,
|
family_name,
|
||||||
variableobj,
|
variableobj,
|
||||||
subpath,
|
space.path,
|
||||||
)
|
)
|
||||||
|
elif isinstance(variableobj, self.variables):
|
||||||
|
variableobj.path = variableobj.name
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def add_to_tree_structure(variableobj,
|
def add_to_tree_structure(variableobj,
|
||||||
|
|
|
@ -41,9 +41,6 @@ class XMLReflector:
|
||||||
writing the xml result on the disk
|
writing the xml result on the disk
|
||||||
"""
|
"""
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.dtd = None
|
|
||||||
|
|
||||||
def parse_dtd(self) -> None:
|
|
||||||
"""Loads the Creole DTD
|
"""Loads the Creole DTD
|
||||||
|
|
||||||
:raises IOError: if the DTD is not found
|
:raises IOError: if the DTD is not found
|
||||||
|
@ -56,23 +53,9 @@ class XMLReflector:
|
||||||
with open(dtdfilename, 'r') as dtdfd:
|
with open(dtdfilename, 'r') as dtdfd:
|
||||||
self.dtd = DTD(dtdfd)
|
self.dtd = DTD(dtdfd)
|
||||||
|
|
||||||
def parse_xmlfile(self, xmlfile):
|
def load_xml_from_folders(self,
|
||||||
"""Parses and validates some Creole XML against the Creole DTD
|
xmlfolders: List[str],
|
||||||
|
):
|
||||||
:returns: the root element tree object
|
|
||||||
"""
|
|
||||||
try:
|
|
||||||
document = parse(xmlfile)
|
|
||||||
except XMLSyntaxError as err:
|
|
||||||
raise DictConsistencyError(_(f'not a XML file: {err}'), 52, [xmlfile]) from err
|
|
||||||
if not self.dtd.validate(document):
|
|
||||||
dtd_error = self.dtd.error_log.filter_from_errors()[0]
|
|
||||||
msg = _(f'not a valid XML file: {dtd_error}')
|
|
||||||
raise DictConsistencyError(msg, 43, [xmlfile])
|
|
||||||
return document.getroot()
|
|
||||||
|
|
||||||
@staticmethod
|
|
||||||
def load_xml_from_folders(xmlfolders: List[str]):
|
|
||||||
"""Loads all the XML files located in the xmlfolders' list
|
"""Loads all the XML files located in the xmlfolders' list
|
||||||
|
|
||||||
:param xmlfolders: list of full folder's name
|
:param xmlfolders: list of full folder's name
|
||||||
|
@ -82,4 +65,12 @@ class XMLReflector:
|
||||||
filename.endswith('.xml')]
|
filename.endswith('.xml')]
|
||||||
filenames.sort()
|
filenames.sort()
|
||||||
for xmlfile in filenames:
|
for xmlfile in filenames:
|
||||||
yield xmlfile
|
try:
|
||||||
|
document = parse(xmlfile)
|
||||||
|
except XMLSyntaxError as err:
|
||||||
|
raise DictConsistencyError(_(f'not a XML file: {err}'), 52, [xmlfile]) from err
|
||||||
|
if not self.dtd.validate(document):
|
||||||
|
dtd_error = self.dtd.error_log.filter_from_errors()[0]
|
||||||
|
msg = _(f'not a valid XML file: {dtd_error}')
|
||||||
|
raise DictConsistencyError(msg, 43, [xmlfile])
|
||||||
|
yield xmlfile, document.getroot()
|
||||||
|
|
|
@ -0,0 +1,18 @@
|
||||||
|
<?xml version='1.0' encoding='UTF-8'?>
|
||||||
|
<rougail>
|
||||||
|
<variables>
|
||||||
|
<family name="enumfam" mode="expert">
|
||||||
|
<variable name="str" multi="True"/>
|
||||||
|
<variable name="enumvar" type="number"/>
|
||||||
|
</family>
|
||||||
|
</variables>
|
||||||
|
|
||||||
|
<constraints>
|
||||||
|
<check name="valid_enum">
|
||||||
|
<param type="variable">str</param>
|
||||||
|
<target>enumvar</target>
|
||||||
|
</check>
|
||||||
|
</constraints>
|
||||||
|
</rougail>
|
||||||
|
<!-- vim: ts=4 sw=4 expandtab
|
||||||
|
-->
|
Loading…
Reference in New Issue