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

Feat: build fig element #748

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
64 changes: 64 additions & 0 deletions packtools/sps/formats/sps_xml/fig.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
"""
data = {
"fig-id": "f01",
"fig-type": "map",
"label": "Figure 1",
"caption-title": "Título de figura",
"caption-p": ["Deaths Among Patients..."],
"graphic-xlink": "1234-5678-zwy-12-04-0123-gf02.tif",
"attrib": "Fonte: IBGE (2018)"
}
"""

import xml.etree.ElementTree as ET


def build_fig(data):
fig_id = data.get("fig-id")
if not fig_id:
raise ValueError("Attrib id is required")

attrib = {"id": fig_id}

fig_type = data.get("fig-type")
if fig_type:
attrib["fig-type"] = fig_type

# build fig
fig_elem = ET.Element("fig", attrib=attrib)

# add label
if label := data.get("label"):
label_elem = ET.Element("label")
label_elem.text = label
fig_elem.append(label_elem)

# add caption
caption = data.get("caption-title")
paragraphs = data.get("caption-p")
if caption or paragraphs:
caption_elem = ET.Element("caption")
if caption:
# testando SubElement
title_elem = ET.SubElement(caption_elem, "title")
title_elem.text = caption

for paragraph in (paragraphs or []):
# testando SubElement
paragraph_elem = ET.SubElement(caption_elem, "p")
paragraph_elem.text = paragraph

fig_elem.append(caption_elem)

# add xlink
if xlink := data.get("graphic-xlink"):
xlink_elem = ET.Element("graphic", attrib={"xlink:href": xlink})
fig_elem.append(xlink_elem)

# add attrib
if attrib := data.get("attrib"):
attrib_elem = ET.Element("attrib")
attrib_elem.text = attrib
fig_elem.append(attrib_elem)

return fig_elem
97 changes: 97 additions & 0 deletions tests/sps/formats/sps_xml/test_fig.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
import unittest
import xml.etree.ElementTree as ET
from packtools.sps.formats.sps_xml.fig import build_fig


class TestBuildFig(unittest.TestCase):

def test_build_fig_without_id(self):
data = {
"fig-id": None,
}
with self.assertRaises(ValueError) as e:
build_fig(data)
self.assertEqual(str(e.exception), "Attrib id is required")


class TestBuildFigType(unittest.TestCase):

def test_build_fig_type(self):
data = {"fig-id": "f01", "fig-type": "map"}
expected_xml_str = '<fig id="f01" fig-type="map" />'
fig_elem = build_fig(data)
generated_xml_str = ET.tostring(fig_elem, encoding="unicode", method="xml")
self.assertEqual(generated_xml_str.strip(), expected_xml_str.strip())


class TestBuildFigLabel(unittest.TestCase):

def test_build_fig_label(self):
data = {"fig-id": "f01", "fig-type": "map", "label": "Figure 1"}
expected_xml_str = (
'<fig id="f01" fig-type="map">' "<label>Figure 1</label>" "</fig>"
)
fig_elem = build_fig(data)
generated_xml_str = ET.tostring(fig_elem, encoding="unicode", method="xml")
self.assertEqual(generated_xml_str.strip(), expected_xml_str.strip())


class TestBuildFigCaption(unittest.TestCase):

def test_build_fig_caption_without_title(self):
data = {"fig-id": "f01", "caption-p": ["Deaths Among Patients..."]}
expected_xml_str = (
'<fig id="f01">'
"<caption>"
"<p>Deaths Among Patients...</p>"
"</caption>"
"</fig>"
)
fig_elem = build_fig(data)
generated_xml_str = ET.tostring(fig_elem, encoding="unicode", method="xml")
self.assertEqual(generated_xml_str.strip(), expected_xml_str.strip())

def test_build_fig_caption_without_paragraphs(self):
data = {
"fig-id": "f01",
"caption-title": "Título de figura",
}
expected_xml_str = (
'<fig id="f01">'
"<caption>"
"<title>Título de figura</title>"
"</caption>"
"</fig>"
)
fig_elem = build_fig(data)
generated_xml_str = ET.tostring(fig_elem, encoding="unicode", method="xml")
self.assertEqual(generated_xml_str.strip(), expected_xml_str.strip())


class TestBuildFigXlink(unittest.TestCase):

def test_build_fig_xlink(self):
data = {
"fig-id": "f01",
"graphic-xlink": "1234-5678-zwy-12-04-0123-gf02.tif",
}
expected_xml_str = (
'<fig id="f01">'
'<graphic xlink:href="1234-5678-zwy-12-04-0123-gf02.tif" />'
"</fig>"
)
fig_elem = build_fig(data)
generated_xml_str = ET.tostring(fig_elem, encoding="unicode", method="xml")
self.assertEqual(generated_xml_str.strip(), expected_xml_str.strip())


class TestBuildFigAttrib(unittest.TestCase):

def test_build_fig_attrib(self):
data = {"fig-id": "f01", "attrib": "Fonte: IBGE (2018)"}
expected_xml_str = (
'<fig id="f01">' "<attrib>Fonte: IBGE (2018)</attrib>" "</fig>"
)
fig_elem = build_fig(data)
generated_xml_str = ET.tostring(fig_elem, encoding="unicode", method="xml")
self.assertEqual(generated_xml_str.strip(), expected_xml_str.strip())