def test_exif(self):
        # datetime
        d = datetime.datetime(1899, 12, 31)
        self.assertEqual(DateTimeFormatter.exif(d), '1899:12:31 00:00:00')
        d = datetime.datetime(1899, 12, 31, 23)
        self.assertEqual(DateTimeFormatter.exif(d), '1899:12:31 23:00:00')
        d = datetime.datetime(1899, 12, 31, 23, 59)
        self.assertEqual(DateTimeFormatter.exif(d), '1899:12:31 23:59:00')
        d = datetime.datetime(1899, 12, 31, 23, 59, 59)
        self.assertEqual(DateTimeFormatter.exif(d), '1899:12:31 23:59:59')
        d = datetime.datetime(1899, 12, 31, 23, 59, 59, 999999)
        self.assertEqual(DateTimeFormatter.exif(d), '1899:12:31 23:59:59')
        d = datetime.datetime(1899, 12, 31, 23, 59, 59, tzinfo=FixedOffset())
        self.assertEqual(DateTimeFormatter.exif(d), '1899:12:31 23:59:59')
        d = datetime.datetime(1899,
                              12,
                              31,
                              23,
                              59,
                              59,
                              tzinfo=FixedOffset(hours=5))
        self.assertEqual(DateTimeFormatter.exif(d), '1899:12:31 23:59:59')
        d = datetime.datetime(2011, 8, 8, 19, 3, 37)
        self.assertEqual(DateTimeFormatter.exif(d), '2011:08:08 19:03:37')

        # date
        d = datetime.date(1899, 12, 31)
        self.assertEqual(DateTimeFormatter.exif(d), '1899:12:31')
        d = datetime.date(2011, 8, 8)
        self.assertEqual(DateTimeFormatter.exif(d), '2011:08:08')

        # invalid type
        self.assertRaises(TypeError, DateTimeFormatter.exif, None)
        self.assertRaises(TypeError, DateTimeFormatter.exif, 3.14)
    def test_iptc_date(self):
        # datetime
        d = datetime.datetime(1899, 12, 31)
        self.assertEqual(DateTimeFormatter.iptc_date(d), '1899-12-31')
        d = datetime.datetime(1899, 12, 31, 23)
        self.assertEqual(DateTimeFormatter.iptc_date(d), '1899-12-31')
        d = datetime.datetime(1899, 12, 31, 23, 59)
        self.assertEqual(DateTimeFormatter.iptc_date(d), '1899-12-31')
        d = datetime.datetime(1899, 12, 31, 23, 59, 59)
        self.assertEqual(DateTimeFormatter.iptc_date(d), '1899-12-31')
        d = datetime.datetime(1899, 12, 31, 23, 59, 59, 999999)
        self.assertEqual(DateTimeFormatter.iptc_date(d), '1899-12-31')
        d = datetime.datetime(1899, 12, 31, 23, 59, 59, tzinfo=FixedOffset())
        self.assertEqual(DateTimeFormatter.iptc_date(d), '1899-12-31')
        d = datetime.datetime(1899,
                              12,
                              31,
                              23,
                              59,
                              59,
                              tzinfo=FixedOffset(hours=5))
        self.assertEqual(DateTimeFormatter.iptc_date(d), '1899-12-31')
        d = datetime.datetime(2011, 8, 8, 19, 3, 37)
        self.assertEqual(DateTimeFormatter.iptc_date(d), '2011-08-08')

        # date
        d = datetime.date(1899, 12, 31)
        self.assertEqual(DateTimeFormatter.iptc_date(d), '1899-12-31')
        d = datetime.date(2011, 8, 8)
        self.assertEqual(DateTimeFormatter.iptc_date(d), '2011-08-08')

        # invalid type
        self.assertRaises(TypeError, DateTimeFormatter.iptc_date, None)
        self.assertRaises(TypeError, DateTimeFormatter.iptc_date, 3.14)
