480
480
super(ExifTag, self).__init__(key, name, label,
481
481
description, type, value)
482
482
self.fvalue = fvalue
483
if type in ('Short', 'Long', 'SLong', 'Rational', 'SRational'):
485
def _init_values(self):
486
# Initial conversion of the raw values to their corresponding python
488
if self.type in ('Short', 'Long', 'SLong', 'Rational', 'SRational'):
484
489
# May contain multiple values
485
values = value.split()
490
values = self.raw_value.split()
486
491
if len(values) > 1:
488
map(lambda x: ExifTag._convert_to_python(x, type, fvalue), values)
492
self._value = map(self._convert_to_python, values)
490
self._value = ExifTag._convert_to_python(value, type, fvalue)
494
self._value = self._convert_to_python(self.raw_value)
492
496
def _get_value(self):
493
497
return self._value
507
511
value = property(fget=_get_value, fset=_set_value, fdel=_del_value,
511
def _convert_to_python(value, xtype, fvalue):
514
def _convert_to_python(self, value):
513
516
Convert a raw value to its corresponding python type.
515
518
@param value: the raw value to be converted
516
519
@type value: C{str}
517
@param xtype: the EXIF type of the value
519
@param fvalue: the value formatted as a human-readable string by exiv2
522
521
@return: the value converted to its corresponding python type
523
@rtype: depends on xtype (DOCME)
522
@rtype: depends on C{self.type} (DOCME)
525
524
@raise ExifValueError: if the conversion fails
526
if self.type == 'Ascii':
528
527
# The value may contain a Datetime
529
528
for format in ExifTag._datetime_formats:
538
537
return unicode(value, 'utf-8')
539
538
except TypeError:
540
raise ExifValueError(value, xtype)
539
raise ExifValueError(value, self.type)
542
elif xtype == 'Byte':
541
elif self.type == 'Byte':
545
elif xtype == 'Short':
544
elif self.type == 'Short':
547
546
return int(value)
548
547
except ValueError:
549
raise ExifValueError(value, xtype)
548
raise ExifValueError(value, self.type)
551
elif xtype in ('Long', 'SLong'):
550
elif self.type in ('Long', 'SLong'):
553
552
return long(value)
554
553
except ValueError:
555
raise ExifValueError(value, xtype)
554
raise ExifValueError(value, self.type)
557
elif xtype in ('Rational', 'SRational'):
556
elif self.type in ('Rational', 'SRational'):
559
558
r = Rational.from_string(value)
560
559
except (ValueError, ZeroDivisionError):
561
raise ExifValueError(value, xtype)
560
raise ExifValueError(value, self.type)
563
if xtype == 'Rational' and r.numerator < 0:
564
raise ExifValueError(value, xtype)
562
if self.type == 'Rational' and r.numerator < 0:
563
raise ExifValueError(value, self.type)
567
elif xtype == 'Undefined':
566
elif self.type == 'Undefined':
569
return unicode(fvalue, 'utf-8')
568
return unicode(self.fvalue, 'utf-8')
570
569
except TypeError:
571
raise ExifValueError(fvalue, xtype)
570
raise ExifValueError(self.fvalue, self.type)
573
raise ExifValueError(value, xtype)
572
raise ExifValueError(value, self.type)
576
575
def _convert_to_string(value, xtype):