def test_xmpfiles_write(self): """According to test-xmpfiles-write.cpp""" filename = pkg_resources.resource_filename(__name__, "samples/BlueSquare.jpg") fmt = exempi.files_check_file_format(filename) self.assertEqual(fmt, libxmp.consts.XMP_FT_JPEG) xfptr = exempi.files_open_new(filename, XMP_OPEN_READ) xmp = exempi.files_get_new_xmp(xfptr) exempi.files_free(xfptr) with tempfile.NamedTemporaryFile(suffix=".jpg") as tfile: shutil.copyfile(filename, tfile.name) xfptr = exempi.files_open_new(tfile.name, XMP_OPEN_FORUPDATE) exempi.set_property(xmp, NS_PHOTOSHOP, "ICCProfile", "foo", 0) self.assertTrue(exempi.files_can_put_xmp(xfptr, xmp)) exempi.files_put_xmp(xfptr, xmp) exempi.free(xmp) exempi.files_close(xfptr, XMP_CLOSE_SAFEUPDATE) exempi.files_free(xfptr) xfptr = exempi.files_open_new(tfile.name, XMP_OPEN_READ) xmp = exempi.files_get_new_xmp(xfptr) the_prop, _ = exempi.get_property(xmp, NS_PHOTOSHOP, "ICCProfile") self.assertEqual("foo", the_prop) exempi.free(xmp) exempi.files_close(xfptr, XMP_CLOSE_NOOPTION) exempi.files_free(xfptr)
def test_write_new_date_property(self): """ TODO: fill in the doc string """ filename = pkg_resources.resource_filename(__name__, "samples/test1.xmp") with open(filename, 'r') as fptr: strbuffer = fptr.read() xmp = exempi.new_empty() self.assertFalse(exempi.get_error()) exempi.parse(xmp, strbuffer) self.assertFalse(exempi.get_error()) the_dt = datetime.datetime(2005, 12, 25, 12, 42, 42) exempi.set_property_date(xmp, NS_EXIF, "DateTimeOriginal", the_dt, 0) the_prop, _ = exempi.get_property(xmp, NS_EXIF, "DateTimeOriginal") self.assertEqual("2005-12-25T12:42:42", the_prop) the_prop, _ = exempi.get_property_date(xmp, NS_EXIF, "DateTimeOriginal") self.assertEqual(the_prop.year, 2005) self.assertEqual(the_prop.minute, 42) self.assertEqual( the_prop, datetime.datetime(2005, 12, 25, 12, 42, 42, tzinfo=pytz.utc)) exempi.free(xmp)
def test_3(self): """Corresponds to test3.cpp""" filename = pkg_resources.resource_filename(__name__, "samples/test1.xmp") with open(filename, 'r') as fptr: strbuffer = fptr.read() xmp = exempi.new_empty() exempi.parse(xmp, strbuffer) options = XMP_ITERATOR_OPTIONS['iter_justleafnodes'] iterator = exempi.iterator_new(xmp, None, None, options) schemas = [] paths = [] props = [] while True: try: schema, path, prop, _ = exempi.iterator_next(iterator) schemas.append(schema) paths.append(path) props.append(prop) except StopIteration: break exempi.iterator_free(iterator) exempi.free(xmp)
def collect_iteration(self, schema, prop, options): """Run thru an iteration for a given configuration.""" filename = pkg_resources.resource_filename(__name__, "samples/test1.xmp") with open(filename, 'r') as fptr: strbuffer = fptr.read() xmp = exempi.new_empty() exempi.parse(xmp, strbuffer) iterator = exempi.iterator_new(xmp, schema, prop, options) schemas = [] paths = [] props = [] while True: try: schema, path, prop, _ = exempi.iterator_next(iterator) schemas.append(schema) paths.append(path) props.append(prop) except StopIteration: break exempi.iterator_free(iterator) exempi.free(xmp) return schemas, paths, props
def test_write_new_date_property(self): """ TODO: fill in the doc string """ filename = pkg_resources.resource_filename(__name__, "samples/test1.xmp") with open(filename, 'r') as fptr: strbuffer = fptr.read() xmp = exempi.new_empty() self.assertFalse(exempi.get_error()) exempi.parse(xmp, strbuffer) self.assertFalse(exempi.get_error()) the_dt = datetime.datetime(2005, 12, 25, 12, 42, 42) exempi.set_property_date(xmp, NS_EXIF, "DateTimeOriginal", the_dt, 0) the_prop, _ = exempi.get_property(xmp, NS_EXIF, "DateTimeOriginal") self.assertEqual("2005-12-25T12:42:42", the_prop) the_prop, _ = exempi.get_property_date(xmp, NS_EXIF, "DateTimeOriginal") self.assertEqual(the_prop.year, 2005) self.assertEqual(the_prop.minute, 42) self.assertEqual(the_prop, datetime.datetime(2005, 12, 25, 12, 42, 42, tzinfo=pytz.utc)) exempi.free(xmp)
def collect_iteration(self, schema, prop, options): """Run thru an iteration for a given configuration.""" filename = pkg_resources.resource_filename(__name__, "samples/test1.xmp") with open(filename, 'r') as fptr: strbuffer = fptr.read() xmp = exempi.new_empty() exempi.parse(xmp, strbuffer) iterator = exempi.iterator_new(xmp, schema, prop, options) schemas = [] paths = [] props = [] if iterator: while True: try: schema, path, prop, _ = exempi.iterator_next(iterator) schemas.append(schema) paths.append(path) props.append(prop) except StopIteration: break exempi.iterator_free(iterator) exempi.free(xmp) return schemas, paths, props
def get_xmp(self, tag): xfptr = exempi.files_open_new(self.path, XMP_OPEN_READ) xmp = exempi.files_get_new_xmp(xfptr) the_prop, _ = exempi.get_property(xmp, NS_PHOTOSHOP, tag) exempi.free(xmp) exempi.files_close(xfptr, XMP_CLOSE_NOOPTION) exempi.files_free(xfptr)
def write_xmp(self, tag, value): xfptr = exempi.files_open_new(self.path, XMP_OPEN_FORUPDATE) xmp = exempi.files_get_new_xmp(xfptr) if xmp is not None: exempi.set_property(xmp, NS_PHOTOSHOP, tag, value, 0) exempi.files_put_xmp(xfptr, xmp) exempi.free(xmp) exempi.files_close(xfptr, XMP_CLOSE_SAFEUPDATE) exempi.files_free(xfptr)
def test_bgo(self): """Corresponds to test-bgo.cpp """ filename = pkg_resources.resource_filename(__name__, "samples/fdo18635.jpg") xfptr = exempi.files_open_new(filename, XMP_OPEN_READ) xmp = exempi.files_get_new_xmp(xfptr) exempi.free(xmp) exempi.files_free(xfptr) self.assertTrue(True)
def test_tiff_leak(self): """Corresponds to test-tiff-leak.cpp""" orig_file = pkg_resources.resource_filename(__name__, "samples/BlueSquare.tif") with tempfile.NamedTemporaryFile(suffix='.tif') as tfile: shutil.copyfile(orig_file, tfile.name) xfptr = exempi.files_open_new(tfile.name, XMP_OPEN_FORUPDATE) xmp = exempi.files_get_new_xmp(xfptr) exempi.set_localized_text(xmp, NS_DC, "description", "en", "en-US", "foo", 0) exempi.files_put_xmp(xfptr, xmp) exempi.files_close(xfptr, XMP_CLOSE_NOOPTION) exempi.free(xmp) exempi.files_free(xfptr) self.assertTrue(True)
def test_serialize(self): """Corresponds to test-serialize.cpp""" filename = pkg_resources.resource_filename(__name__, "samples/test1.xmp") with open(filename, 'r') as fptr: strbuffer = fptr.read() self.assertFalse(exempi.get_error()) xmp = exempi.new_empty() self.assertFalse(exempi.get_error()) exempi.parse(xmp, strbuffer) self.assertFalse(exempi.get_error()) options = XMP_SERIAL_OPTIONS['omit_packet_wrapper'] # TODO: test this somehow. output = exempi.serialize_and_format(xmp, options, 0, '\n', ' ', 0) self.assertFalse(exempi.get_error()) exempi.free(xmp)
def test_init(self): """TODO: fill in the docstring""" filename = pkg_resources.resource_filename(__name__, "samples/test1.xmp") with open(filename, 'r') as fptr: strbuffer = fptr.read() exempi.init() exempi.init() xmp = exempi.new_empty() exempi.parse(xmp, strbuffer) exempi.free(xmp) exempi.terminate() xmp = exempi.new_empty() exempi.parse(xmp, strbuffer) exempi.free(xmp) exempi.terminate() self.assertTrue(True)
def test_iter_skip_subtree(self): """Alter the iteration midstream.""" options = XMP_ITERATOR_OPTIONS['iter_properties'] filename = pkg_resources.resource_filename(__name__, "samples/test1.xmp") with open(filename, 'r') as fptr: strbuffer = fptr.read() xmp = exempi.new_empty() exempi.parse(xmp, strbuffer) iterator = exempi.iterator_new(xmp, None, None, options) schemas = [] paths = [] props = [] while True: try: schema, path, prop, _ = exempi.iterator_next(iterator) if schema == NS_TIFF: exempi.iterator_skip(iterator, XMP_SKIP_OPTIONS['iter_skipsubtree']) continue schemas.append(schema) paths.append(path) props.append(prop) except StopIteration: break exempi.iterator_free(iterator) exempi.free(xmp) # If the iteration modification worked, there should be no TIFF # properties in the list of schemas. self.assertTrue(NS_TIFF not in schemas)
def test_write_new_property(self): """Corresponds to test-write-new-property.cpp""" filename = pkg_resources.resource_filename(__name__, "samples/test1.xmp") with open(filename, 'r') as fptr: strbuffer = fptr.read() xmp = exempi.new_empty() self.assertFalse(exempi.get_error()) exempi.parse(xmp, strbuffer) self.assertFalse(exempi.get_error()) reg_prefix = exempi.register_namespace(NS_CC, "cc") self.assertEqual("cc:", reg_prefix) reg_prefix = exempi.prefix_namespace_uri("cc") self.assertEqual(NS_CC, reg_prefix) reg_prefix = exempi.namespace_prefix(NS_CC) self.assertEqual("cc:", reg_prefix) exempi.set_property(xmp, NS_CC, "License", "Foo", 0) the_prop, _ = exempi.get_property(xmp, NS_CC, "License") self.assertEqual(the_prop, "Foo") the_dt = datetime.datetime(2005, 12, 25, 12, 42, 42, tzinfo=pytz.utc) exempi.set_property_date(xmp, NS_EXIF, "DateTimeOriginal", the_dt, 0) the_prop, _ = exempi.get_property(xmp, NS_EXIF, "DateTimeOriginal") self.assertEqual("2005-12-25T12:42:42", the_prop) the_prop, _ = exempi.get_property_date(xmp, NS_EXIF, "DateTimeOriginal") self.assertEqual(the_prop.year, 2005) self.assertEqual(the_prop.minute, 42) self.assertEqual(the_prop.tzinfo, pytz.utc) exempi.free(xmp)
def test_exempi_core(self): """According to test-exempi-core.cpp""" filename = pkg_resources.resource_filename(__name__, "samples/test1.xmp") with open(filename, 'r') as fptr: strbuffer = fptr.read() xmp = exempi.new_empty() exempi.parse(xmp, strbuffer) self.assertTrue(exempi.has_property(xmp, NS_TIFF, 'Make')) self.assertFalse(exempi.has_property(xmp, NS_TIFF, 'Foo')) prop, mask = exempi.get_property(xmp, NS_TIFF, 'Make') self.assertEqual(prop, "Canon") exempi.set_property(xmp, NS_TIFF, "Make", "Leica", 0) prop, mask = exempi.get_property(xmp, NS_TIFF, 'Make') self.assertEqual(prop, "Leica") # Retrieves xml:lang attribute value of 1st rights element prop, mask = exempi.get_property(xmp, NS_DC, "rights[1]/?xml:lang") self.assertTrue(mask & XMP_PROP_IS_QUALIFIER) prop, mask = exempi.get_property(xmp, NS_DC, "rights[1]") self.assertTrue(mask & XMP_PROP_HAS_QUALIFIERS) item, mask, actual_lang = exempi.get_localized_text(xmp, NS_DC, "rights", None, "x-default") self.assertEqual(item, "2006, Hubert Figuiere") self.assertEqual(actual_lang, "x-default") exempi.set_localized_text(xmp, NS_DC, "rights", "en", "en-CA", "Foo bar", 0) # Ask for a US alternative. item, mask, actual_lang = exempi.get_localized_text(xmp, NS_DC, "rights", "en", "en-US") # And we only got the "en-CA" as the only "en" self.assertNotEqual(actual_lang, "en-US") self.assertEqual(actual_lang, "en-CA") # Check its value self.assertEqual(item, "Foo bar") # Remove the property x-default. exempi.delete_localized_text(xmp, NS_DC, "rights", "en", "en-CA") self.assertFalse(exempi.has_property(xmp, NS_DC, "rights[1]")) self.assertFalse(exempi.has_property(xmp, NS_DC, "rights[1]")) exempi.set_array_item(xmp, NS_DC, "creator", 2, "foo", 0) the_prop, bits = exempi.get_array_item(xmp, NS_DC, "creator", 2) self.assertTrue((bits & XMP_PROP_COMPOSITE_MASK) == 0) exempi.append_array_item(xmp, NS_DC, "creator", 0, "bar", 0) the_prop, bits = exempi.get_array_item(xmp, NS_DC, "creator", 3) self.assertTrue((bits & XMP_PROP_COMPOSITE_MASK) == 0) self.assertEqual(the_prop, "bar") exempi.delete_property(xmp, NS_DC, "creator[3]") self.assertFalse(exempi.has_property(xmp, NS_DC, "creator[3]")) 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. the_prop, _ = exempi.get_property_date(xmp, NS_EXIF, "DateTimeOriginal") self.assertEqual(the_prop.year, 2006) self.assertEqual(the_prop.minute, 20) self.assertEqual(the_prop.tzinfo, pytz.utc) the_prop, _ = exempi.get_property(xmp, NS_XAP, "Rating") self.assertEqual(the_prop, "3") # testing float get set the_prop, _ = exempi.get_property_float(xmp, NS_CAMERA_RAW_SETTINGS, "SharpenRadius") self.assertEqual(the_prop, 1.0) exempi.set_property_float(xmp, NS_CAMERA_RAW_SETTINGS, "SharpenRadius", 2.5, 0) the_prop, _ = exempi.get_property_float(xmp, NS_CAMERA_RAW_SETTINGS, "SharpenRadius") self.assertEqual(the_prop, 2.5) # testing bool get set the_prop, _ = exempi.get_property_bool(xmp, NS_CAMERA_RAW_SETTINGS, "AlreadyApplied") self.assertFalse(the_prop) exempi.set_property_bool(xmp, NS_CAMERA_RAW_SETTINGS, "AlreadyApplied", True, 0) the_prop, _ = exempi.get_property_bool(xmp, NS_CAMERA_RAW_SETTINGS, "AlreadyApplied") self.assertTrue(the_prop) # testing int get set the_prop, _ = exempi.get_property_int32(xmp, NS_EXIF, "MeteringMode") self.assertEqual(the_prop, 5) exempi.set_property_int32(xmp, NS_EXIF, "MeteringMode", 10, 0) the_prop, _ = exempi.get_property_int64(xmp, NS_EXIF, "MeteringMode") self.assertEqual(the_prop, 10) exempi.set_property_int64(xmp, NS_EXIF, "MeteringMode", 32, 0) the_prop, _ = exempi.get_property_int32(xmp, NS_EXIF, "MeteringMode") self.assertEqual(the_prop, 32) exempi.free(xmp) self.assertTrue(True)
def test_exempi_core(self): """According to test-exempi-core.cpp""" filename = pkg_resources.resource_filename(__name__, "samples/test1.xmp") with open(filename, 'r') as fptr: strbuffer = fptr.read() xmp = exempi.new_empty() exempi.parse(xmp, strbuffer) self.assertTrue(exempi.has_property(xmp, NS_TIFF, 'Make')) self.assertFalse(exempi.has_property(xmp, NS_TIFF, 'Foo')) prop, mask = exempi.get_property(xmp, NS_TIFF, 'Make') self.assertEqual(prop, "Canon") exempi.set_property(xmp, NS_TIFF, "Make", "Leica", 0) prop, mask = exempi.get_property(xmp, NS_TIFF, 'Make') self.assertEqual(prop, "Leica") # Retrieves xml:lang attribute value of 1st rights element prop, mask = exempi.get_property(xmp, NS_DC, "rights[1]/?xml:lang") self.assertTrue(mask & XMP_PROP_IS_QUALIFIER) prop, mask = exempi.get_property(xmp, NS_DC, "rights[1]") self.assertTrue(mask & XMP_PROP_HAS_QUALIFIERS) item, mask, actual_lang = exempi.get_localized_text( xmp, NS_DC, "rights", None, "x-default") self.assertEqual(item, "2006, Hubert Figuiere") self.assertEqual(actual_lang, "x-default") exempi.set_localized_text(xmp, NS_DC, "rights", "en", "en-CA", "Foo bar", 0) # Ask for a US alternative. item, mask, actual_lang = exempi.get_localized_text( xmp, NS_DC, "rights", "en", "en-US") # And we only got the "en-CA" as the only "en" self.assertNotEqual(actual_lang, "en-US") self.assertEqual(actual_lang, "en-CA") # Check its value self.assertEqual(item, "Foo bar") # Remove the property x-default. exempi.delete_localized_text(xmp, NS_DC, "rights", "en", "en-CA") self.assertFalse(exempi.has_property(xmp, NS_DC, "rights[1]")) self.assertFalse(exempi.has_property(xmp, NS_DC, "rights[1]")) exempi.set_array_item(xmp, NS_DC, "creator", 2, "foo", 0) the_prop, bits = exempi.get_array_item(xmp, NS_DC, "creator", 2) self.assertTrue((bits & XMP_PROP_COMPOSITE_MASK) == 0) exempi.append_array_item(xmp, NS_DC, "creator", 0, "bar", 0) the_prop, bits = exempi.get_array_item(xmp, NS_DC, "creator", 3) self.assertTrue((bits & XMP_PROP_COMPOSITE_MASK) == 0) self.assertEqual(the_prop, "bar") exempi.delete_property(xmp, NS_DC, "creator[3]") self.assertFalse(exempi.has_property(xmp, NS_DC, "creator[3]")) 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. the_prop, _ = exempi.get_property_date(xmp, NS_EXIF, "DateTimeOriginal") self.assertEqual(the_prop.year, 2006) self.assertEqual(the_prop.minute, 20) self.assertEqual(the_prop.tzinfo, pytz.utc) the_prop, _ = exempi.get_property(xmp, NS_XAP, "Rating") self.assertEqual(the_prop, "3") # testing float get set the_prop, _ = exempi.get_property_float(xmp, NS_CAMERA_RAW_SETTINGS, "SharpenRadius") self.assertEqual(the_prop, 1.0) exempi.set_property_float(xmp, NS_CAMERA_RAW_SETTINGS, "SharpenRadius", 2.5, 0) the_prop, _ = exempi.get_property_float(xmp, NS_CAMERA_RAW_SETTINGS, "SharpenRadius") self.assertEqual(the_prop, 2.5) # testing bool get set the_prop, _ = exempi.get_property_bool(xmp, NS_CAMERA_RAW_SETTINGS, "AlreadyApplied") self.assertFalse(the_prop) exempi.set_property_bool(xmp, NS_CAMERA_RAW_SETTINGS, "AlreadyApplied", True, 0) the_prop, _ = exempi.get_property_bool(xmp, NS_CAMERA_RAW_SETTINGS, "AlreadyApplied") self.assertTrue(the_prop) # testing int get set the_prop, _ = exempi.get_property_int32(xmp, NS_EXIF, "MeteringMode") self.assertEqual(the_prop, 5) exempi.set_property_int32(xmp, NS_EXIF, "MeteringMode", 10, 0) the_prop, _ = exempi.get_property_int64(xmp, NS_EXIF, "MeteringMode") self.assertEqual(the_prop, 10) exempi.set_property_int64(xmp, NS_EXIF, "MeteringMode", 32, 0) the_prop, _ = exempi.get_property_int32(xmp, NS_EXIF, "MeteringMode") self.assertEqual(the_prop, 32) exempi.free(xmp) self.assertTrue(True)