Ejemplo n.º 3
0
 def test_convert_to_python_date(self):
     # Valid values
     tag = XmpTag('Xmp.xmp.CreateDate')
     self.assertEqual(tag.type, 'Date')
     self.assertEqual(tag._convert_to_python('1999', 'Date'),
                      datetime.date(1999, 1, 1))
     self.assertEqual(tag._convert_to_python('1999-10', 'Date'),
                      datetime.date(1999, 10, 1))
     self.assertEqual(tag._convert_to_python('1999-10-13', 'Date'),
                      datetime.date(1999, 10, 13))
     self.assertEqual(tag._convert_to_python('1999-10-13T05:03Z', 'Date') - \
                      datetime.datetime(1999, 10, 13, 5, 3, tzinfo=FixedOffset()),
                      datetime.timedelta(0))
     self.assertEqual(tag._convert_to_python('1999-10-13T05:03+06:00', 'Date') - \
                      datetime.datetime(1999, 10, 13, 5, 3, tzinfo=FixedOffset('+', 6, 0)),
                      datetime.timedelta(0))
     self.assertEqual(tag._convert_to_python('1999-10-13T05:03-06:00', 'Date') - \
                      datetime.datetime(1999, 10, 13, 5, 3, tzinfo=FixedOffset('-', 6, 0)),
                      datetime.timedelta(0))
     self.assertEqual(tag._convert_to_python('1999-10-13T05:03:54Z', 'Date') - \
                      datetime.datetime(1999, 10, 13, 5, 3, 54, tzinfo=FixedOffset()),
                      datetime.timedelta(0))
     self.assertEqual(tag._convert_to_python('1999-10-13T05:03:54+06:00', 'Date') - \
                      datetime.datetime(1999, 10, 13, 5, 3, 54, tzinfo=FixedOffset('+', 6, 0)),
                      datetime.timedelta(0))
     self.assertEqual(tag._convert_to_python('1999-10-13T05:03:54-06:00', 'Date') - \
                      datetime.datetime(1999, 10, 13, 5, 3, 54, tzinfo=FixedOffset('-', 6, 0)),
                      datetime.timedelta(0))
     self.assertEqual(tag._convert_to_python('1999-10-13T05:03:54.721Z', 'Date') - \
                      datetime.datetime(1999, 10, 13, 5, 3, 54, 721000, tzinfo=FixedOffset()),
                      datetime.timedelta(0))
     self.assertEqual(tag._convert_to_python('1999-10-13T05:03:54.721+06:00', 'Date') - \
                      datetime.datetime(1999, 10, 13, 5, 3, 54, 721000, tzinfo=FixedOffset('+', 6, 0)),
                      datetime.timedelta(0))
     self.assertEqual(tag._convert_to_python('1999-10-13T05:03:54.721-06:00', 'Date') - \
                      datetime.datetime(1999, 10, 13, 5, 3, 54, 721000, tzinfo=FixedOffset('-', 6, 0)),
                      datetime.timedelta(0))
     # Invalid values
     self.assertRaises(XmpValueError, tag._convert_to_python, 'invalid',
                       'Date')
     self.assertRaises(XmpValueError, tag._convert_to_python, '11/10/1983',
                       'Date')
     self.assertRaises(XmpValueError, tag._convert_to_python, '-1000',
                       'Date')
     self.assertRaises(XmpValueError, tag._convert_to_python, '2009-13',
                       'Date')
     self.assertRaises(XmpValueError, tag._convert_to_python, '2009-10-32',
                       'Date')
     self.assertRaises(XmpValueError, tag._convert_to_python,
                       '2009-10-30T25:12Z', 'Date')
     self.assertRaises(XmpValueError, tag._convert_to_python,
                       '2009-10-30T23:67Z', 'Date')
     self.assertRaises(XmpValueError, tag._convert_to_python,
                       '2009-01-22T21', 'Date')
