Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cria validação individual das figuras e cria validação de todas as figuras do artigo #745

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
148 changes: 86 additions & 62 deletions packtools/sps/validation/fig.py
Original file line number Diff line number Diff line change
@@ -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 <fig> 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 <fig> 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 <fig> elements within the XML document.
def validate(self):
for element in self.elements:
yield from FigValidation(element, self.rules).validate()

If <fig> elements are found, yields a formatted response for each figure.
If no <fig> 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 <fig> 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="<fig> element",
obtained=f'<fig fig-type="{figure.get("fig_type")}" id="{figure.get("fig_id")}">',
advice=None,
data=figure,
error_level="OK",
is_valid=False,
expected="<fig/>",
obtained=None,
advice=f"article-type={self.article_type} requires <fig/>. 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="<fig> element",
expected=name,
obtained=None,
advice="Add <fig> 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"],
)
5 changes: 5 additions & 0 deletions packtools/sps/validation_rules/article_type_rules.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@
"review-article",
"reviewer-report",
"other"
],
"article_types_requires": [
"case-report",
"research-article",
"review-article"
]
}
}
8 changes: 7 additions & 1 deletion packtools/sps/validation_rules/fig_rules.json
Original file line number Diff line number Diff line change
@@ -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",
}
}