some correction
This commit is contained in:
parent
c24ef61bff
commit
b0588768af
9 changed files with 72 additions and 77 deletions
|
@ -66,8 +66,6 @@ class CheckAnnotator(TargetAnnotator, ParamAnnotator):
|
|||
variable_type = None
|
||||
if obj.name == 'valid_enum':
|
||||
for target in obj.target:
|
||||
if variable_type and target.name.type != variable_type:
|
||||
raise Exception('pfff')
|
||||
variable_type = target.name.type
|
||||
return variable_type
|
||||
|
||||
|
|
|
@ -78,8 +78,6 @@ class FamilyAnnotator(Walk):
|
|||
return True
|
||||
else:
|
||||
return True
|
||||
else:
|
||||
return False
|
||||
return False
|
||||
|
||||
def remove_empty_families(self) -> None:
|
||||
|
@ -99,8 +97,6 @@ class FamilyAnnotator(Walk):
|
|||
for family in self.get_families():
|
||||
if not hasattr(family, 'description'):
|
||||
family.description = family.name
|
||||
if not hasattr(family, 'path'):
|
||||
family.path = family.name
|
||||
family.doc = family.description
|
||||
del family.description
|
||||
family.name = normalize_family(family.name)
|
||||
|
|
|
@ -69,7 +69,8 @@ class ParamAnnotator:
|
|||
)
|
||||
param.text = self.objectspace.paths.get_variable(path)
|
||||
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:
|
||||
param.suffix = suffix
|
||||
family_path = self.objectspace.paths.get_variable_family_path(path)
|
||||
|
@ -83,7 +84,7 @@ class ParamAnnotator:
|
|||
elif variable_type:
|
||||
if 'type' in vars(param) and variable_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)
|
||||
try:
|
||||
param.text = CONVERT_OPTION[variable_type].get('func', str)(param.text)
|
||||
|
|
|
@ -59,47 +59,48 @@ class RougailConvert:
|
|||
"""Rougail object
|
||||
"""
|
||||
def __init__(self) -> None:
|
||||
self.xmlreflector = XMLReflector()
|
||||
self.xmlreflector.parse_dtd()
|
||||
self.rougailobjspace = RougailObjSpace(self.xmlreflector)
|
||||
self._load_dictionaries(None, RougailConfig['dictionaries_dir'])
|
||||
for extra_name, extra_dir in RougailConfig['extra_dictionaries'].items():
|
||||
self._load_dictionaries(extra_name, extra_dir)
|
||||
xmlreflector = XMLReflector()
|
||||
rougailobjspace = RougailObjSpace(xmlreflector)
|
||||
self._load_dictionaries(xmlreflector,
|
||||
rougailobjspace,
|
||||
RougailConfig['variable_namespace'],
|
||||
RougailConfig['dictionaries_dir'],
|
||||
)
|
||||
for namespace, extra_dir in RougailConfig['extra_dictionaries'].items():
|
||||
if namespace in ['services', RougailConfig['variable_namespace']]:
|
||||
msg = _(f'Namespace name "{namespace}" is not allowed')
|
||||
raise DictConsistencyError(msg, 21, None)
|
||||
self._load_dictionaries(xmlreflector,
|
||||
rougailobjspace,
|
||||
namespace,
|
||||
extra_dir,
|
||||
)
|
||||
functions_file = RougailConfig['functions_file']
|
||||
SpaceAnnotator(rougailobjspace,
|
||||
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]:
|
||||
if namespace in ['services', RougailConfig['variable_namespace']]:
|
||||
msg = _(f'Namespace name "{namespace}" is not allowed')
|
||||
raise DictConsistencyError(msg, 21, None)
|
||||
if hasattr(self.rougailobjspace.space, 'variables') and \
|
||||
namespace in self.rougailobjspace.space.variables:
|
||||
msg = _(f'Namespace "{namespace}" is already loader')
|
||||
raise DictConsistencyError(msg, 2, None)
|
||||
for xmlfile in self.xmlreflector.load_xml_from_folders(xmlfolders):
|
||||
document = self.xmlreflector.parse_xmlfile(xmlfile)
|
||||
self.rougailobjspace.redefine_variables = []
|
||||
self.rougailobjspace.xml_parse_document(xmlfile,
|
||||
document,
|
||||
namespace,
|
||||
)
|
||||
for xmlfile, document in xmlreflector.load_xml_from_folders(xmlfolders):
|
||||
rougailobjspace.xml_parse_document(xmlfile,
|
||||
document,
|
||||
namespace,
|
||||
)
|
||||
|
||||
def save(self,
|
||||
filename: str,
|
||||
) -> str:
|
||||
"""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:
|
||||
with open(filename, 'w') as tiramisu:
|
||||
tiramisu.write(output)
|
||||
return output
|
||||
tiramisu.write(self.output)
|
||||
return self.output
|
||||
|
|
|
@ -169,8 +169,6 @@ class RougailObjSpace:
|
|||
):
|
||||
"""Parses a Rougail XML file and populates the RougailObjSpace
|
||||
"""
|
||||
if not namespace:
|
||||
namespace = RougailConfig['variable_namespace']
|
||||
redefine_variables = []
|
||||
self._xml_parse(xmlfile,
|
||||
document,
|
||||
|
@ -328,7 +326,8 @@ class RougailObjSpace:
|
|||
tag = FORCE_TAG.get(child.tag, child.tag)
|
||||
if tag not in vars(space):
|
||||
setattr(space, tag, {})
|
||||
return getattr(self, child.tag)(xmlfile, name)
|
||||
obj = getattr(self, child.tag)(xmlfile, name)
|
||||
return obj
|
||||
|
||||
def get_existed_obj(self,
|
||||
name: str,
|
||||
|
@ -342,10 +341,7 @@ class RougailObjSpace:
|
|||
name = normalize_family(name)
|
||||
if child.tag == 'variable': # pylint: disable=E1101
|
||||
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):
|
||||
return None
|
||||
old_family_name = self.paths.get_variable_family_path(name)
|
||||
|
@ -469,30 +465,24 @@ class RougailObjSpace:
|
|||
family_name = normalize_family(document.attrib['name'])
|
||||
else:
|
||||
family_name = namespace
|
||||
if isinstance(space, self.variables):
|
||||
subpath = space.name
|
||||
else:
|
||||
subpath = space.path
|
||||
|
||||
self.paths.add_variable(namespace,
|
||||
normalize_family(variableobj.name),
|
||||
subpath,
|
||||
space.path,
|
||||
document.attrib.get('dynamic') is not None,
|
||||
variableobj,
|
||||
)
|
||||
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)
|
||||
if namespace != RougailConfig['variable_namespace']:
|
||||
family_name = namespace + '.' + family_name
|
||||
self.paths.add_family(namespace,
|
||||
family_name,
|
||||
variableobj,
|
||||
subpath,
|
||||
space.path,
|
||||
)
|
||||
elif isinstance(variableobj, self.variables):
|
||||
variableobj.path = variableobj.name
|
||||
|
||||
@staticmethod
|
||||
def add_to_tree_structure(variableobj,
|
||||
|
|
|
@ -41,9 +41,6 @@ class XMLReflector:
|
|||
writing the xml result on the disk
|
||||
"""
|
||||
def __init__(self):
|
||||
self.dtd = None
|
||||
|
||||
def parse_dtd(self) -> None:
|
||||
"""Loads the Creole DTD
|
||||
|
||||
:raises IOError: if the DTD is not found
|
||||
|
@ -56,23 +53,9 @@ class XMLReflector:
|
|||
with open(dtdfilename, 'r') as dtdfd:
|
||||
self.dtd = DTD(dtdfd)
|
||||
|
||||
def parse_xmlfile(self, xmlfile):
|
||||
"""Parses and validates some Creole XML against the Creole DTD
|
||||
|
||||
: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]):
|
||||
def load_xml_from_folders(self,
|
||||
xmlfolders: List[str],
|
||||
):
|
||||
"""Loads all the XML files located in the xmlfolders' list
|
||||
|
||||
:param xmlfolders: list of full folder's name
|
||||
|
@ -82,4 +65,12 @@ class XMLReflector:
|
|||
filename.endswith('.xml')]
|
||||
filenames.sort()
|
||||
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 a new issue