Ejemplo n.º 4
0
    def test_convert_to_python_time(self):
        # Valid values
        tag = IptcTag('Iptc.Envelope.TimeSent')
        self.assertEqual(tag.type, 'Time')
        self.assertEqual(tag._convert_to_python('05:03:54+00:00'),
                         datetime.time(5, 3, 54, tzinfo=FixedOffset()))
        self.assertEqual(tag._convert_to_python('05:03:54+06:00'),
                         datetime.time(5, 3, 54, tzinfo=FixedOffset('+', 6, 0)))
        self.assertEqual(tag._convert_to_python('05:03:54-10:30'),
                         datetime.time(5, 3, 54, tzinfo=FixedOffset('-', 10, 30)))

        # Invalid values
        self.failUnlessRaises(IptcValueError, tag._convert_to_python, 'invalid')
        self.failUnlessRaises(IptcValueError, tag._convert_to_python, '23:12:42')
        self.failUnlessRaises(IptcValueError, tag._convert_to_python, '25:12:42+00:00')
        self.failUnlessRaises(IptcValueError, tag._convert_to_python, '21:77:42+00:00')
        self.failUnlessRaises(IptcValueError, tag._convert_to_python, '21:12:98+00:00')
        self.failUnlessRaises(IptcValueError, tag._convert_to_python, '081242+0000')
    def test_iptc_time(self):
        # datetime
        d = datetime.datetime(1899, 12, 31)
        self.assertEqual(DateTimeFormatter.iptc_time(d), '00:00:00+00:00')
        d = datetime.datetime(1899, 12, 31, 23)
        self.assertEqual(DateTimeFormatter.iptc_time(d), '23:00:00+00:00')
        d = datetime.datetime(1899, 12, 31, 23, 59)
        self.assertEqual(DateTimeFormatter.iptc_time(d), '23:59:00+00:00')
        d = datetime.datetime(1899, 12, 31, 23, 59, 59)
        self.assertEqual(DateTimeFormatter.iptc_time(d), '23:59:59+00:00')
        d = datetime.datetime(1899, 12, 31, 23, 59, 59, 999999)
        self.assertEqual(DateTimeFormatter.iptc_time(d), '23:59:59+00:00')
        d = datetime.datetime(1899, 12, 31, 23, 59, 59, tzinfo=FixedOffset())
        self.assertEqual(DateTimeFormatter.iptc_time(d), '23:59:59+00:00')
        d = datetime.datetime(1899,
                              12,
                              31,
                              23,
                              59,
                              59,
                              tzinfo=FixedOffset(hours=5))
        self.assertEqual(DateTimeFormatter.iptc_time(d), '23:59:59+05:00')
        d = datetime.datetime(2011, 8, 8, 19, 3, 37)
        self.assertEqual(DateTimeFormatter.iptc_time(d), '19:03:37+00:00')

        # time
        d = datetime.time(23)
        self.assertEqual(DateTimeFormatter.iptc_time(d), '23:00:00+00:00')
        d = datetime.time(23, 59)
        self.assertEqual(DateTimeFormatter.iptc_time(d), '23:59:00+00:00')
        d = datetime.time(23, 59, 59)
        self.assertEqual(DateTimeFormatter.iptc_time(d), '23:59:59+00:00')
        d = datetime.time(23, 59, 59, 999999)
        self.assertEqual(DateTimeFormatter.iptc_time(d), '23:59:59+00:00')
        d = datetime.time(23, 59, 59, tzinfo=FixedOffset())
        self.assertEqual(DateTimeFormatter.iptc_time(d), '23:59:59+00:00')
        d = datetime.time(23, 59, 59, tzinfo=FixedOffset(hours=5))
        self.assertEqual(DateTimeFormatter.iptc_time(d), '23:59:59+05:00')
        d = datetime.time(19, 3, 37)
        self.assertEqual(DateTimeFormatter.iptc_time(d), '19:03:37+00:00')

        # invalid type
        self.assertRaises(TypeError, DateTimeFormatter.iptc_time, None)
        self.assertRaises(TypeError, DateTimeFormatter.iptc_time, 3.14)
Ejemplo n.º 6
0
 def test_set_value(self):
     tag = XmpTag(
         'Xmp.xmp.ModifyDate',
         datetime.datetime(2005,
                           9,
                           7,
                           15,
                           9,
                           51,
                           tzinfo=FixedOffset('-', 7, 0)))
     old_value = tag.value
     tag.value = datetime.datetime(2009,
                                   4,
                                   22,
                                   8,
                                   30,
                                   27,
                                   tzinfo=FixedOffset())
     self.failIfEqual(tag.value, old_value)
Ejemplo n.º 7
0
def DT(y, mo, d, h=0, mi=0, s=0, us=0, tz=()):
    if tz is None:
        return datetime.datetime(y, mo, d, h, mi, s, us)
    else:
        return datetime.datetime(y,
                                 mo,
                                 d,
                                 h,
                                 mi,
                                 s,
                                 us,
                                 tzinfo=FixedOffset(*tz))
