Skip to content

Commit

Permalink
Refactor: front articlemeta issue (#873)
Browse files Browse the repository at this point in the history
* Adiciona "front_articlemeta_issue_rules"

* Cria variável

* Adiciona 'params'

* Formata valores do dicionário

* Adapta os testes
  • Loading branch information
Rossi-Luciano authored Feb 7, 2025
1 parent 0e39781 commit 0ebe569
Show file tree
Hide file tree
Showing 4 changed files with 219 additions and 296 deletions.
97 changes: 54 additions & 43 deletions packtools/sps/validation/front_articlemeta_issue.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def __init__(self, xml_tree, params):
self.article_issue = ArticleMetaIssue(xml_tree)
self.params = params

def validate_volume_format(self, error_level):
def validate_volume_format(self):
"""
Validates the format of the volume number.
Checks if the volume number is a valid non-zero alphanumeric value.
Expand All @@ -63,20 +63,22 @@ def validate_volume_format(self, error_level):
if self.article_issue.volume:
result = is_valid_value(self.article_issue.volume, zero_is_allowed=False)
return build_response(
title="volume",
title="issue volume format",
parent={"parent": "article"},
item="volume",
sub_item=None,
validation_type="format",
is_valid=result["is_valid"],
expected=result["expected"],
obtained=result["got"],
advice="Consulte SPS documentation to complete volume element",
advice=f"Replace {result["got"]} in <article-meta><volume> with {result["expected"]}",
data=self.article_issue.data,
error_level=error_level,
error_level=self.params["volume_format_error_level"],
element_name="article-meta",
sub_element_name="volume"
)

def validate_number_format(self, error_level):
def validate_number_format(self):
"""
Validates the format of the issue number.
Checks if the issue number is a valid non-zero alphanumeric value.
Expand All @@ -90,20 +92,22 @@ def validate_number_format(self, error_level):
if self.article_issue.number:
result = is_valid_value(self.article_issue.number, zero_is_allowed=False)
return build_response(
title="number",
title="issue number format",
parent={"parent": "article"},
item="number",
sub_item=None,
validation_type="format",
is_valid=result["is_valid"],
expected=result["expected"],
obtained=result["got"],
advice="Consulte SPS documentation to complete issue element",
advice=f"Replace {result["got"]} in <article-meta><issue> with {result["expected"]}",
data=self.article_issue.data,
error_level=error_level,
error_level=self.params["number_format_error_level"],
element_name="article-meta",
sub_element_name="issue"
)

def validate_supplement_format(self, error_level):
def validate_supplement_format(self):
"""
Validates the format of the supplement number.
Unlike volume and issue, supplement can be zero.
Expand All @@ -117,20 +121,22 @@ def validate_supplement_format(self, error_level):
if self.article_issue.suppl:
result = is_valid_value(self.article_issue.suppl, zero_is_allowed=True)
return build_response(
title="supplement",
title="supplement format",
parent={"parent": "article"},
item="supplement",
sub_item=None,
validation_type="format",
is_valid=result["is_valid"],
expected=result["expected"],
obtained=result["got"],
advice="Consulte SPS documentation to complete issue or supplement elements",
advice=f"Replace {result["got"]} in <article-meta><supplement> with {result["expected"]}",
data=self.article_issue.data,
error_level=error_level,
error_level=self.params["supplement_format_error_level"],
element_name="article-meta",
sub_element_name="supplement"
)

def validate_issue_format(self, error_level):
def validate_issue_format(self):
"""
Validates special issue or supplement format.
Handles different formats like 'spe' (special) and 'sup' (supplement).
Expand Down Expand Up @@ -179,15 +185,17 @@ def validate_issue_format(self, error_level):
expected=expected,
obtained=parsed_issue,
advice=(
"Consulte SPS documentation to complete issue element"
f"Replace {parsed_issue["type"]} {parsed_issue["type_value"]} in <article-meta><issue> with one of {expected}"
if not got_valid_format
else None
),
data={"issue": self.article_issue.issue},
error_level=error_level,
error_level=self.params["issue_format_error_level"],
element_name="article-meta",
sub_element_name="issue"
)

