Skip to content

Commit 55f4f1d

Browse files
author
Ben Cipollini
committed
RF: add thin layer around xml.etree.ElementTree and share XmlSerializable
1 parent 926976a commit 55f4f1d

File tree

2 files changed

+34
-21
lines changed

2 files changed

+34
-21
lines changed

nibabel/gifti/gifti.py

Lines changed: 9 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@
99
from __future__ import division, print_function, absolute_import
1010

1111
import sys
12-
import xml.etree.ElementTree as xml
1312

1413
import numpy as np
1514

15+
from .. import xmlutils as xml
1616
from ..nifti1 import data_type_codes, xform_codes, intent_codes
1717
from .util import (array_index_order_codes, gifti_encoding_codes,
1818
gifti_endian_codes, KIND2FMT)
@@ -23,19 +23,7 @@
2323
import base64
2424

2525

26-
class XmlSerializable(object):
27-
""" Basic interface for serializing an object to xml"""
28-
def _to_xml_element(self):
29-
""" Output should be a xml.etree.ElementTree.Element"""
30-
raise NotImplementedError()
31-
32-
def to_xml(self, enc='utf-8'):
33-
""" Output should be an xml string with the given encoding.
34-
(default: utf-8)"""
35-
return xml.tostring(self._to_xml_element(), enc)
36-
37-
38-
class GiftiMetaData(XmlSerializable):
26+
class GiftiMetaData(xml.XmlSerializable):
3927
""" A list of GiftiNVPairs in stored in
4028
the list self.data """
4129
def __init__(self, nvpair=None):
@@ -87,7 +75,7 @@ def __init__(self, name='', value=''):
8775
self.value = value
8876

8977

90-
class GiftiLabelTable(XmlSerializable):
78+
class GiftiLabelTable(xml.XmlSerializable):
9179

9280
def __init__(self):
9381
self.labels = []
@@ -113,7 +101,7 @@ def print_summary(self):
113101
print(self.get_labels_as_dict())
114102

115103

116-
class GiftiLabel(XmlSerializable):
104+
class GiftiLabel(xml.XmlSerializable):
117105
key = int
118106
label = str
119107
# rgba
@@ -166,7 +154,7 @@ def _arr2txt(arr, elem_fmt):
166154
return '\n'.join(fmt % tuple(row) for row in arr)
167155

168156

169-
class GiftiCoordSystem(XmlSerializable):
157+
class GiftiCoordSystem(xml.XmlSerializable):
170158
dataspace = int
171159
xformspace = int
172160
xform = np.ndarray # 4x4 numpy array
@@ -199,7 +187,7 @@ def print_summary(self):
199187

200188
def _data_tag_element(dataarray, encoding, datatype, ordering):
201189
""" Creates the data tag depending on the required encoding,
202-
returns as bytes"""
190+
returns as XML element"""
203191
import zlib
204192
ord = array_index_order_codes.npcode[ordering]
205193
enclabel = gifti_encoding_codes.label[encoding]
@@ -225,7 +213,7 @@ def data_tag(dataarray, encoding, datatype, ordering):
225213
return xml.tostring(data, 'utf-8')
226214

227215

228-
class GiftiDataArray(XmlSerializable):
216+
class GiftiDataArray(xml.XmlSerializable):
229217

230218
# These are for documentation only; we don't use these class variables
231219
intent = int
@@ -362,7 +350,7 @@ def metadata(self):
362350
return self.meta.metadata
363351

364352

365-
class GiftiImage(XmlSerializable):
353+
class GiftiImage(xml.XmlSerializable):
366354

367355
def __init__(self, meta=None, labeltable=None, darrays=None,
368356
version="1.0"):
@@ -506,4 +494,4 @@ def to_xml(self, enc='utf-8'):
506494
""" Return XML corresponding to image content """
507495
return b"""<?xml version="1.0" encoding="UTF-8"?>
508496
<!DOCTYPE GIFTI SYSTEM "http://www.nitrc.org/frs/download.php/115/gifti.dtd">
509-
""" + XmlSerializable.to_xml(self, enc)
497+
""" + xml.XmlSerializable.to_xml(self, enc)

nibabel/xmlutils.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# emacs: -*- mode: python-mode; py-indent-offset: 4; indent-tabs-mode: nil -*-
2+
# vi: set ft=python sts=4 ts=4 sw=4 et:
3+
### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ##
4+
#
5+
# See COPYING file distributed along with the NiBabel package for the
6+
# copyright and license terms.
7+
#
8+
### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ##
9+
"""
10+
Thin layer around xml.etree.ElementTree, to abstract nibabel xml support.
11+
"""
12+
from xml.etree.ElementTree import Element, SubElement, tostring
13+
14+
15+
class XmlSerializable(object):
16+
""" Basic interface for serializing an object to xml"""
17+
18+
def _to_xml_element(self):
19+
""" Output should be a xml.etree.ElementTree.Element"""
20+
raise NotImplementedError()
21+
22+
def to_xml(self, enc='utf-8'):
23+
""" Output should be an xml string with the given encoding.
24+
(default: utf-8)"""
25+
return tostring(self._to_xml_element(), enc)

0 commit comments

Comments
 (0)