Ejemplo n.º 8
0
    def test_convert_to_string_time(self):
        # Valid values
        tag = IptcTag('Iptc.Envelope.TimeSent')
        self.assertEqual(tag.type, 'Time')
        self.assertEqual(tag._convert_to_string(datetime.time(10, 52, 4)),
                         '10:52:04+00:00')
        self.assertEqual(tag._convert_to_string(datetime.time(10, 52, 4, 574)),
                         '10:52:04+00:00')
        self.assertEqual(tag._convert_to_string(datetime.time(10, 52, 4, tzinfo=FixedOffset())),
                         '10:52:04+00:00')
        self.assertEqual(tag._convert_to_string(datetime.time(10, 52, 4, tzinfo=FixedOffset('+', 5, 30))),
                         '10:52:04+05:30')
        self.assertEqual(tag._convert_to_string(datetime.time(10, 52, 4, tzinfo=FixedOffset('-', 4, 0))),
                         '10:52:04-04:00')
        self.assertEqual(tag._convert_to_string(datetime.datetime(1899, 12, 31, 23, 59, 59)),
                         '23:59:59+00:00')
        self.assertEqual(tag._convert_to_string(datetime.datetime(2007, 2, 7, 10, 52, 4)),
                         '10:52:04+00:00')
        self.assertEqual(tag._convert_to_string(datetime.datetime(1899, 12, 31, 23, 59, 59, 999)),
                         '23:59:59+00:00')
        self.assertEqual(tag._convert_to_string(datetime.datetime(2007, 2, 7, 10, 52, 4, 478)),
                         '10:52:04+00:00')
        self.assertEqual(tag._convert_to_string(datetime.datetime(1899, 12, 31, 23, 59, 59, tzinfo=FixedOffset())),
                         '23:59:59+00:00')
        self.assertEqual(tag._convert_to_string(datetime.datetime(2007, 2, 7, 10, 52, 4, tzinfo=FixedOffset())),
                         '10:52:04+00:00')
        self.assertEqual(tag._convert_to_string(datetime.datetime(1899, 12, 31, 23, 59, 59, tzinfo=FixedOffset('+', 5, 30))),
                         '23:59:59+05:30')
        self.assertEqual(tag._convert_to_string(datetime.datetime(2007, 2, 7, 10, 52, 4, tzinfo=FixedOffset('+', 5, 30))),
                         '10:52:04+05:30')
        self.assertEqual(tag._convert_to_string(datetime.datetime(1899, 12, 31, 23, 59, 59, tzinfo=FixedOffset('-', 4, 0))),
                         '23:59:59-04:00')
        self.assertEqual(tag._convert_to_string(datetime.datetime(2007, 2, 7, 10, 52, 4, tzinfo=FixedOffset('-', 4, 0))),
                         '10:52:04-04:00')

        # Invalid values
        self.failUnlessRaises(IptcValueError, tag._convert_to_string, 'invalid')
Ejemplo n.º 9
0
 def test_pickle_iptc_tag(self):
     tags = []
     tags.append(IptcTag('Iptc.Envelope.FileFormat', [23]))
     tags.append(IptcTag('Iptc.Application2.Subject', ['foo', 'bar', 'baz']))
     tags.append(IptcTag('Iptc.Envelope.DateSent', [datetime.date.today()]))
     tags.append(IptcTag('Iptc.Envelope.TimeSent',
                         [datetime.time(23, 37, 4, tzinfo=FixedOffset('+', 6, 0))]))
     tags.append(IptcTag('Iptc.Application2.Preview', ['01001101']))
     for tag in tags:
         s = pickle.dumps(tag)
         t = pickle.loads(s)
         self.assert_(isinstance(t, IptcTag))
         self.assertEqual(t.key, tag.key)
         self.assertEqual(t.type, tag.type)
         self.assertEqual(t.name, tag.name)
         self.assertEqual(t.title, tag.title)
         self.assertEqual(t.description, tag.description)
         self.assertEqual(t.photoshop_name, tag.photoshop_name)
         self.assertEqual(t.repeatable, tag.repeatable)
         self.assertEqual(t.record_name, tag.record_name)
         self.assertEqual(t.record_description, tag.record_description)
         self.assertEqual(t.raw_value, tag.raw_value)
         self.assertEqual(t.value, tag.value)
