diff --git a/CHANGELOG b/CHANGELOG index c48e5d1..ea38b7c 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,4 +1,10 @@ -Release 2.0.0 ( TBD ) +Release 2.0.x ( March 21, 2016 ) + * Qualify on Python 3.5, exempi 2.3.0 + +Release 2.0.1 ( March 9, 2014 ) + * Fix file_to_dict import + +Release 2.0.0 ( Jan 17, 2014 ) * Added support for Python 3.3, dropped support for 2.5. * All string outputs in 2.6 and 2.7 are unicode objects with UTF-8 encoding. * Added exempi module, a low-level ctypes interface to exempi library. diff --git a/libxmp/exempi.py b/libxmp/exempi.py index 51345e7..0d39d3f 100644 --- a/libxmp/exempi.py +++ b/libxmp/exempi.py @@ -35,7 +35,8 @@ """ Wrapper functions for individual exempi library routines. """ -import ctypes, ctypes.util +import ctypes +import ctypes.util import datetime import os import platform @@ -45,6 +46,7 @@ from . import XMPError, ExempiLoadError from .consts import XMP_OPEN_READ, XMP_OPEN_NOOPTION + def _load_exempi(): """ Loads exempi library. @@ -55,7 +57,7 @@ def _load_exempi(): if os.path.exists('/opt/local/lib/libexempi.dylib'): # MacPorts starndard location. path = '/opt/local/lib/libexempi.dylib' - + if path is None: raise ExempiLoadError('Exempi library not found.') @@ -68,44 +70,51 @@ def _load_exempi(): EXEMPI = _load_exempi() +if hasattr(EXEMPI, 'xmp_files_get_xmp_xmpstring'): + _libexempi_version = '2.3' +else: + _libexempi_version = '2.2' + # Error codes defined by libexempi. See "xmperrors.h" -ERROR_MESSAGE = { 0: "unknown error", - -1: "TBD", - -2: "unavailable", - -3: "bad object", - -4: "bad parameter", - -5: "bad value", - -6: "assert failure", - -7: "enforce failure", - -8: "unimplemented", - -9: "internal failure", - -10: "deprecated", - -11: "external failure", - -12: "user abort", - -13: "std exception", - -14: "unknown exception", - -15: "no memory", - -101: "bad schema", - -102: "bad XPath", - -103: "bad options", - -104: "bad index", - -105: "bad iter position", - -106: "bad parse", - -107: "bad serialize", - -108: "bad file format", - -109: "no file handler", - -110: "too large for JPEG", - -201: "bad XML", - -202: "bad RDF", - -203: "bad XMP", - -204: "empty iterator", - -205: "bad unicode", - -206: "bad TIFF", - -207: "bad JPEG", - -208: "bad PSD", - -209: "bad PSIR", - -210: "bad IPTC", - -211: "bad MPEG" } +ERROR_MESSAGE = { + 0: "unknown error", + -1: "TBD", + -2: "unavailable", + -3: "bad object", + -4: "bad parameter", + -5: "bad value", + -6: "assert failure", + -7: "enforce failure", + -8: "unimplemented", + -9: "internal failure", + -10: "deprecated", + -11: "external failure", + -12: "user abort", + -13: "std exception", + -14: "unknown exception", + -15: "no memory", + -101: "bad schema", + -102: "bad XPath", + -103: "bad options", + -104: "bad index", + -105: "bad iter position", + -106: "bad parse", + -107: "bad serialize", + -108: "bad file format", + -109: "no file handler", + -110: "too large for JPEG", + -201: "bad XML", + -202: "bad RDF", + -203: "bad XMP", + -204: "empty iterator", + -205: "bad unicode", + -206: "bad TIFF", + -207: "bad JPEG", + -208: "bad PSD", + -209: "bad PSIR", + -210: "bad IPTC", + -211: "bad MPEG" +} class XmpDateTime(ctypes.Structure): @@ -185,6 +194,21 @@ def copy(xmp): return newxmp +class PacketInfoType(ctypes.Structure): + """ + Corresponds to XmpPacketInfo type. + """ + _fields_ = [ + ("offset", ctypes.c_int64), + ("length", ctypes.c_int32), + ("pad_size", ctypes.c_int32), + ("char_form", ctypes.c_uint8), + ("writeable", ctypes.c_bool), + ("has_wrapper", ctypes.c_bool), + ("pad", ctypes.c_uint8), + ] + + def files_can_put_xmp(xfptr, xmp): """Wrapper for xmp_files_can_put_xmp library routine. @@ -206,23 +230,6 @@ def files_can_put_xmp(xfptr, xmp): return value == 1 -def files_check_file_format(filename): - """Check the file format of a file. - - Wrapper for xmp_check_file_format library routine. - - Parameters - ---------- - filename : str - Path to file. - """ - if not os.path.exists(filename): - raise IOError("{0} does not exist.".format(filename)) - EXEMPI.xmp_files_check_file_format.restype = ctypes.c_int32 - EXEMPI.xmp_files_check_file_format.argtypes = [ctypes.c_char_p] - return EXEMPI.xmp_files_check_file_format(filename.encode('utf-8')) - - def delete_localized_text(xmp, schema, name, generic_lang, specific_lang): """Remove a property. @@ -283,7 +290,26 @@ def delete_property(xmp, schema, name): EXEMPI.xmp_delete_property.argtypes = [ctypes.c_void_p, ctypes.c_char_p, ctypes.c_char_p] - EXEMPI.xmp_delete_property(xmp, schema.encode('utf-8'), name.encode('utf-8')) + EXEMPI.xmp_delete_property(xmp, + schema.encode('utf-8'), + name.encode('utf-8')) + + +def files_check_file_format(filename): + """Check the file format of a file. + + Wrapper for xmp_check_file_format library routine. + + Parameters + ---------- + filename : str + Path to file. + """ + if not os.path.exists(filename): + raise IOError("{0} does not exist.".format(filename)) + EXEMPI.xmp_files_check_file_format.restype = ctypes.c_int32 + EXEMPI.xmp_files_check_file_format.argtypes = [ctypes.c_char_p] + return EXEMPI.xmp_files_check_file_format(filename.encode('utf-8')) def files_close(xfptr, options): @@ -419,6 +445,28 @@ def files_get_xmp(xfptr): return xmp +def files_get_xmp_xmpstring(xfptr, xmp): + """ + Returns + ------- + packet_info : + """ + if _libexempi_version.startswith('2.2'): + message = 'This method requires exempi version 2.3 or higher.' + raise NotImplementedError(message) + + EXEMPI.xmp_files_get_xmp_xmpstring.restype = check_error + EXEMPI.xmp_files_get_xmp_xmpstring.argtypes = [ + ctypes.c_void_p, + ctypes.c_void_p, + ctypes.POINTER(PacketInfoType) + ] + + packet_info = PacketInfoType() + EXEMPI.xmp_files_get_xmp_xmpstring(xfptr, xmp, ctypes.byref(packet_info)) + return packet_info + + def files_open(xfptr, filename, options): """Wrapper for xmp_files_open library routine. @@ -551,6 +599,7 @@ def get_array_item(xmp, schema, name, index): return item, property_bits.value + def get_localized_text(xmp, schema, name, generic_lang, specific_lang): """Get a localised text from a localisable property. @@ -755,10 +804,13 @@ def get_property_date(xmp, schema, name): minutes=xmp_date_time.tzminute, microseconds=xmp_date_time.nanosecond * 1000) + # If the time being described is one hour ahead of UTC, the offset is + # typically given as "+01". So subtract to get UTC. + # pla if xmp_date_time.tzsign < 0: - the_date = utc_date - delta - else: the_date = utc_date + delta + else: + the_date = utc_date - delta return the_date, prop_bits.value @@ -788,11 +840,13 @@ def get_property_int32(xmp, schema, name): """ # Use a function callback instead of returning a boolean value. EXEMPI.xmp_get_property_int32.restype = check_error - EXEMPI.xmp_get_property_int32.argtypes = [ctypes.c_void_p, - ctypes.c_char_p, - ctypes.c_char_p, - ctypes.POINTER(ctypes.c_int32), - ctypes.POINTER(ctypes.c_uint32)] + EXEMPI.xmp_get_property_int32.argtypes = [ + ctypes.c_void_p, + ctypes.c_char_p, + ctypes.c_char_p, + ctypes.POINTER(ctypes.c_int32), + ctypes.POINTER(ctypes.c_uint32) + ] ivalue = ctypes.c_int32(0) prop_bits = ctypes.c_uint32(0) @@ -830,11 +884,13 @@ def get_property_int64(xmp, schema, name): """ # Use a function callback instead of returning a boolean value. EXEMPI.xmp_get_property_int64.restype = check_error - EXEMPI.xmp_get_property_int64.argtypes = [ctypes.c_void_p, - ctypes.c_char_p, - ctypes.c_char_p, - ctypes.POINTER(ctypes.c_int64), - ctypes.POINTER(ctypes.c_uint32)] + EXEMPI.xmp_get_property_int64.argtypes = [ + ctypes.c_void_p, + ctypes.c_char_p, + ctypes.c_char_p, + ctypes.POINTER(ctypes.c_int64), + ctypes.POINTER(ctypes.c_uint32) + ] ivalue = ctypes.c_int64(0) prop_bits = ctypes.c_uint32(0) @@ -975,8 +1031,9 @@ def iterator_next(iterator): _propvalue = _string_new() options = ctypes.c_uint32(0) - success = EXEMPI.xmp_iterator_next(iterator, _schema, _propname, _propvalue, - ctypes.byref(options)) + success = EXEMPI.xmp_iterator_next(iterator, _schema, _propname, + _propvalue, + ctypes.byref(options)) if not success: _string_free(_schema) @@ -1192,6 +1249,7 @@ def register_namespace(namespace_uri, prefix): return registered_prefix + def serialize(xmp, options, padding): """Serialize the XMP Packet. @@ -1266,7 +1324,9 @@ def serialize_and_format(xmp, options, padding, newline, tab, indent): ctypes.c_int32] _item = _string_new() EXEMPI.xmp_serialize_and_format(xmp, _item, options, padding, - newline.encode('utf-8'), tab.encode('utf-8'), indent) + newline.encode('utf-8'), + tab.encode('utf-8'), + indent) item = string_cstr(_item) _string_free(_item) @@ -1357,6 +1417,7 @@ def set_localized_text(xmp, schema, name, generic_lang, specific_lang, value, value.encode('utf-8'), mask) + def set_property(xmp, schema, name, value, option_bits=0): """Set an XMP property in the XMP packet. @@ -1456,11 +1517,13 @@ def set_property_date(xmp, schema, name, the_date, option_bits=0): """ # Use a function callback instead of returning a boolean value. EXEMPI.xmp_set_property_date.restype = check_error - EXEMPI.xmp_set_property_date.argtypes = [ctypes.c_void_p, - ctypes.c_char_p, - ctypes.c_char_p, - ctypes.POINTER(XmpDateTime), - ctypes.c_uint32] + EXEMPI.xmp_set_property_date.argtypes = [ + ctypes.c_void_p, + ctypes.c_char_p, + ctypes.c_char_p, + ctypes.POINTER(XmpDateTime), + ctypes.c_uint32 + ] if the_date.tzinfo is not None: the_date = the_date.astimezone(pytz.utc) diff --git a/test/test_exempi.py b/test/test_exempi.py index aafd59d..5c336ae 100644 --- a/test/test_exempi.py +++ b/test/test_exempi.py @@ -2,14 +2,11 @@ """ Test suites for exempi routine wrappers. """ - -# R0904: Not too many methods in unittest. -# pylint: disable=R0904 - import datetime import os import pkg_resources import platform +import re import shutil import sys import tempfile @@ -72,6 +69,27 @@ class TestPythonXmpToolkit(unittest.TestCase): """ Test suite for cases added by python xmp toolkit devs. """ + @unittest.skipIf(re.match('2.[3-9]', exempi._libexempi_version), + 'Requires exempi < 2.3') + def test_do_not_run_when_libexempi_lt_2p3(self): + """ + Error out when library version not at least 2.3 + + files_get_xmp_xmpstring method is not available until exempi version + 2.3 + """ + filename = pkg_resources.resource_filename(__name__, + "samples/BlueSquare.jpg") + + with tempfile.NamedTemporaryFile(suffix=".jpg") as tfile: + shutil.copyfile(filename, tfile.name) + + xfptr = exempi.files_open_new(tfile.name, XMP_OPEN_FORUPDATE) + xmp = exempi.files_get_new_xmp(xfptr) + + with self.assertRaises(NotImplementedError): + exempi.files_get_xmp_xmpstring(xfptr, xmp) + def test_file_not_there_open_new(self): """ The library does not catch comfortably, so we perform our own check. @@ -97,7 +115,8 @@ def tearDown(self): exempi.terminate() def test_bgo(self): - """Corresponds to test-bgo.cpp + """ + Corresponds to test-bgo.cpp """ filename = pkg_resources.resource_filename(__name__, "samples/fdo18635.jpg") @@ -107,8 +126,16 @@ def test_bgo(self): exempi.files_free(xfptr) self.assertTrue(True) + def test_get_property_datetime(self): + """ + See issue #48 + """ + pass + def test_write_new_property(self): - """Corresponds to test-write-new-property.cpp""" + """ + Corresponds to test-write-new-property.cpp + """ filename = pkg_resources.resource_filename(__name__, "samples/test1.xmp") @@ -242,10 +269,14 @@ def test_exempi_core(self): the_prop, _ = exempi.get_property(xmp, NS_EXIF, "DateTimeOriginal") self.assertEqual(the_prop, "2006-12-07T23:20:43-05:00") - # When the time information is read back, it is UTC. + # When the time information is read back, it is UTC -5 the_prop, _ = exempi.get_property_date(xmp, NS_EXIF, "DateTimeOriginal") self.assertEqual(the_prop.year, 2006) + self.assertEqual(the_prop.month, 12) + self.assertEqual(the_prop.day, 8) + self.assertEqual(the_prop.hour, 4) self.assertEqual(the_prop.minute, 20) + self.assertEqual(the_prop.second, 43) self.assertEqual(the_prop.tzinfo, pytz.utc) the_prop, _ = exempi.get_property(xmp, NS_XAP, "Rating") @@ -289,7 +320,9 @@ def test_exempi_core(self): def test_xmpfiles_write(self): - """According to test-xmpfiles-write.cpp""" + """ + Corresponds to test-xmpfiles-write.cpp + """ filename = pkg_resources.resource_filename(__name__, "samples/BlueSquare.jpg") @@ -304,6 +337,14 @@ def test_xmpfiles_write(self): shutil.copyfile(filename, tfile.name) xfptr = exempi.files_open_new(tfile.name, XMP_OPEN_FORUPDATE) + xmp = exempi.files_get_new_xmp(xfptr) + + if exempi._libexempi_version.startswith('2.3'): + packet_info = exempi.files_get_xmp_xmpstring(xfptr, xmp) + self.assertEqual(packet_info.offset, 2189) + self.assertEqual(packet_info.length, 4782) + self.assertEqual(packet_info.pad_size, 2049) + self.assertTrue(packet_info.has_wrapper) exempi.set_property(xmp, NS_PHOTOSHOP, "ICCProfile", "foo", 0) self.assertTrue(exempi.files_can_put_xmp(xfptr, xmp))