From 77b58c1c5f956666816d9b40de746442c102c765 Mon Sep 17 00:00:00 2001 From: jevans Date: Sat, 25 Jan 2014 15:36:55 -0500 Subject: [PATCH] No explicit dependence on libxmp anymore. Back to ElementTree. --- glymur/_uuid_io/Exif.py | 14 ++++++++++++++ glymur/_uuid_io/__init__.py | 2 +- glymur/jp2box.py | 28 ++++++++++++---------------- glymur/test/test_jp2box_uuid.py | 7 ++++--- glymur/test/test_jp2k.py | 5 ++++- glymur/test/test_printing.py | 12 ++++-------- 6 files changed, 39 insertions(+), 29 deletions(-) diff --git a/glymur/_uuid_io/Exif.py b/glymur/_uuid_io/Exif.py index c1b30f0..967468d 100644 --- a/glymur/_uuid_io/Exif.py +++ b/glymur/_uuid_io/Exif.py @@ -6,6 +6,7 @@ import pprint import struct import sys import warnings +import xml.etree.cElementTree as ET if sys.hexversion < 0x02070000: # pylint: disable=F0401,E0611 @@ -13,8 +14,21 @@ if sys.hexversion < 0x02070000: else: from collections import OrderedDict +def xml(raw_data): + """ + XMP data to be parsed as XML. + """ + if sys.hexversion < 0x03000000: + elt = ET.fromstring(raw_data) + else: + text = raw_data.decode('utf-8') + elt = ET.fromstring(text) + + return ET.ElementTree(elt) + def tiff_header(read_buffer): """ + Interpret the uuid raw data as a tiff header. """ # Ignore the first six bytes. # Next 8 should be (73, 73, 42, 8) or (77, 77, 42, 8) diff --git a/glymur/_uuid_io/__init__.py b/glymur/_uuid_io/__init__.py index d12d20c..778f912 100644 --- a/glymur/_uuid_io/__init__.py +++ b/glymur/_uuid_io/__init__.py @@ -1,4 +1,4 @@ """ Sub package for handling various types of UUIDs. """ -from .Exif import tiff_header +from .Exif import tiff_header, xml diff --git a/glymur/jp2box.py b/glymur/jp2box.py index 300ac12..da8e1c9 100644 --- a/glymur/jp2box.py +++ b/glymur/jp2box.py @@ -33,12 +33,6 @@ else: import numpy as np -try: - from libxmp import XMPMeta - _HAS_PYTHON_XMP_TOOLKIT = True -except ImportError: - _HAS_PYTHON_XMP_TOOLKIT = False - from .codestream import Codestream from .core import _COLORSPACE_MAP_DISPLAY from .core import _COLOR_TYPE_MAP_DISPLAY @@ -2220,18 +2214,15 @@ class UUIDBox(Jp2kBox): try: self._parse_raw_data() - except Exception as e: - warnings.warn(str(e)) + except RuntimeError as error: + warnings.warn(str(error)) def _parse_raw_data(self): """ Private function for parsing UUID payloads if possible. """ if self.uuid == uuid.UUID('be7acfcb-97a9-42e8-9c71-999491e3afac'): - xmp = XMPMeta() - xmp.parse_from_str(self.raw_data.decode('utf-8'), - xmpmeta_wrap=False) - self.data = xmp + self.data = _uuid_io.xml(self.raw_data) elif self.uuid.bytes == b'JpgTiffExif->JP2': self.data = _uuid_io.tiff_header(self.raw_data) else: @@ -2243,11 +2234,16 @@ class UUIDBox(Jp2kBox): return msg.format(repr(self.uuid), len(self.data)) def __str__(self): - msg = '{0}\n' - msg += ' UUID: {1}\n' - msg += ' UUID Data: {2}' + msg = '{0}\n UUID: {1}'.format(Jp2kBox.__str__(self), self.uuid) - msg = msg.format(Jp2kBox.__str__(self), self.uuid, str(self.data)) + if self.uuid == uuid.UUID('be7acfcb-97a9-42e8-9c71-999491e3afac'): + line = ' (XMP)\n UUID Data: {0}' + msg += line.format(_pretty_print_xml(self.data)) + elif self.uuid.bytes == b'JpgTiffExif->JP2': + msg += ' (EXIF)\n UUID Data: {0}'.format(str(self.data)) + else: + line = ' (unknown)\n UUID Data: {0} bytes' + msg += line.format(len(self.raw_data)) return msg diff --git a/glymur/test/test_jp2box_uuid.py b/glymur/test/test_jp2box_uuid.py index 8f24f74..0d3684a 100644 --- a/glymur/test/test_jp2box_uuid.py +++ b/glymur/test/test_jp2box_uuid.py @@ -18,7 +18,7 @@ import sys import tempfile import uuid import warnings -from xml.etree import cElementTree as ET +import xml.etree if sys.hexversion < 0x02070000: import unittest2 as unittest @@ -68,7 +68,8 @@ class TestUUIDXMP(unittest.TestCase): # The data should be an XMP packet, which gets interpreted as # an ElementTree. - self.assertTrue(isinstance(jp2.box[-1].data, XMPMeta)) + self.assertTrue(isinstance(jp2.box[-1].data, + xml.etree.ElementTree.ElementTree)) class TestUUIDExif(unittest.TestCase): """Tests for UUIDs of Exif type.""" @@ -184,7 +185,7 @@ class TestUUIDExif(unittest.TestCase): tfile.flush() jp2 = glymur.Jp2k(tfile.name) - self.assertEqual(jp2.box[-1].data.ifds['Image']['Make'], "HTC") + self.assertEqual(jp2.box[-1].data['Make'], "HTC") if __name__ == "__main__": unittest.main() diff --git a/glymur/test/test_jp2k.py b/glymur/test/test_jp2k.py index c055aba..aeb0e41 100644 --- a/glymur/test/test_jp2k.py +++ b/glymur/test/test_jp2k.py @@ -31,6 +31,7 @@ import numpy as np import pkg_resources import libxmp +from libxmp import XMPMeta import glymur from glymur import Jp2k @@ -364,7 +365,9 @@ class TestJp2k(unittest.TestCase): def test_xmp_attribute(self): """Verify the XMP packet in the shipping example file can be read.""" j = Jp2k(self.jp2file) - xmp = j.box[3].data + xmp = XMPMeta() + xmp.parse_from_str(j.box[3].raw_data.decode('utf-8'), + xmpmeta_wrap=False) creator_tool = xmp.get_property(libxmp.consts.XMP_NS_XMP, 'CreatorTool') self.assertEqual(creator_tool, 'Google') diff --git a/glymur/test/test_printing.py b/glymur/test/test_printing.py index a1f0c55..382e28d 100644 --- a/glymur/test/test_printing.py +++ b/glymur/test/test_printing.py @@ -1057,6 +1057,8 @@ class TestPrinting(unittest.TestCase): tfile.write(struct.pack('