Ejemplo n.º 10
0
    def _convert_to_python(self, value, type_):
        """Convert a raw value to its corresponding python type.

        Args:
        value -- the raw value to be converted
        type_ -- the simple type of the raw value

        Return: the value converted to its corresponding python type

        Raise XmpValueError: if the conversion fails
        """
        if type_ == 'Boolean':
            if value == 'True':
                return True

            elif value == 'False':
                return False

            else:
                raise XmpValueError(value, type_)

        elif type_ == 'Colorant':
            # TODO
            raise NotImplementedError('XMP conversion for type [%s]' % type_)

        elif type_ == 'Date':
            match = self._date_re.match(value)
            if match is None:
                raise XmpValueError(value, type_)

            gd = match.groupdict()
            if gd['month'] is not None:
                month = int(gd['month'])

            else:
                month = 1

            if gd['day'] is not None:
                day = int(gd['day'])

            else:
                day = 1

            if gd['time'] is None:
                try:
                    return datetime.date(int(gd['year']), month, day)
                except ValueError:
                    raise XmpValueError(value, type_)

            else:
                if gd['minutes'] is None:
                    # Malformed time
                    raise XmpValueError(value, type_)

                if gd['seconds'] is not None:
                    seconds = int(gd['seconds'])

                else:
                    seconds = 0

                if gd['decimal'] is not None:
                    microseconds = int(float('0.%s' % gd['decimal']) * 1E6)

                else:
                    microseconds = 0

                if gd['tzd'] == 'Z':
                    tzinfo = FixedOffset()

                else:
                    tzinfo = FixedOffset(gd['sign'], int(gd['ohours']),
                                         int(gd['ominutes']))

                try:
                    return datetime.datetime(int(gd['year']), month, day,
                                             int(gd['hours']),
                                             int(gd['minutes']), seconds,
                                             microseconds, tzinfo)
                except ValueError:
                    raise XmpValueError(value, type_)

        elif type_ == 'Dimensions':
            # TODO
            raise NotImplementedError('XMP conversion for type [%s]' % type_)

        elif type_ == 'Font':
            # TODO
            raise NotImplementedError('XMP conversion for type [%s]' % type_)

        elif type_ == 'GPSCoordinate':
            try:
                return GPSCoordinate.from_string(value)
            except ValueError:
                raise XmpValueError(value, type_)

        elif type_ == 'Integer':
            try:
                return int(value)
            except ValueError:
                raise XmpValueError(value, type_)

        elif type_ == 'Locale':
            # TODO
            # See RFC 3066
            raise NotImplementedError('XMP conversion for type [%s]' % type_)

        elif type_ == 'MIMEType':
            if value.count('/') != 1:
                raise XmpValueError(value, type_)
            try:
                return tuple(value.split('/', 1))
            except ValueError:
                raise XmpValueError(value, type_)

        elif type_ == 'Rational':
            try:
                return make_fraction(value)
            except (ValueError, ZeroDivisionError):
                raise XmpValueError(value, type_)

        elif type_ == 'Real':
            # TODO
            raise NotImplementedError('XMP conversion for type [%s]' % type_)

        elif type_ in ('AgentName', 'ProperName', 'Text'):
            if isinstance(value, bytes):
                try:
                    value = str(value, 'utf-8')
                except TypeError:
                    raise XmpValueError(value, type_)
            return value

        elif type_ == 'Thumbnail':
            # TODO
            raise NotImplementedError('XMP conversion for type [%s]' % type_)

        elif type_ in ('URI', 'URL'):
            if isinstance(value, bytes):
                try:
                    value = value.decode('utf-8')
                except UnicodeDecodeError:
                    # Unknow encoding, return the raw value
                    pass
            return value

        elif type_ == 'XPath':
            # TODO
            raise NotImplementedError('XMP conversion for type [%s]' % type_)

        raise NotImplementedError('XMP conversion for type [%s]' % type_)
