From 56d39a5533de74f69b4bb586d3fa4be87748539a Mon Sep 17 00:00:00 2001 From: John Evans Date: Sat, 20 Feb 2016 10:18:12 -0500 Subject: [PATCH 1/6] Fix timezone offset bug If the time being described is one hour ahead of UTC, the UTC offset is typically given as "+01", so subtract one to get UTC. We had done it the other way around. Closes-Issue: #48 --- libxmp/exempi.py | 7 +++++-- test/test_exempi.py | 13 ++++++++++++- 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/libxmp/exempi.py b/libxmp/exempi.py index 51345e7..072bcb8 100644 --- a/libxmp/exempi.py +++ b/libxmp/exempi.py @@ -755,10 +755,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 diff --git a/test/test_exempi.py b/test/test_exempi.py index aafd59d..79ef901 100644 --- a/test/test_exempi.py +++ b/test/test_exempi.py @@ -107,6 +107,13 @@ 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""" filename = pkg_resources.resource_filename(__name__, @@ -242,10 +249,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") From 254a106dd2689a5bab9cba0cbe0c8086f0d52240 Mon Sep 17 00:00:00 2001 From: John Evans Date: Mon, 21 Mar 2016 20:03:04 -0400 Subject: [PATCH 2/6] Qualify on Python 3.5, exempi 2.3.0 All tests pass. Closes-Issue: #49 --- CHANGELOG | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) 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. From c7e7ec344cdd1a4506fcc2c2a3aa2e3e16140134 Mon Sep 17 00:00:00 2001 From: John Evans Date: Tue, 22 Mar 2016 20:32:09 -0400 Subject: [PATCH 3/6] Added xmp_files_get_xmp_xmpstring wrapper Causes a segmentation fault after return, probably got the bool part wrong. --- libxmp/exempi.py | 66 +++++++++++++++++++++++++++++++++------------ test/test_exempi.py | 7 +++++ 2 files changed, 56 insertions(+), 17 deletions(-) diff --git a/libxmp/exempi.py b/libxmp/exempi.py index 072bcb8..cba01bd 100644 --- a/libxmp/exempi.py +++ b/libxmp/exempi.py @@ -185,6 +185,20 @@ 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_int32), + ("has_wrapper", ctypes.c_int32), + ("pad", ctypes.c_uint8), + ] + def files_can_put_xmp(xfptr, xmp): """Wrapper for xmp_files_can_put_xmp library routine. @@ -206,23 +220,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. @@ -286,6 +283,23 @@ def delete_property(xmp, schema, name): 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): """Close an XMP file, flush the changes. @@ -419,6 +433,24 @@ def files_get_xmp(xfptr): return xmp +def files_get_xmp_xmpstring(xfptr, xmp): + """ + Returns + ------- + packet_info : + """ + 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. diff --git a/test/test_exempi.py b/test/test_exempi.py index 79ef901..56a5d2d 100644 --- a/test/test_exempi.py +++ b/test/test_exempi.py @@ -315,6 +315,13 @@ 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) + + 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.assertFalse(packet_info.has_wrapper) exempi.set_property(xmp, NS_PHOTOSHOP, "ICCProfile", "foo", 0) self.assertTrue(exempi.files_can_put_xmp(xfptr, xmp)) From 71482c530643e091bf2ce73e8ad784b329c01156 Mon Sep 17 00:00:00 2001 From: John Evans Date: Wed, 23 Mar 2016 18:33:54 -0400 Subject: [PATCH 4/6] Add exempi library version attribute. Store in 'exempi._libexempi_version'. Not to be used publically. Nothing in libexempi API to get this, must determine based on other APIs. --- libxmp/exempi.py | 5 +++++ test/test_exempi.py | 11 ++++++----- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/libxmp/exempi.py b/libxmp/exempi.py index cba01bd..82a268d 100644 --- a/libxmp/exempi.py +++ b/libxmp/exempi.py @@ -68,6 +68,11 @@ 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", diff --git a/test/test_exempi.py b/test/test_exempi.py index 56a5d2d..1ca48db 100644 --- a/test/test_exempi.py +++ b/test/test_exempi.py @@ -317,11 +317,12 @@ def test_xmpfiles_write(self): xfptr = exempi.files_open_new(tfile.name, XMP_OPEN_FORUPDATE) xmp = exempi.files_get_new_xmp(xfptr) - 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.assertFalse(packet_info.has_wrapper) + 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.assertFalse(packet_info.has_wrapper) exempi.set_property(xmp, NS_PHOTOSHOP, "ICCProfile", "foo", 0) self.assertTrue(exempi.files_can_put_xmp(xfptr, xmp)) From f5f2981d871825ec2138d77b7cd7424a1f2e8021 Mon Sep 17 00:00:00 2001 From: John Evans Date: Wed, 23 Mar 2016 19:02:15 -0400 Subject: [PATCH 5/6] Add unit test for appropriate handling of 2.3 APIs If not at least version 2.3, should error out if attempting to use 2.3 APIs. PEP8 work. --- libxmp/exempi.py | 141 ++++++++++++++++++++++++++------------------ test/test_exempi.py | 38 +++++++++--- 2 files changed, 112 insertions(+), 67 deletions(-) diff --git a/libxmp/exempi.py b/libxmp/exempi.py index 82a268d..c46a6cf 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.') @@ -74,43 +76,45 @@ def _load_exempi(): _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): @@ -204,6 +208,7 @@ class PacketInfoType(ctypes.Structure): ("pad", ctypes.c_uint8), ] + def files_can_put_xmp(xfptr, xmp): """Wrapper for xmp_files_can_put_xmp library routine. @@ -285,7 +290,9 @@ 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): @@ -442,8 +449,12 @@ def files_get_xmp_xmpstring(xfptr, xmp): """ Returns ------- - packet_info : + 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, @@ -588,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. @@ -828,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) @@ -870,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) @@ -1015,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) @@ -1232,6 +1249,7 @@ def register_namespace(namespace_uri, prefix): return registered_prefix + def serialize(xmp, options, padding): """Serialize the XMP Packet. @@ -1306,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) @@ -1397,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. @@ -1496,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 1ca48db..b11b299 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") @@ -109,13 +128,14 @@ def test_bgo(self): 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") @@ -300,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") From 6fef52a285750788e7920a4e0c5988a7455a38b1 Mon Sep 17 00:00:00 2001 From: John Evans Date: Wed, 23 Mar 2016 19:31:10 -0400 Subject: [PATCH 6/6] bool types translate to c_bool --- libxmp/exempi.py | 4 ++-- test/test_exempi.py | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/libxmp/exempi.py b/libxmp/exempi.py index c46a6cf..0d39d3f 100644 --- a/libxmp/exempi.py +++ b/libxmp/exempi.py @@ -203,8 +203,8 @@ class PacketInfoType(ctypes.Structure): ("length", ctypes.c_int32), ("pad_size", ctypes.c_int32), ("char_form", ctypes.c_uint8), - ("writeable", ctypes.c_int32), - ("has_wrapper", ctypes.c_int32), + ("writeable", ctypes.c_bool), + ("has_wrapper", ctypes.c_bool), ("pad", ctypes.c_uint8), ] diff --git a/test/test_exempi.py b/test/test_exempi.py index b11b299..5c336ae 100644 --- a/test/test_exempi.py +++ b/test/test_exempi.py @@ -344,7 +344,7 @@ def test_xmpfiles_write(self): self.assertEqual(packet_info.offset, 2189) self.assertEqual(packet_info.length, 4782) self.assertEqual(packet_info.pad_size, 2049) - self.assertFalse(packet_info.has_wrapper) + self.assertTrue(packet_info.has_wrapper) exempi.set_property(xmp, NS_PHOTOSHOP, "ICCProfile", "foo", 0) self.assertTrue(exempi.files_can_put_xmp(xfptr, xmp))