Skip to content
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
42 changes: 28 additions & 14 deletions packtools/sps/formats/pubmed.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 <Article> 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")

Expand Down Expand Up @@ -127,20 +140,21 @@ def xml_pubmed_pub_date_pipe(xml_pubmed, xml_tree):
</PubDate>
"""
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):
Expand Down
8 changes: 7 additions & 1 deletion packtools/sps/formats/pubmed_generator.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import argparse
import sys

from packtools.sps.formats import pubmed
from packtools.sps.utils import xml_utils
Expand Down Expand Up @@ -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}")
Expand Down
51 changes: 41 additions & 10 deletions tests/sps/formats/test_pubmed.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
xml_pubmed_citations,
xml_pubmed_abstract,
xml_pubmed_other_abstract,
pipeline_pubmed,
MissingRequiredElementError,
)


Expand Down Expand Up @@ -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 = (
'<Article>'
'<Journal/>'
'</Article>'
)
xml_pubmed = ET.fromstring(
'<Article>'
'<Journal/>'
Expand All @@ -527,11 +524,8 @@ def test_xml_pubmed_pub_date_pipe_without_date(self):
'</article>'
)

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 = (
Expand Down Expand Up @@ -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(
'<article xmlns:mml="http://www.w3.org/1998/Math/MathML" '
'xmlns:xlink="http://www.w3.org/1999/xlink" '
'article-type="research-article" dtd-version="1.1" specific-use="sps-1.9" xml:lang="en">'
'<front>'
'<article-meta>'
'</article-meta>'
'</front>'
'</article>'
)

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(
'<article xmlns:mml="http://www.w3.org/1998/Math/MathML" '
'xmlns:xlink="http://www.w3.org/1999/xlink" '
'article-type="research-article" dtd-version="1.1" specific-use="sps-1.9" xml:lang="en">'
'<front>'
'<article-meta>'
'<pub-date publication-format="electronic" date-type="pub">'
'<day>06</day>'
'<month>01</month>'
'<year>2023</year>'
'</pub-date>'
'</article-meta>'
'</front>'
'</article>'
)

xml_pubmed = pipeline_pubmed(xml_tree)

self.assertIn("<PubDate", xml_pubmed)


if __name__ == '__main__':
unittest.main()