Ejemplo n.º 11
0
    def _convert_to_python(self, value):
        """Convert one raw value to its corresponding python type.

        Args:
        value -- the raw value to be converted

        Return: the value converted to its corresponding python type

        Raise IptcValueError: if the conversion fails
        """
        if self.type == 'Short':
            try:
                return int(value)
            except ValueError:
                raise IptcValueError(value, self.type)

        elif self.type == 'String':
            # There is currently no charset conversion.
            # TODO: guess the encoding and decode accordingly into unicode
            # where relevant.
            if isinstance(value, bytes):
                try:
                    value = value.decode('utf-8')
                except UnicodeDecodeError:
                    # Unknow encoding, return the raw value
                    pass
            return value

        elif self.type == 'Date':
            # According to the IPTC specification, the format for a string field
            # representing a date is '%Y%m%d'. However, the string returned by
            # exiv2 using method DateValue::toString() is formatted using
            # pattern '%Y-%m-%d'.
            format = '%Y-%m-%d'
            try:
                t = time.strptime(value, format)
                return datetime.date(*t[:3])
            except ValueError:
                raise IptcValueError(value, self.type)

        elif self.type == 'Time':
            # According to the IPTC specification, the format for a string field
            # representing a time is '%H%M%S±%H%M'. However, the string returned
            # by exiv2 using method TimeValue::toString() is formatted using
            # pattern '%H:%M:%S±%H:%M'.
            match = IptcTag._time_re.match(value)
            if match is None:
                raise IptcValueError(value, self.type)

            gd = match.groupdict()
            try:
                tzinfo = FixedOffset(gd['sign'], int(gd['ohours']),
                                     int(gd['ominutes']))
            except TypeError:
                raise IptcValueError(value, self.type)

            try:
                return datetime.time(int(gd['hours']),
                                     int(gd['minutes']),
                                     int(gd['seconds']),
                                     tzinfo=tzinfo)
            except (TypeError, ValueError):
                raise IptcValueError(value, self.type)

        elif self.type == 'Undefined':
            # Binary data, return it unmodified
            return value

        raise IptcValueError(value, self.type)
    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)
