diff --git a/packtools/sps/formats/pubmed.py b/packtools/sps/formats/pubmed.py index f99b2651d..92f78dba8 100644 --- a/packtools/sps/formats/pubmed.py +++ b/packtools/sps/formats/pubmed.py @@ -16,6 +16,19 @@ ) +class MissingRequiredElementError(Exception): + """Raised when a SciELO article has no data for an element the PubMed + DTD marks as required (no `?`), so a valid
cannot be built + for it. `element_path` identifies which element (e.g. "Journal/PubDate").""" + + def __init__(self, element_path): + self.element_path = element_path + super().__init__( + f"{element_path} é obrigatório na DTD do PubMed e não foi " + "possível determinar seu valor para este artigo." + ) + + def xml_pubmed_article_pipe(): return ET.Element("Article") @@ -127,20 +140,21 @@ def xml_pubmed_pub_date_pipe(xml_pubmed, xml_tree): """ date = get_date(xml_tree) - if date is not None: - dt = ET.Element("PubDate") - dt.set("PubStatus", "epublish") - for element in ["year", "month", "day"]: - # TODO - # Season - # The season of publication. e.g.,Winter, Spring, Summer, Fall. Do not use if a Month is available. - # There is no example of using this value in the files. - - if date.get(element): - el = ET.Element(element.capitalize()) - el.text = date.get(element) - dt.append(el) - xml_pubmed.find("Journal").append(dt) + if date is None: + raise MissingRequiredElementError("Journal/PubDate") + dt = ET.Element("PubDate") + dt.set("PubStatus", "epublish") + for element in ["year", "month", "day"]: + # TODO + # Season + # The season of publication. e.g.,Winter, Spring, Summer, Fall. Do not use if a Month is available. + # There is no example of using this value in the files. + + if date.get(element): + el = ET.Element(element.capitalize()) + el.text = date.get(element) + dt.append(el) + xml_pubmed.find("Journal").append(dt) def xml_pubmed_replaces_pipe(xml_pubmed, xml_tree): diff --git a/packtools/sps/formats/pubmed_generator.py b/packtools/sps/formats/pubmed_generator.py index eccd7388c..e77bb9caa 100644 --- a/packtools/sps/formats/pubmed_generator.py +++ b/packtools/sps/formats/pubmed_generator.py @@ -1,4 +1,5 @@ import argparse +import sys from packtools.sps.formats import pubmed from packtools.sps.utils import xml_utils @@ -27,7 +28,12 @@ def main(): arguments = parser.parse_args() xml_tree = xml_utils.get_xml_tree(arguments.path_to_read) - xml_pubmed = pubmed.pipeline_pubmed(xml_tree) + try: + xml_pubmed = pubmed.pipeline_pubmed(xml_tree) + except pubmed.MissingRequiredElementError as exc: + print(f"Erro: {exc}", file=sys.stderr) + raise SystemExit(1) + with open(arguments.path_to_write, "w", encoding="utf-8") as file: file.write(xml_pubmed) print(f"Arquivo criado em: {arguments.path_to_write}") diff --git a/tests/sps/formats/test_pubmed.py b/tests/sps/formats/test_pubmed.py index a8afad598..5284a39b9 100644 --- a/tests/sps/formats/test_pubmed.py +++ b/tests/sps/formats/test_pubmed.py @@ -25,6 +25,8 @@ xml_pubmed_citations, xml_pubmed_abstract, xml_pubmed_other_abstract, + pipeline_pubmed, + MissingRequiredElementError, ) @@ -506,11 +508,6 @@ def test_xml_pubmed_pub_date_pipe_without_year(self): self.assertEqual(obtained, expected) def test_xml_pubmed_pub_date_pipe_without_date(self): - expected = ( - '
' - '' - '
' - ) xml_pubmed = ET.fromstring( '
' '' @@ -527,11 +524,8 @@ def test_xml_pubmed_pub_date_pipe_without_date(self): '
' ) - xml_pubmed_pub_date_pipe(xml_pubmed, xml_tree) - - obtained = ET.tostring(xml_pubmed, encoding="utf-8").decode("utf-8") - - self.assertEqual(obtained, expected) + with self.assertRaises(MissingRequiredElementError): + xml_pubmed_pub_date_pipe(xml_pubmed, xml_tree) def test_xml_pubmed_article_title_pipe(self): expected = ( @@ -1828,6 +1822,43 @@ def test_xml_pubmed_other_abstract(self): self.assertEqual(obtained, expected) +class PipelinePubmedRequiredPubDate(unittest.TestCase): + def test_pipeline_pubmed_raises_when_no_pub_date_is_found(self): + xml_tree = ET.fromstring( + '
' + '' + '' + '' + '' + '
' + ) + + with self.assertRaises(MissingRequiredElementError): + pipeline_pubmed(xml_tree) + + def test_pipeline_pubmed_does_not_raise_when_pub_date_is_found(self): + xml_tree = ET.fromstring( + '
' + '' + '' + '' + '06' + '01' + '2023' + '' + '' + '' + '
' + ) + + xml_pubmed = pipeline_pubmed(xml_tree) + + self.assertIn("