def test_xmp(self): # datetime d = datetime.datetime(1899, 12, 31) self.assertEqual(DateTimeFormatter.xmp(d), '1899-12-31') d = datetime.datetime(1899, 12, 31, tzinfo=FixedOffset()) self.assertEqual(DateTimeFormatter.xmp(d), '1899-12-31') d = datetime.datetime(1899, 12, 31, 23, 59) self.assertEqual(DateTimeFormatter.xmp(d), '1899-12-31T23:59Z') d = datetime.datetime(1899, 12, 31, 23, 59, tzinfo=FixedOffset()) self.assertEqual(DateTimeFormatter.xmp(d), '1899-12-31T23:59Z') d = datetime.datetime(1899, 12, 31, 23, 59, tzinfo=FixedOffset(hours=3)) self.assertEqual(DateTimeFormatter.xmp(d), '1899-12-31T23:59+03:00') d = datetime.datetime(1899, 12, 31, 23, 59, 59) self.assertEqual(DateTimeFormatter.xmp(d), '1899-12-31T23:59:59Z') d = datetime.datetime(1899, 12, 31, 23, 59, 59, tzinfo=FixedOffset()) self.assertEqual(DateTimeFormatter.xmp(d), '1899-12-31T23:59:59Z') d = datetime.datetime(1899, 12, 31, 23, 59, 59, tzinfo=FixedOffset(hours=3)) self.assertEqual(DateTimeFormatter.xmp(d), '1899-12-31T23:59:59+03:00') d = datetime.datetime(1899, 12, 31, 23, 59, 59, 999999) self.assertEqual(DateTimeFormatter.xmp(d), '1899-12-31T23:59:59.999999Z') d = datetime.datetime(1899, 12, 31, 23, 59, 59, 999999, tzinfo=FixedOffset()) self.assertEqual(DateTimeFormatter.xmp(d), '1899-12-31T23:59:59.999999Z') d = datetime.datetime(1899, 12, 31, 23, 59, 59, 999999, tzinfo=FixedOffset(hours=3)) self.assertEqual(DateTimeFormatter.xmp(d), '1899-12-31T23:59:59.999999+03:00') d = datetime.datetime(2011, 8, 11, 9, 23, 44) self.assertEqual(DateTimeFormatter.xmp(d), '2011-08-11T09:23:44Z') # date d = datetime.date(1899, 12, 31) self.assertEqual(DateTimeFormatter.xmp(d), '1899-12-31') d = datetime.date(2011, 8, 8) self.assertEqual(DateTimeFormatter.xmp(d), '2011-08-08') # invalid type self.assertRaises(TypeError, DateTimeFormatter.xmp, None) self.assertRaises(TypeError, DateTimeFormatter.xmp, 3.14)
def _convert_to_string(self, value, type_): """Convert a value to its corresponding string representation. Args: value -- the value to be converted type_ -- the simple type of the value Return: the value converted to its corresponding string representation Raise XmpValueError: if the conversion fails """ if type_ == 'Boolean': if isinstance(value, bool): return str(value) else: raise XmpValueError(value, type_) elif type_ == 'Date': if isinstance(value, (datetime.date, datetime.datetime)): return DateTimeFormatter.xmp(value) else: raise XmpValueError(value, type_) elif type_ == 'GPSCoordinate': if isinstance(value, GPSCoordinate): return str(value) else: raise XmpValueError(value, type_) elif type_ == 'Integer': if isinstance(value, int): return str(value) else: raise XmpValueError(value, type_) elif type_ == 'MIMEType': if isinstance(value, tuple) and len(value) == 2: return '/'.join(value) else: raise XmpValueError(value, type_) elif type_ in ('AgentName', 'ProperName', 'Text', 'URI', 'URL'): if isinstance(value, str): try: return value.encode('utf-8') except UnicodeEncodeError: raise XmpValueError(value, type_) elif isinstance(value, bytes): return value raise XmpValueError(value, type_) elif type_ == 'Rational': if is_fraction(value): return str(value) else: raise XmpValueError(value, type_) elif type_ == '': # Undefined type if isinstance(value, str): try: return value.encode('utf-8') except UnicodeEncodeError: raise XmpValueError(value, type_) elif isinstance(value, (datetime.date, datetime.datetime)): return DateTimeFormatter.xmp(value) raise NotImplementedError('XMP conversion for type [%s]' % type_)
def _convert_to_string(self, value, type): """ Convert a value to its corresponding string representation, suitable to pass to libexiv2. :param value: the value to be converted :param type: the simple type of the value :type type: string :return: the value converted to its corresponding string representation :rtype: string :raise XmpValueError: if the conversion fails """ if type == 'Boolean': if isinstance(value, bool): return str(value) else: raise XmpValueError(value, type) elif type == 'Date': if isinstance(value, (datetime.date, datetime.datetime)): return DateTimeFormatter.xmp(value) else: raise XmpValueError(value, type) elif type == 'GPSCoordinate': if isinstance(value, GPSCoordinate): return str(value) else: raise XmpValueError(value, type) elif type == 'Integer': if isinstance(value, (int, long)): return str(value) else: raise XmpValueError(value, type) elif type == 'MIMEType': if isinstance(value, tuple) and len(value) == 2: return '/'.join(value) else: raise XmpValueError(value, type) elif type in ('AgentName', 'ProperName', 'Text', 'URI', 'URL'): if isinstance(value, unicode): try: return value.encode('utf-8') except UnicodeEncodeError: raise XmpValueError(value, type) elif isinstance(value, str): return value else: raise XmpValueError(value, type) elif type == 'Rational': if is_fraction(value): return str(value) else: raise XmpValueError(value, type) elif type == '': # Unknown type, assume string if isinstance(value, unicode): try: return value.encode('utf-8') except UnicodeEncodeError: raise XmpValueError(value, type) elif isinstance(value, str): return value else: raise XmpValueError(value, type) raise NotImplementedError('XMP conversion for type [%s]' % type)
def test_xmp_invalid(bad_ts): with pytest.raises(TypeError): DateTimeFormatter.xmp(bad_ts)
def test_xmp_valid(formatted, ts): assert DateTimeFormatter.xmp(ts) == formatted