From b0588768af35f7d9a70f67e3e711494e45fd16ac Mon Sep 17 00:00:00 2001 From: Emmanuel Garette Date: Sun, 14 Feb 2021 17:48:50 +0100 Subject: [PATCH] some correction --- src/rougail/annotator/check.py | 2 - src/rougail/annotator/family.py | 4 -- src/rougail/annotator/param.py | 5 +- src/rougail/convert.py | 63 ++++++++++--------- src/rougail/objspace.py | 24 +++---- src/rougail/xmlreflector.py | 33 ++++------ .../00-base.xml | 18 ++++++ .../__init__.py | 0 .../88valid_enum_not_number_variable/errno_26 | 0 9 files changed, 72 insertions(+), 77 deletions(-) create mode 100644 tests/dictionaries/88valid_enum_not_number_variable/00-base.xml create mode 100644 tests/dictionaries/88valid_enum_not_number_variable/__init__.py create mode 100644 tests/dictionaries/88valid_enum_not_number_variable/errno_26 diff --git a/src/rougail/annotator/check.py b/src/rougail/annotator/check.py index 2cc5e220..8d497a14 100644 --- a/src/rougail/annotator/check.py +++ b/src/rougail/annotator/check.py @@ -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 diff --git a/src/rougail/annotator/family.py b/src/rougail/annotator/family.py index cd9cdd02..d4a54799 100644 --- a/src/rougail/annotator/family.py +++ b/src/rougail/annotator/family.py @@ -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) diff --git a/src/rougail/annotator/param.py b/src/rougail/annotator/param.py index aa7545d0..c12ab364 100644 --- a/src/rougail/annotator/param.py +++ b/src/rougail/annotator/param.py @@ -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) diff --git a/src/rougail/convert.py b/src/rougail/convert.py index 1c3a83d4..143bbf82 100644 --- a/src/rougail/convert.py +++ b/src/rougail/convert.py @@ -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 diff --git a/src/rougail/objspace.py b/src/rougail/objspace.py index 6e681486..b32ba34e 100644 --- a/src/rougail/objspace.py +++ b/src/rougail/objspace.py @@ -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, diff --git a/src/rougail/xmlreflector.py b/src/rougail/xmlreflector.py index 68fd4e12..e59408b2 100644 --- a/src/rougail/xmlreflector.py +++ b/src/rougail/xmlreflector.py @@ -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() diff --git a/tests/dictionaries/88valid_enum_not_number_variable/00-base.xml b/tests/dictionaries/88valid_enum_not_number_variable/00-base.xml new file mode 100644 index 00000000..84dc6823 --- /dev/null +++ b/tests/dictionaries/88valid_enum_not_number_variable/00-base.xml @@ -0,0 +1,18 @@ + + + + + + + + + + + + str + enumvar + + + + diff --git a/tests/dictionaries/88valid_enum_not_number_variable/__init__.py b/tests/dictionaries/88valid_enum_not_number_variable/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/tests/dictionaries/88valid_enum_not_number_variable/errno_26 b/tests/dictionaries/88valid_enum_not_number_variable/errno_26 new file mode 100644 index 00000000..e69de29b