Ejemplo n.º 13
0
 def test_convert_to_string_date(self):
     # Valid values
     tag = XmpTag('Xmp.xmp.CreateDate')
     self.assertEqual(tag.type, 'Date')
     self.assertEqual(
         tag._convert_to_string(datetime.date(2009, 2, 4), 'Date'),
         '2009-02-04')
     self.assertEqual(
         tag._convert_to_string(datetime.date(1899, 12, 31), 'Date'),
         '1899-12-31')
     self.assertEqual(
         tag._convert_to_string(datetime.datetime(1999, 10, 13), 'Date'),
         '1999-10-13')
     self.assertEqual(
         tag._convert_to_string(datetime.datetime(1999, 10, 13, 5, 3),
                                'Date'), '1999-10-13T05:03Z')
     self.assertEqual(
         tag._convert_to_string(datetime.datetime(1899, 12, 31, 23, 59),
                                'Date'), '1899-12-31T23:59Z')
     self.assertEqual(
         tag._convert_to_string(
             datetime.datetime(1999, 10, 13, 5, 3, tzinfo=FixedOffset()),
             'Date'), '1999-10-13T05:03Z')
     self.assertEqual(
         tag._convert_to_string(
             datetime.datetime(1899, 12, 31, 23, 59, tzinfo=FixedOffset()),
             'Date'), '1899-12-31T23:59Z')
     self.assertEqual(
         tag._convert_to_string(
             datetime.datetime(1999,
                               10,
                               13,
                               5,
                               3,
                               tzinfo=FixedOffset('+', 5, 30)), 'Date'),
         '1999-10-13T05:03+05:30')
     self.assertEqual(
         tag._convert_to_string(
             datetime.datetime(1999,
                               10,
                               13,
                               5,
                               3,
                               tzinfo=FixedOffset('-', 11, 30)), 'Date'),
         '1999-10-13T05:03-11:30')
     self.assertEqual(
         tag._convert_to_string(
             datetime.datetime(1899,
                               12,
                               31,
                               23,
                               59,
                               tzinfo=FixedOffset('+', 5, 30)), 'Date'),
         '1899-12-31T23:59+05:30')
     self.assertEqual(
         tag._convert_to_string(datetime.datetime(1999, 10, 13, 5, 3, 27),
                                'Date'), '1999-10-13T05:03:27Z')
     self.assertEqual(
         tag._convert_to_string(datetime.datetime(1899, 12, 31, 23, 59, 59),
                                'Date'), '1899-12-31T23:59:59Z')
     self.assertEqual(
         tag._convert_to_string(
             datetime.datetime(1999, 10, 13, 5, 3, 27,
                               tzinfo=FixedOffset()), 'Date'),
         '1999-10-13T05:03:27Z')
     self.assertEqual(
         tag._convert_to_string(
             datetime.datetime(1899,
                               12,
                               31,
                               23,
                               59,
                               59,
                               tzinfo=FixedOffset()), 'Date'),
         '1899-12-31T23:59:59Z')
     self.assertEqual(
         tag._convert_to_string(
             datetime.datetime(1999,
                               10,
                               13,
                               5,
                               3,
                               27,
                               tzinfo=FixedOffset('+', 5, 30)), 'Date'),
         '1999-10-13T05:03:27+05:30')
     self.assertEqual(
         tag._convert_to_string(
             datetime.datetime(1999,
                               10,
                               13,
                               5,
                               3,
                               27,
                               tzinfo=FixedOffset('-', 11, 30)), 'Date'),
         '1999-10-13T05:03:27-11:30')
     self.assertEqual(
         tag._convert_to_string(
             datetime.datetime(1899,
                               12,
                               31,
                               23,
                               59,
                               59,
                               tzinfo=FixedOffset('+', 5, 30)), 'Date'),
         '1899-12-31T23:59:59+05:30')
     self.assertEqual(
         tag._convert_to_string(
             datetime.datetime(1999, 10, 13, 5, 3, 27, 124300), 'Date'),
         '1999-10-13T05:03:27.1243Z')
     self.assertEqual(
         tag._convert_to_string(
             datetime.datetime(1899, 12, 31, 23, 59, 59, 124300), 'Date'),
         '1899-12-31T23:59:59.1243Z')
     self.assertEqual(
         tag._convert_to_string(
             datetime.datetime(1999,
                               10,
                               13,
                               5,
                               3,
                               27,
                               124300,
                               tzinfo=FixedOffset()), 'Date'),
         '1999-10-13T05:03:27.1243Z')
     self.assertEqual(
         tag._convert_to_string(
             datetime.datetime(1899,
                               12,
                               31,
                               23,
                               59,
                               59,
                               124300,
                               tzinfo=FixedOffset()), 'Date'),
         '1899-12-31T23:59:59.1243Z')
     self.assertEqual(
         tag._convert_to_string(
             datetime.datetime(1999,
                               10,
                               13,
                               5,
                               3,
                               27,
                               124300,
                               tzinfo=FixedOffset('+', 5, 30)), 'Date'),
         '1999-10-13T05:03:27.1243+05:30')
     self.assertEqual(
         tag._convert_to_string(
             datetime.datetime(1999,
                               10,
                               13,
                               5,
                               3,
                               27,
                               124300,
                               tzinfo=FixedOffset('-', 11, 30)), 'Date'),
         '1999-10-13T05:03:27.1243-11:30')
     self.assertEqual(
         tag._convert_to_string(
             datetime.datetime(1899,
                               12,
                               31,
                               23,
                               59,
                               59,
                               124300,
                               tzinfo=FixedOffset('+', 5, 30)), 'Date'),
         '1899-12-31T23:59:59.1243+05:30')
     # Invalid values
     self.assertRaises(XmpValueError, tag._convert_to_string, 'invalid',
                       'Date')
     self.assertRaises(XmpValueError, tag._convert_to_string, None, 'Date')
