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

Faz melhorias em packtools.sps.validation.article_and_subarticles #731

Merged
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
58 changes: 28 additions & 30 deletions packtools/sps/validation/article_and_subarticles.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,14 +84,17 @@ def validate_language(self, language_codes_list=None, error_level=None):
msg = "<article article-type={} xml:lang={}>".format(
article_type, article_lang
)
parent__id = parent
else:
parent = "sub-article"
msg = "<sub-article article-type={} id={} xml:lang={}>".format(
article_type, article_id, article_lang
)
parent__id = f'{parent}[@id="{article_id}"]'

advice = None if validated else f'Provide for {parent__id}/@xml:lang one of {language_codes_list}'
yield format_response(
title="Article element lang attribute validation",
title="text language",
parent=parent,
parent_id=article_id,
parent_article_type=article_type,
Expand All @@ -102,9 +105,7 @@ def validate_language(self, language_codes_list=None, error_level=None):
is_valid=validated,
expected=language_codes_list,
obtained=article_lang,
advice="{} has {} as language, expected one item of this list: {}".format(
msg, article_lang, " | ".join(language_codes_list)
),
advice=advice,
data=article,
error_level=error_level
)
Expand Down Expand Up @@ -168,7 +169,7 @@ def validate_specific_use(self, specific_use_list=None, error_level=None):
specific_use_list = specific_use_list or self.specific_use_list
if not specific_use_list:
raise ValidationArticleAndSubArticlesSpecificUseException(
"Function requires list of specific uses"
"ArticleAttribsValidation.validate_specific_use requires specific_use_list"
)

validated = self.articles.main_specific_use in specific_use_list
Expand All @@ -179,8 +180,9 @@ def validate_specific_use(self, specific_use_list=None, error_level=None):
"dtd_version": self.articles.main_dtd_version
})

advice = None if validated else f"Provide for article/@specific-use one of {specific_use_list}"
yield format_response(
title="Article element specific-use attribute validation",
title="article/@specific-use",
parent="article",
parent_id=None,
parent_article_type=self.articles.main_article_type,
Expand All @@ -191,11 +193,7 @@ def validate_specific_use(self, specific_use_list=None, error_level=None):
is_valid=validated,
expected=specific_use_list,
obtained=self.articles.main_specific_use,
advice="XML {} has {} as specific-use, expected one item of this list: {}".format(
self.articles.main_article_type,
self.articles.main_specific_use,
" | ".join(specific_use_list),
),
advice=advice,
data=data,
error_level=error_level,
)
Expand Down Expand Up @@ -252,7 +250,7 @@ def validate_dtd_version(self, dtd_version_list=None, error_level=None):
dtd_version_list = dtd_version_list or self.dtd_version_list
if not dtd_version_list:
raise ValidationArticleAndSubArticlesDtdVersionException(
"Function requires list of dtd versions"
"ArticleAttribsValidation.validate_dtd_version requires dtd_version_list"
)

validated = self.articles.main_dtd_version in dtd_version_list
Expand All @@ -263,8 +261,9 @@ def validate_dtd_version(self, dtd_version_list=None, error_level=None):
"dtd_version": self.articles.main_dtd_version
})

advice = None if validated else f"Provide for article/@dtd-version one of {dtd_version_list}"
yield format_response(
title="Article element dtd-version attribute validation",
title="article/@dtd-version",
parent="article",
parent_id=None,
parent_article_type=self.articles.main_article_type,
Expand All @@ -275,11 +274,7 @@ def validate_dtd_version(self, dtd_version_list=None, error_level=None):
is_valid=validated,
expected=dtd_version_list,
obtained=self.articles.main_dtd_version,
advice="XML {} has {} as dtd-version, expected one item of this list: {}".format(
self.articles.main_article_type,
self.articles.main_dtd_version,
" | ".join(dtd_version_list),
),
advice=advice,
data=data,
error_level=error_level
)
Expand Down Expand Up @@ -351,7 +346,7 @@ def validate_article_type(self, article_type_list=None, error_level=None):

if not article_type_list:
raise ValidationArticleAndSubArticlesArticleTypeException(
"Function requires list of article types"
"ArticleTypeValidation.validate_article_type requires article_type_list"
)

validated = article_type in article_type_list
Expand All @@ -361,9 +356,9 @@ def validate_article_type(self, article_type_list=None, error_level=None):
"specific_use": self.articles.main_specific_use,
"dtd_version": self.articles.main_dtd_version
})

advice = None if validated else f"Provide for article/@article-type one of {article_type_list}"
yield format_response(
title="Article type validation",
title="article/@article-type",
parent="article",
parent_id=None,
parent_article_type=self.articles.main_article_type,
Expand All @@ -374,9 +369,7 @@ def validate_article_type(self, article_type_list=None, error_level=None):
is_valid=validated,
expected=article_type_list,
obtained=article_type,
advice="XML has {} as article-type, expected one item of this list: {}".format(
article_type, " | ".join(article_type_list)
),
advice=advice,
data=data,
error_level=error_level,
)
Expand Down Expand Up @@ -598,13 +591,18 @@ def validate_article_id_other(self, error_level=None):
},
}
"""
is_valid = self.article_ids.other.isnumeric() and len(self.article_ids.other) <= 5
if not self.article_ids.other:
return

try:
is_valid = 0 < int(self.article_ids.other) <= 99999
except (TypeError, ValueError, AttributeError):
is_valid = False

error_level = error_level or "ERROR"
expected_value = (
self.article_ids.other if is_valid else "a numeric value with up to five digits"
)
expected_value = "numerical value from 1 to 99999"
yield format_response(
title="Article id other validation",
title='article-id (@pub-id-type="other")',
parent="article",
parent_id=None,
parent_article_type=self.articles.main_article_type,
Expand All @@ -615,7 +613,7 @@ def validate_article_id_other(self, error_level=None):
is_valid=is_valid,
expected=expected_value,
obtained=self.article_ids.other,
advice='Provide a numeric value for <article-id pub-id-type="other"> with up to five digits',
advice='Provide for <article-id pub-id-type="other"> numerical value from 1 to 99999',
data=self.article_ids.data,
error_level=error_level,
)
4 changes: 3 additions & 1 deletion packtools/sps/validation/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ def format_response(
data,
error_level,
):
if validation_type == "value in list" and 'one of ' not in expected:
expected = f'one of {expected}'
return {
"title": title,
"parent": parent,
Expand All @@ -31,7 +33,7 @@ def format_response(
"sub_item": sub_item,
"validation_type": validation_type,
"response": "OK" if is_valid else error_level,
"expected_value": expected,
"expected_value": obtained if is_valid else expected,
"got_value": obtained,
"message": f"Got {obtained}, expected {expected}",
"advice": None if is_valid else advice,
Expand Down