Skip to content

Commit e356408

Browse files
authored
Merge pull request #1258 from effigies/enh/xml-kwargs
ENH: Permit XmlSerializable.to_xml() to pass kwargs to ElementTree.tostring()
2 parents adcc9ba + cea2f6c commit e356408

File tree

2 files changed

+19
-12
lines changed

2 files changed

+19
-12
lines changed

nibabel/gifti/gifti.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -852,7 +852,7 @@ def _to_xml_element(self):
852852
GIFTI.append(dar._to_xml_element())
853853
return GIFTI
854854

855-
def to_xml(self, enc='utf-8', *, mode='strict') -> bytes:
855+
def to_xml(self, enc='utf-8', *, mode='strict', **kwargs) -> bytes:
856856
"""Return XML corresponding to image content"""
857857
if mode == 'strict':
858858
if any(arr.datatype not in GIFTI_DTYPES for arr in self.darrays):
@@ -882,7 +882,7 @@ def to_xml(self, enc='utf-8', *, mode='strict') -> bytes:
882882
header = b"""<?xml version="1.0" encoding="UTF-8"?>
883883
<!DOCTYPE GIFTI SYSTEM "http://www.nitrc.org/frs/download.php/115/gifti.dtd">
884884
"""
885-
return header + super().to_xml(enc)
885+
return header + super().to_xml(enc, **kwargs)
886886

887887
# Avoid the indirection of going through to_file_map
888888
def to_bytes(self, enc='utf-8', *, mode='strict'):

nibabel/xmlutils.py

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,24 @@
1515

1616

1717
class XmlSerializable:
18-
"""Basic interface for serializing an object to xml"""
18+
"""Basic interface for serializing an object to XML"""
1919

20-
def _to_xml_element(self):
20+
def _to_xml_element(self) -> Element:
2121
"""Output should be a xml.etree.ElementTree.Element"""
22-
raise NotImplementedError()
22+
raise NotImplementedError # pragma: no cover
2323

24-
def to_xml(self, enc='utf-8'):
25-
"""Output should be an xml string with the given encoding.
26-
(default: utf-8)"""
24+
def to_xml(self, enc='utf-8', **kwargs) -> bytes:
25+
r"""Generate an XML bytestring with a given encoding.
26+
27+
Parameters
28+
----------
29+
enc : :class:`string`
30+
Encoding to use for the generated bytestring. Default: 'utf-8'
31+
\*\*kwargs : :class:`dict`
32+
Additional keyword arguments to :func:`xml.etree.ElementTree.tostring`.
33+
"""
2734
ele = self._to_xml_element()
28-
return '' if ele is None else tostring(ele, enc)
35+
return b'' if ele is None else tostring(ele, enc, **kwargs)
2936

3037

3138
class XmlBasedHeader(FileBasedHeader, XmlSerializable):
@@ -101,10 +108,10 @@ def parse(self, string=None, fname=None, fptr=None):
101108
parser.ParseFile(fptr)
102109

103110
def StartElementHandler(self, name, attrs):
104-
raise NotImplementedError
111+
raise NotImplementedError # pragma: no cover
105112

106113
def EndElementHandler(self, name):
107-
raise NotImplementedError
114+
raise NotImplementedError # pragma: no cover
108115

109116
def CharacterDataHandler(self, data):
110-
raise NotImplementedError
117+
raise NotImplementedError # pragma: no cover

0 commit comments

Comments
 (0)