def validate_expected_issues(self, expected_issues, error_level):
def validate_expected_issues(self, expected_issues):
"""
Validates if the issue exists in the list of expected issues.
Expand Down Expand Up @@ -215,7 +223,9 @@ def validate_expected_issues(self, expected_issues, error_level):
obtained=issue,
advice="Provide registered issues to check issue data in XML file",
data={"issue": issue},
error_level=error_level,
error_level=self.params["expected_issues_error_level"],
element_name="article-meta",
sub_element_name="issue"
)

return build_response(
Expand All @@ -227,9 +237,11 @@ def validate_expected_issues(self, expected_issues, error_level):
is_valid=issue in expected_issues,
expected=expected_issues,
obtained=issue,
advice="Consulte SPS documentation to complete volume, issue and supplement elements",
advice=f"Replace {issue} in <article-meta><issue> with one of {expected_issues}",
data={"issue": issue},
error_level=error_level,
error_level=self.params["expected_issues_error_level"],
element_name="article-meta",
sub_element_name="issue"
)

def validate(self):
Expand All @@ -241,12 +253,10 @@ def validate(self):
Raises:
MissingJournalDataException: If journal_data is missing from params
"""
yield self.validate_volume_format(self.params["volume_format_error_level"])
yield self.validate_number_format(self.params["number_format_error_level"])
yield self.validate_supplement_format(
self.params["supplement_format_error_level"]
)
yield self.validate_issue_format(self.params["issue_format_error_level"])
yield self.validate_volume_format()
yield self.validate_number_format()
yield self.validate_supplement_format()
yield self.validate_issue_format()

try:
journal_data = self.params["journal_data"]
Expand All @@ -257,8 +267,7 @@ def validate(self):
else:
expected_issues = journal_data.get("issues")
yield self.validate_expected_issues(
expected_issues,
self.params["expected_issues_error_level"],
expected_issues
)


Expand All @@ -268,7 +277,7 @@ class PaginationValidation:
Checks for proper page numbering or electronic location ID.
"""

def __init__(self, xml_tree):
def __init__(self, xml_tree, params):
"""
Initialize with XML tree containing pagination data.
Expand All @@ -277,8 +286,9 @@ def __init__(self, xml_tree):
"""
self.xml_tree = xml_tree
self.issue = ArticleMetaIssue(xml_tree)
self.params = params

def validate(self, error_level):
def validate(self):
"""
Validates pagination format according to SPS rules.
Article must have either:
Expand Down Expand Up @@ -308,17 +318,18 @@ def validate(self, error_level):
# ahead of print article - no pagination needed
is_valid = True

if not is_valid:
return build_response(
title="Pagination",
parent={"parent": "article"},
item="elocation-id | fpage / lpage",
sub_item="elocation-id | fpage / lpage",
validation_type="match",
is_valid=is_valid,
expected="elocation-id or fpage + lpage",
obtained=f"elocation-id: {self.issue.elocation_id}, fpage: {self.issue.fpage}, lpage: {self.issue.lpage}",
advice="Provide elocation-id or fpage + lpage",
data=self.issue.data,
error_level=error_level,
)
return build_response(
title="Pagination",
parent={"parent": "article"},
item="elocation-id | fpage / lpage",
sub_item="elocation-id | fpage / lpage",
validation_type="match",
is_valid=is_valid,
expected="elocation-id or fpage + lpage",
obtained=f"elocation-id: {self.issue.elocation_id}, fpage: {self.issue.fpage}, lpage: {self.issue.lpage}",
advice="Provide elocation-id or fpage + lpage",
data=self.issue.data,
error_level=self.params["pagination_error_level"],
element_name="article-meta",
sub_element_name="issue"
)
1 change: 1 addition & 0 deletions packtools/sps/validation/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,7 @@ def format_advice(
elif validation_type == "value in list":

if obtained:
incorrect = ""
if attribute_name:
incorrect = f' {attribute_name}="{obtained}"'
if sub_element_name:
Expand Down
13 changes: 13 additions & 0 deletions packtools/sps/validation_rules/front_articlemeta_issue_rules.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"front_articlemeta_issue_rules": {
"volume_format_error_level": "CRITICAL",
"number_format_error_level": "CRITICAL",
"supplement_format_error_level": "CRITICAL",
"issue_format_error_level": "CRITICAL",
"expected_issues_error_level": "CRITICAL",
"pagination_error_level": "CRITICAL",
"journal_data": {
"issues": []
}
}
}
Loading

0 comments on commit 0ebe569

Please sign in to comment.