From db42f40048f51cb9a4672372d07f52d808b364ae Mon Sep 17 00:00:00 2001 From: Olivier Tilloy Date: Tue, 20 Jan 2009 09:14:56 +0100 Subject: XMP Date custom regular expression. --- src/pyexiv2.py | 86 ++++++++++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 75 insertions(+), 11 deletions(-) (limited to 'src/pyexiv2.py') diff --git a/src/pyexiv2.py b/src/pyexiv2.py index a013c1f..7fae318 100644 --- a/src/pyexiv2.py +++ b/src/pyexiv2.py @@ -357,6 +357,7 @@ class MetadataTag(object): self.label = label self.description = description self.type = type + self._value = value self.value = value def __str__(self): @@ -368,7 +369,7 @@ class MetadataTag(object): 'Label = ' + self.label + os.linesep + \ 'Description = ' + self.description + os.linesep + \ 'Type = ' + self.type + os.linesep + \ - 'Raw value = ' + str(self.value) + 'Raw value = ' + str(self._value) return r @@ -395,19 +396,19 @@ class ExifTag(MetadataTag): pass elif self.type == 'Ascii': # try to guess if the value is a datetime - self.value = StringToDateTime(self.value) + self.value = StringToDateTime(self._value) elif self.type == 'Short': - self.value = int(self.value) + self.value = int(self._value) elif self.type == 'Long' or self.type == 'SLong': - self.value = long(self.value) + self.value = long(self._value) elif self.type == 'Rational' or self.type == 'SRational': - self.value = StringToRational(self.value) + self.value = StringToRational(self._value) elif self.type == 'Undefined': # self.value is a sequence of bytes whose codes are written as a # string, each code being followed by a blank space (e.g. # "48 50 50 49 " for "0221" in the "Exif.Photo.ExifVersion" tag). try: - self.value = UndefinedToString(self.value) + self.value = UndefinedToString(self._value) except ValueError: # Some tags such as "Exif.Photo.UserComment" are marked as # Undefined but do not store their value as expected. @@ -442,13 +443,13 @@ class IptcTag(MetadataTag): Convert the stored values from strings to the matching Python type. """ if self.type == 'Short': - self.value = map(int, self.value) + self.value = map(int, self._value) elif self.type == 'String': pass elif self.type == 'Date': - self.value = map(StringToDate, self.value) + self.value = map(StringToDate, self._value) elif self.type == 'Time': - self.value = map(StringToTime, self.value) + self.value = map(StringToTime, self._value) elif self.type == 'Undefined': pass @@ -466,6 +467,18 @@ class XmpTag(MetadataTag): An XMP metadata tag can have several values. """ + # Valid dates, to check the correctness of the RE + # valid = ('1999', '1999-10', '1999-10-13', '1999-10-13T05:03Z', '1999-10-13T05:03+06:00', '1999-10-13T05:03-06:00', '1999-10-13T05:03:54Z', '1999-10-13T05:03:54+06:00', '1999-10-13T05:03:54-06:00', '1999-10-13T05:03:54.721Z', '1999-10-13T05:03:54.721+06:00', '1999-10-13T05:03:54.721-06:00') + + # strptime is not flexible enough to handle all valid Date formats, we use a + # custom regular expression + # TODO: restrict the value ranges for year, month, day, hours, minutes, seconds, tzd + _time_zone_re = r'Z|((?P\+|-)(?P\d{2}):(?P\d{2}))' + _time_re = r'(?P\d{2}):(?P\d{2})(:(?P\d{2})(.(?P\d+))?)?(?P%s)' % _time_zone_re + _date_re = re.compile(r'(?P\d{4})(-(?P\d{2})(-(?P\d{2})(T(?P