Ejemplo n.º 14
0
    def _convert_to_python(self, value, type_):
        """Convert a raw value to its corresponding python type.

        Args:
        value -- the raw value to be converted
        type_ -- the simple type of the raw value

        Return: the value converted to its corresponding python type

        Raise XmpValueError: if the conversion fails
        """
        if type_ == 'Boolean':
            if value == 'True':
                return True

            elif value == 'False':
                return False

            else:
                raise XmpValueError(value, type_)

        elif type_ == 'Colorant':
            # TODO
            raise NotImplementedError('XMP conversion for type [%s]' % type_)

        elif type_ == 'Date':
            match = self._date_re.match(value)
            if match is None:
                raise XmpValueError(value, type_)

            gd = match.groupdict()
            if gd['month'] is not None:
                month = int(gd['month'])

            else:
                month = 1

            if gd['day'] is not None:
                day = int(gd['day'])

            else:
                day = 1

            if gd['time'] is None:
                try:
                    return datetime.date(int(gd['year']), month, day)
                except ValueError:
                    raise XmpValueError(value, type_)

            else:
                if gd['minutes'] is None:
                    # Malformed time
                    raise XmpValueError(value, type_)

                if gd['seconds'] is not None:
                    seconds = int(gd['seconds'])

                else:
                    seconds = 0

                if gd['decimal'] is not None:
                    microseconds = int(float('0.%s' % gd['decimal']) * 1E6)

                else:
                    microseconds = 0

                if gd['tzd'] is not None and gd[
                        'tzd'] == 'Z' and gd['sign'] is None and gd[
                            'ohours'] is None and gd['ominutes'] is None:
                    tzinfo = FixedOffset()

                elif gd['sign'] is not None and gd[
                        'ohours'] is not None and gd['ominutes'] is not None:
                    tzinfo = FixedOffset(gd['sign'], int(gd['ohours']),
                                         int(gd['ominutes']))
                else:
                    tzinfo = None
                    # TODO: Decide if following is way to hande
                    # ======================================================================
                    # ERROR: test_convert_to_python_date (xmp.TestXmpTag)
                    # ----------------------------------------------------------------------
                    # Traceback (most recent call last):
                    #   File "c:\Users\BSFau\Cloudstation\BSFsoftDev\axternal\py3exiv2\py3exiv2\test\xmp.py", line 91, in test_convert_to_python_date
                    #     datetime.datetime(1999, 10, 13, 5, 3, tzinfo=FixedOffset()),
                    # TypeError: can't subtract offset-naive and offset-aware datetimes
                    # ----------------------------------------------------------------------

                    # u_tm = datetime.datetime.utcfromtimestamp(0)
                    # l_tm = datetime.datetime.fromtimestamp(0)
                    # tzinfo = datetime.timezone(l_tm - u_tm)
                try:
                    if tzinfo is not None:
                        return datetime.datetime(int(gd['year']), month, day,
                                                 int(gd['hours']),
                                                 int(gd['minutes']), seconds,
                                                 microseconds, tzinfo)
                    else:
                        return datetime.datetime(int(gd['year']), month, day,
                                                 int(gd['hours']),
                                                 int(gd['minutes']), seconds,
                                                 microseconds)

                except ValueError:
                    raise XmpValueError(value, type_)

        elif type_ == 'Dimensions':
            # TODO
            raise NotImplementedError('XMP conversion for type [%s]' % type_)

        elif type_ == 'Font':
            # TODO
            raise NotImplementedError('XMP conversion for type [%s]' % type_)

        elif type_ == 'GPSCoordinate':
            try:
                return GPSCoordinate.from_string(value)
            except ValueError:
                raise XmpValueError(value, type_)

        elif type_ == 'Integer':
            try:
                return int(value)
            except ValueError:
                raise XmpValueError(value, type_)

        elif type_ == 'Locale':
            # TODO
            # See RFC 3066
            raise NotImplementedError('XMP conversion for type [%s]' % type_)

        elif type_ == 'MIMEType':
            if value.count('/') != 1:
                raise XmpValueError(value, type_)
            try:
                return tuple(value.split('/', 1))
            except ValueError:
                raise XmpValueError(value, type_)

        elif type_ == 'Rational':
            try:
                return make_fraction(value)
            except (ValueError, ZeroDivisionError):
                raise XmpValueError(value, type_)

        elif type_ == 'Real':
            # TODO
            raise NotImplementedError('XMP conversion for type [%s]' % type_)

        elif type_ in ('AgentName', 'ProperName', 'Text'):
            if isinstance(value, bytes):
                try:
                    value = str(value, 'utf-8')
                except TypeError:
                    raise XmpValueError(value, type_)
            return value

        elif type_ == 'Thumbnail':
            # TODO
            raise NotImplementedError('XMP conversion for type [%s]' % type_)

        elif type_ in ('URI', 'URL'):
            if isinstance(value, bytes):
                try:
                    value = value.decode('utf-8')
                except UnicodeDecodeError:
                    # Unknow encoding, return the raw value
                    pass
            return value

        elif type_ == 'XPath':
            # TODO
            raise NotImplementedError('XMP conversion for type [%s]' % type_)

        raise NotImplementedError('XMP conversion for type [%s]' % type_)
Ejemplo n.º 15
0
def T(h=0, mi=0, s=0, us=0, tz=()):
    if tz is None:
        return datetime.time(h, mi, s, us)
    else:
        return datetime.time(h, mi, s, us, tzinfo=FixedOffset(*tz))