No explicit dependence on libxmp anymore. Back to ElementTree.

This commit is contained in:
jevans 2014-01-25 15:36:55 -05:00
commit 77b58c1c5f
6 changed files with 39 additions and 29 deletions

View file

@ -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)

View file

@ -1,4 +1,4 @@
"""
Sub package for handling various types of UUIDs.
"""
from .Exif import tiff_header
from .Exif import tiff_header, xml

View file

@ -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

View file

@ -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()

View file

@ -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')

View file

@ -1057,6 +1057,8 @@ class TestPrinting(unittest.TestCase):
tfile.write(struct.pack('<HHI4s', 271, 2, 3, b'HTC\x00'))
tfile.flush()
import shutil
shutil.copyfile(tfile.name, '/Users/jevans/a.jp2')
j = glymur.Jp2k(tfile.name)
with patch('sys.stdout', new=StringIO()) as fake_out:
@ -1064,14 +1066,8 @@ class TestPrinting(unittest.TestCase):
actual = fake_out.getvalue().strip()
lines = ["UUID Box (uuid) @ (1135519, 76)",
" UUID: 4a706754-6966-6645-7869-662d3e4a5032 (Exif)",
" UUID Data: ",
"{'Image': {'ImageWidth': 256,",
" 'ImageLength': 512,",
" 'Make': 'HTC'},",
" 'Photo': None,",
" 'GPSInfo': None,",
" 'Iop': None}"]
" UUID: 4a706754-6966-6645-7869-662d3e4a5032 (EXIF)",
" UUID Data: OrderedDict([('ImageWidth', 256), ('ImageLength', 512), ('Make', 'HTC')])"]
expected = '\n'.join(lines)