From 2ca9192267a24d99b376d528631a4e820e182546 Mon Sep 17 00:00:00 2001 From: Roberta Takenaka Date: Sun, 3 Nov 2024 08:18:09 -0300 Subject: [PATCH 1/3] =?UTF-8?q?Cria=20uma=20valida=C3=A7=C3=A3o=20de=20fig?= =?UTF-8?q?ura=20individual=20e=20uma=20valida=C3=A7=C3=A3o=20para=20as=20?= =?UTF-8?q?figuras=20do=20artigo?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packtools/sps/validation/fig.py | 148 +++++++++++++++++++------------- 1 file changed, 86 insertions(+), 62 deletions(-) diff --git a/packtools/sps/validation/fig.py b/packtools/sps/validation/fig.py index cdfc91aff..5ee7544ff 100644 --- a/packtools/sps/validation/fig.py +++ b/packtools/sps/validation/fig.py @@ -1,79 +1,103 @@ from packtools.sps.models.fig import ArticleFigs -from packtools.sps.validation.utils import format_response +from packtools.sps.validation.utils import format_response, build_response -class FigValidation: - """ - A class used to validate the existence of elements within an XML document. - - Attributes - ---------- - xml_tree : lxml.etree._ElementTree - The parsed XML document representing the article. - figures : list - A list of dictionaries containing information about figures extracted from the XML. - - Methods - ------- - validate_fig_existence(error_level="WARNING") - Validates the existence of elements within the XML document and yields formatted responses. - """ - - def __init__(self, xml_tree): +class ArticleFigValidation: + def __init__(self, xml_tree, rules): self.xml_tree = xml_tree - self.figures = list(ArticleFigs(xml_tree).get_all_figs) + self.rules = rules + self.article_types_requires = rules["article_types_requires"] + self.article_type = xml_tree.find(".").get("article-type") + self.required = self.article_type in self.article_types_requires + self.elements = list(ArticleFigs(xml_tree).get_all_figs) - def validate_fig_existence(self, error_level="WARNING"): - """ - Validates the existence of elements within the XML document. + def validate(self): + for element in self.elements: + yield from FigValidation(element, self.rules).validate() - If elements are found, yields a formatted response for each figure. - If no elements are found, yields a single formatted response indicating their absence. - - Parameters - ---------- - error_level : str, optional - The level of the error to be reported (default is "WARNING"). - - Yields - ------ - dict - A dictionary containing the validation response for each element, or a single response indicating no figures were found. - """ - if self.figures: - for figure in self.figures: + else: + # fig is absent + if self.required: yield format_response( title="fig presence", - parent=figure.get("parent"), - parent_id=figure.get("parent_id"), - parent_article_type=figure.get("parent_article_type"), - parent_lang=figure.get("parent_lang"), + parent="article", + parent_id=None, + parent_article_type=self.xml_tree.get("article-type"), + parent_lang=self.xml_tree.get( + "{http://www.w3.org/XML/1998/namespace}lang" + ), item="fig", sub_item=None, validation_type="exist", - is_valid=True, - expected=" element", - obtained=f'', - advice=None, - data=figure, - error_level="OK", + is_valid=False, + expected="", + obtained=None, + advice=f"article-type={self.article_type} requires . Found 0. Identify the fig or check if article-type is correct", + data=None, + error_level=self.rules["required_error_level"], ) - else: - yield format_response( - title="fig presence", - parent="article", - parent_id=None, - parent_article_type=self.xml_tree.get("article-type"), - parent_lang=self.xml_tree.get( - "{http://www.w3.org/XML/1998/namespace}lang" - ), + else: + yield format_response( + title="fig presence", + parent="article", + parent_id=None, + parent_article_type=self.xml_tree.get("article-type"), + parent_lang=self.xml_tree.get( + "{http://www.w3.org/XML/1998/namespace}lang" + ), + item="fig", + sub_item=None, + validation_type="exist", + is_valid=False, + expected=None, + obtained=None, + advice=f"article-type={self.article_type}, found 0 figures", + data=None, + error_level=self.rules["absent_error_level"], + ) + + +class FigValidation: + def __init__(self, data, rules): + self.data = data + self.rules = rules + + def validate(self): + yield self._validate_item("id") + yield self._validate_item("label") + yield self._validate_item("caption") + yield self.validate_content() + + def _validate_item(self, name): + if not self.data.get(name): + key_error_level = f"{name}_error_level" + yield build_response( + title=name, + parent=self.data, + item="fig", + sub_item=name, + validation_type="exist", + is_valid=False, + expected=name, + obtained=None, + advice=f"Identify the {name}", + data=self.data, + error_level=self.rules[key_error_level], + ) + + def validate_content(self): + if not self.get("graphic") and not self.get("alternatives"): + name = "graphic or alternatives" + yield build_response( + title=name, + parent=self.data, item="fig", - sub_item=None, + sub_item=name, validation_type="exist", is_valid=False, - expected=" element", + expected=name, obtained=None, - advice="Add element to illustrate the content.", - data=None, - error_level=error_level, + advice=f"Identify the {name}", + data=self.data, + error_level=self.rules["content_error_level"], ) From 4803bb8a903eccebe9b20145220b0f150bfc7139 Mon Sep 17 00:00:00 2001 From: Roberta Takenaka Date: Sun, 3 Nov 2024 08:18:56 -0300 Subject: [PATCH 2/3] Cria uma lista de article_types que 'exigem' elementos de artigos no XML --- packtools/sps/validation_rules/article_type_rules.json | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/packtools/sps/validation_rules/article_type_rules.json b/packtools/sps/validation_rules/article_type_rules.json index 619520d09..eebb291ab 100644 --- a/packtools/sps/validation_rules/article_type_rules.json +++ b/packtools/sps/validation_rules/article_type_rules.json @@ -26,6 +26,11 @@ "review-article", "reviewer-report", "other" + ], + "article_types_requires": [ + "case-report", + "research-article", + "review-article" ] } } \ No newline at end of file From ede15bfd04428dcf3dbfc6609b1684675991582b Mon Sep 17 00:00:00 2001 From: Roberta Takenaka Date: Sun, 3 Nov 2024 08:19:21 -0300 Subject: [PATCH 3/3] =?UTF-8?q?Atualiza=20as=20regras=20de=20valida=C3=A7?= =?UTF-8?q?=C3=A3o=20de=20figuras?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packtools/sps/validation_rules/fig_rules.json | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/packtools/sps/validation_rules/fig_rules.json b/packtools/sps/validation_rules/fig_rules.json index 4c28ab8c1..9c56cc971 100644 --- a/packtools/sps/validation_rules/fig_rules.json +++ b/packtools/sps/validation_rules/fig_rules.json @@ -1,5 +1,11 @@ { "fig_rules": { - "error_level": "WARNING" + "error_level": "WARNING", + "required_error_level": "CRITICAL", + "absent_error_level": "WARNING", + "id_error_level": "CRITICAL", + "label_error_level": "CRITICAL", + "caption_error_level": "CRITICAL", + "content_error_level": "CRITICAL", } } \ No newline at end of file