diff --git a/glymur/_uuid_io.py b/glymur/_uuid_io.py index ed610e5..399fa99 100644 --- a/glymur/_uuid_io.py +++ b/glymur/_uuid_io.py @@ -7,7 +7,8 @@ import re import struct import sys import warnings -import xml.etree.cElementTree as ET + +import lxml.etree as ET if sys.hexversion < 0x02070000: # pylint: disable=F0401,E0611 diff --git a/glymur/core.py b/glymur/core.py index 620d0e3..30282f2 100644 --- a/glymur/core.py +++ b/glymur/core.py @@ -1,7 +1,7 @@ """Core definitions to be shared amongst the modules. """ import copy -import xml.etree.cElementTree as ET +import lxml.etree as ET # Progression order LRCP = 0 @@ -81,9 +81,9 @@ _CAPABILITIES_DISPLAY = { def _pretty_print_xml(xml, level=0): """Pretty print XML data. """ - xml = copy.deepcopy(xml) - _indent(xml.getroot(), level=level) - xmltext = ET.tostring(xml.getroot(), encoding='utf-8').decode('utf-8') + #xml = copy.deepcopy(xml) + #_indent(xml.getroot(), level=level) + xmltext = ET.tostring(xml, encoding='utf-8').decode('utf-8') # Indent it a bit. lst = [(' ' + x) for x in xmltext.split('\n')] diff --git a/glymur/jp2box.py b/glymur/jp2box.py index 3cf637d..192763f 100644 --- a/glymur/jp2box.py +++ b/glymur/jp2box.py @@ -22,8 +22,8 @@ import struct import sys import uuid import warnings -import xml.etree.cElementTree as ET +import lxml.etree as ET import numpy as np from .codestream import Codestream @@ -2565,7 +2565,10 @@ class XMLBox(Jp2kBox): xml = self.xml if self.xml is not None: - msg += _pretty_print_xml(self.xml) + bstr = ET.tostring(self.xml, + encoding='utf-8', + pretty_print=True).decode('utf-8') + msg += '\n {0}'.format(bstr) else: msg += '\n {0}'.format(xml) return msg @@ -2971,8 +2974,10 @@ class UUIDBox(Jp2kBox): return msg if self.uuid == uuid.UUID('be7acfcb-97a9-42e8-9c71-999491e3afac'): - line = '\n UUID Data: {0}' - xmlstring = _pretty_print_xml(self.data) + line = '\n UUID Data:\n{0}' + xmlstring = ET.tostring(self.data, + encoding='utf-8', + pretty_print=True).decode('utf-8') xmlstring = xmlstring.rstrip() msg += line.format(xmlstring) elif self.uuid.bytes == b'JpgTiffExif->JP2': diff --git a/glymur/test/fixtures.py b/glymur/test/fixtures.py index a7906c5..e7c81a3 100644 --- a/glymur/test/fixtures.py +++ b/glymur/test/fixtures.py @@ -183,90 +183,92 @@ def read_pgx_header(pgx_file): nemo_xmp_box = """UUID Box (uuid) @ (77, 3146) UUID: be7acfcb-97a9-42e8-9c71-999491e3afac (XMP) - UUID Data: - - - - Google - 2013-02-09T14:47:53 - - - 1 - 72/1 - 72/1 - 2 - HTC - HTC Glacier - 2592 - 1456 - - - 8 - 8 - 8 - - - 2 - 3 - - - 1343036288/4294967295 - 1413044224/4294967295 - - - - - 2748779008/4294967295 - 1417339264/4294967295 - 1288490240/4294967295 - 2576980480/4294967295 - 644245120/4294967295 - 257698032/4294967295 - - - - - 1 - 2528 - 1424 - 353/100 - 0 - 0/1 - WGS-84 - 2013-02-09T14:47:53 - - - 76 - - - 0220 - 0100 - - - 1 - 2 - 3 - 0 - - - 42,20.56N - 71,5.29W - 2013-02-09T19:47:53Z - NETWORK - - - 2013-02-09T14:47:53 - - - - - Glymur - Python XMP Toolkit - - - - - """ + UUID Data: + + + + + Google + 2013-02-09T14:47:53 + + + 1 + 72/1 + 72/1 + 2 + HTC + HTC Glacier + 2592 + 1456 + + + 8 + 8 + 8 + + + 2 + 3 + + + 1343036288/4294967295 + 1413044224/4294967295 + + + + + 2748779008/4294967295 + 1417339264/4294967295 + 1288490240/4294967295 + 2576980480/4294967295 + 644245120/4294967295 + 257698032/4294967295 + + + + + 1 + 2528 + 1424 + 353/100 + 0 + 0/1 + WGS-84 + 2013-02-09T14:47:53 + + + 76 + + + 0220 + 0100 + + + 1 + 2 + 3 + 0 + + + 42,20.56N + 71,5.29W + 2013-02-09T19:47:53Z + NETWORK + + + 2013-02-09T14:47:53 + + + + + Glymur + Python XMP Toolkit + + + + + +""" SimpleRDF = """ - - - Google - 2013-02-09T14:47:53 - - - 1 - 72/1 - 72/1 - 2 - HTC - HTC Glacier - 2592 - 1456 - - - 8 - 8 - 8 - - - 2 - 3 - - - 1343036288/4294967295 - 1413044224/4294967295 - - - - - 2748779008/4294967295 - 1417339264/4294967295 - 1288490240/4294967295 - 2576980480/4294967295 - 644245120/4294967295 - 257698032/4294967295 - - - - - 1 - 2528 - 1424 - 353/100 - 0 - 0/1 - WGS-84 - 2013-02-09T14:47:53 - - - 76 - - - 0220 - 0100 - - - 1 - 2 - 3 - 0 - - - 42,20.56N - 71,5.29W - 2013-02-09T19:47:53Z - NETWORK - - - 2013-02-09T14:47:53 - - - - - Glymur - Python XMP Toolkit - - - - - + UUID Data: + + + + + Google + 2013-02-09T14:47:53 + + + 1 + 72/1 + 72/1 + 2 + HTC + HTC Glacier + 2592 + 1456 + + + 8 + 8 + 8 + + + 2 + 3 + + + 1343036288/4294967295 + 1413044224/4294967295 + + + + + 2748779008/4294967295 + 1417339264/4294967295 + 1288490240/4294967295 + 2576980480/4294967295 + 644245120/4294967295 + 257698032/4294967295 + + + + + 1 + 2528 + 1424 + 353/100 + 0 + 0/1 + WGS-84 + 2013-02-09T14:47:53 + + + 76 + + + 0220 + 0100 + + + 1 + 2 + 3 + 0 + + + 42,20.56N + 71,5.29W + 2013-02-09T19:47:53Z + NETWORK + + + 2013-02-09T14:47:53 + + + + + Glymur + Python XMP Toolkit + + + + + + Contiguous Codestream Box (jp2c) @ (3223, 1132296) Main header: SOC marker segment @ (3231, 0) @@ -575,90 +579,92 @@ JP2 Header Box (jp2h) @ (32, 45) Colorspace: sRGB UUID Box (uuid) @ (77, 3146) UUID: be7acfcb-97a9-42e8-9c71-999491e3afac (XMP) - UUID Data: - - - - Google - 2013-02-09T14:47:53 - - - 1 - 72/1 - 72/1 - 2 - HTC - HTC Glacier - 2592 - 1456 - - - 8 - 8 - 8 - - - 2 - 3 - - - 1343036288/4294967295 - 1413044224/4294967295 - - - - - 2748779008/4294967295 - 1417339264/4294967295 - 1288490240/4294967295 - 2576980480/4294967295 - 644245120/4294967295 - 257698032/4294967295 - - - - - 1 - 2528 - 1424 - 353/100 - 0 - 0/1 - WGS-84 - 2013-02-09T14:47:53 - - - 76 - - - 0220 - 0100 - - - 1 - 2 - 3 - 0 - - - 42,20.56N - 71,5.29W - 2013-02-09T19:47:53Z - NETWORK - - - 2013-02-09T14:47:53 - - - - - Glymur - Python XMP Toolkit - - - - - + UUID Data: + + + + + Google + 2013-02-09T14:47:53 + + + 1 + 72/1 + 72/1 + 2 + HTC + HTC Glacier + 2592 + 1456 + + + 8 + 8 + 8 + + + 2 + 3 + + + 1343036288/4294967295 + 1413044224/4294967295 + + + + + 2748779008/4294967295 + 1417339264/4294967295 + 1288490240/4294967295 + 2576980480/4294967295 + 644245120/4294967295 + 257698032/4294967295 + + + + + 1 + 2528 + 1424 + 353/100 + 0 + 0/1 + WGS-84 + 2013-02-09T14:47:53 + + + 76 + + + 0220 + 0100 + + + 1 + 2 + 3 + 0 + + + 42,20.56N + 71,5.29W + 2013-02-09T19:47:53Z + NETWORK + + + 2013-02-09T14:47:53 + + + + + Glymur + Python XMP Toolkit + + + + + + Contiguous Codestream Box (jp2c) @ (3223, 1132296)""" nemo_dump_no_codestream_no_xml = r"""JPEG 2000 Signature Box (jP ) @ (0, 12) @@ -691,10 +697,10 @@ file7_rreq = r"""Reader Requirements Box (rreq) @ (44, 24) Feature 043: 0x40 Deprecated - compositing layer uses restricted ICC profile Vendor Features:""" -file1_xml = r"""XML Box (xml ) @ (36, 439) - - - 2001-11-01T13:45:00.000-06:00 - Professional 120 Image - - """ +file1_xml = """XML Box (xml ) @ (36, 439) + +\t +\t\t2001-11-01T13:45:00.000-06:00 +\t\tProfessional 120 Image +\t +""" diff --git a/glymur/test/test_jp2box.py b/glymur/test/test_jp2box.py index 5861971..5dedbcc 100644 --- a/glymur/test/test_jp2box.py +++ b/glymur/test/test_jp2box.py @@ -22,10 +22,10 @@ import sys import tempfile import uuid from uuid import UUID -import xml.etree.cElementTree as ET import unittest import warnings +import lxml.etree as ET import numpy as np import glymur @@ -1056,7 +1056,7 @@ class TestRepr(unittest.TestCase): box = glymur.jp2box.XMLBox(xml=tree) regexp = r"""glymur.jp2box.XMLBox""" - regexp += r"""\(xml=<(xml.etree.ElementTree.){0,1}ElementTree object """ + regexp += r"""\(xml=\)""" if sys.hexversion < 0x03000000: diff --git a/glymur/test/test_jp2box_jpx.py b/glymur/test/test_jp2box_jpx.py index 7493dea..f8e9db8 100644 --- a/glymur/test/test_jp2box_jpx.py +++ b/glymur/test/test_jp2box_jpx.py @@ -8,7 +8,7 @@ import struct import sys import tempfile import unittest -import xml.etree.cElementTree as ET +import lxml.etree as ET import glymur from glymur import Jp2k diff --git a/glymur/test/test_jp2box_uuid.py b/glymur/test/test_jp2box_uuid.py index 82dc253..a904174 100644 --- a/glymur/test/test_jp2box_uuid.py +++ b/glymur/test/test_jp2box_uuid.py @@ -18,7 +18,6 @@ import sys import tempfile import uuid import warnings -import xml.etree if sys.hexversion < 0x02070000: import unittest2 as unittest @@ -35,6 +34,8 @@ if sys.hexversion <= 0x03030000: else: from unittest.mock import patch +import lxml.etree + from .fixtures import HAS_PYTHON_XMP_TOOLKIT, OPJ_DATA_ROOT if HAS_PYTHON_XMP_TOOLKIT: from libxmp import XMPMeta @@ -71,7 +72,7 @@ class TestUUIDXMP(unittest.TestCase): # The data should be an XMP packet, which gets interpreted as # an ElementTree. self.assertTrue(isinstance(jp2.box[-1].data, - xml.etree.ElementTree.ElementTree)) + lxml.etree._ElementTree)) class TestUUIDExif(unittest.TestCase): """Tests for UUIDs of Exif type.""" diff --git a/glymur/test/test_jp2box_xml.py b/glymur/test/test_jp2box_xml.py index 4950da4..facc4df 100644 --- a/glymur/test/test_jp2box_xml.py +++ b/glymur/test/test_jp2box_xml.py @@ -20,7 +20,6 @@ import sys import tempfile import unittest import warnings -import xml.etree.cElementTree as ET if sys.hexversion < 0x03000000: from StringIO import StringIO @@ -32,6 +31,8 @@ if sys.hexversion <= 0x03030000: else: from unittest.mock import patch +import lxml.etree as ET + import glymur from glymur import Jp2k from glymur.jp2box import ColourSpecificationBox, ContiguousCodestreamBox diff --git a/glymur/test/test_printing.py b/glymur/test/test_printing.py index 132d1ec..45b21df 100644 --- a/glymur/test/test_printing.py +++ b/glymur/test/test_printing.py @@ -15,7 +15,6 @@ import struct import sys import tempfile import warnings -from xml.etree import cElementTree as ET import unittest if sys.hexversion < 0x03000000: @@ -28,10 +27,12 @@ if sys.hexversion <= 0x03030000: else: from unittest.mock import patch +import lxml.etree as ET + import glymur from glymur import Jp2k from . import fixtures -from .fixtures import OPJ_DATA_ROOT, opj_data_file, nemo_xmp_box +from .fixtures import OPJ_DATA_ROOT, opj_data_file from .fixtures import text_gbr_27, text_gbr_33, text_gbr_34 @@ -118,6 +119,7 @@ class TestPrinting(unittest.TestCase): lst = actual.split('\n') lst = lst[1:] actual = '\n'.join(lst) + self.maxDiff = None self.assertEqual(actual, fixtures.nemo_dump_no_codestream) def test_printoptions_no_xml(self): @@ -209,6 +211,7 @@ class TestPrinting(unittest.TestCase): lst = actual.split('\n') lst = lst[1:] actual = '\n'.join(lst) + self.maxDiff = None self.assertEqual(actual, fixtures.nemo_dump_full) def test_entire_file(self): @@ -567,7 +570,7 @@ class TestPrinting(unittest.TestCase): print(j.box[3]) actual = fake_out.getvalue().strip() - expected = nemo_xmp_box + expected = fixtures.nemo_xmp_box self.assertEqual(actual, expected) def test_codestream(self): @@ -639,8 +642,7 @@ class TestPrinting(unittest.TestCase): # # 2.7.5 (fedora 19) prints xml entities. # 2.7.3 seems to want to print hex escapes. - text = u""" - Strömung""" + text = u"""Strömung""" if sys.hexversion < 0x03000000: xml = ET.parse(StringIO(text.encode('utf-8'))) else: @@ -668,8 +670,7 @@ class TestPrinting(unittest.TestCase): # # 2.7.5 (fedora 19) prints xml entities. # 2.7.3 seems to want to print hex escapes. - text = u""" - Россия""" + text = u"""Россия""" if sys.hexversion < 0x03000000: xml = ET.parse(StringIO(text.encode('utf-8'))) else: