~osomon/pyexiv2/pyexiv2-0.3

« back to all changes in this revision

Viewing changes to src/pyexiv2.py

  • Committer: Olivier Tilloy
  • Date: 2009-07-31 15:26:19 UTC
  • Revision ID: olivier@tilloy.net-20090731152619-wzcut0e5oq2la13v
Make ExifTag._convert_to_python an instance method.

Show diffs side-by-side

added added

removed removed

Lines of Context:
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'):
 
483
        self._init_values()
 
484
 
 
485
    def _init_values(self):
 
486
        # Initial conversion of the raw values to their corresponding python
 
487
        # types.
 
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:
487
 
                self._value = \
488
 
                    map(lambda x: ExifTag._convert_to_python(x, type, fvalue), values)
 
492
                self._value = map(self._convert_to_python, values)
489
493
                return
490
 
        self._value = ExifTag._convert_to_python(value, type, fvalue)
 
494
        self._value = self._convert_to_python(self.raw_value)
491
495
 
492
496
    def _get_value(self):
493
497
        return self._value
507
511
    value = property(fget=_get_value, fset=_set_value, fdel=_del_value,
508
512
                     doc=None)
509
513
 
510
 
    @staticmethod
511
 
    def _convert_to_python(value, xtype, fvalue):
 
514
    def _convert_to_python(self, value):
512
515
        """
513
516
        Convert a raw value to its corresponding python type.
514
517
 
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
518
 
        @type xtype:   C{str}
519
 
        @param fvalue: the value formatted as a human-readable string by exiv2
520
 
        @type fvalue:  C{str}
521
520
 
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)
524
523
 
525
524
        @raise ExifValueError: if the conversion fails
526
525
        """
527
 
        if xtype == 'Ascii':
 
526
        if self.type == 'Ascii':
528
527
            # The value may contain a Datetime
529
528
            for format in ExifTag._datetime_formats:
530
529
                try:
537
536
            try:
538
537
                return unicode(value, 'utf-8')
539
538
            except TypeError:
540
 
                raise ExifValueError(value, xtype)
 
539
                raise ExifValueError(value, self.type)
541
540
 
542
 
        elif xtype == 'Byte':
 
541
        elif self.type == 'Byte':
543
542
            return value
544
543
 
545
 
        elif xtype == 'Short':
 
544
        elif self.type == 'Short':
546
545
            try:
547
546
                return int(value)
548
547
            except ValueError:
549
 
                raise ExifValueError(value, xtype)
 
548
                raise ExifValueError(value, self.type)
550
549
 
551
 
        elif xtype in ('Long', 'SLong'):
 
550
        elif self.type in ('Long', 'SLong'):
552
551
            try:
553
552
                return long(value)
554
553
            except ValueError:
555
 
                raise ExifValueError(value, xtype)
 
554
                raise ExifValueError(value, self.type)
556
555
 
557
 
        elif xtype in ('Rational', 'SRational'):
 
556
        elif self.type in ('Rational', 'SRational'):
558
557
            try:
559
558
                r = Rational.from_string(value)
560
559
            except (ValueError, ZeroDivisionError):
561
 
                raise ExifValueError(value, xtype)
 
560
                raise ExifValueError(value, self.type)
562
561
            else:
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)
565
564
                return r
566
565
 
567
 
        elif xtype == 'Undefined':
 
566
        elif self.type == 'Undefined':
568
567
            try:
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)
572
571
 
573
 
        raise ExifValueError(value, xtype)
 
572
        raise ExifValueError(value, self.type)
574
573
 
575
574
    @staticmethod
576
575
    def _convert_to_string(value, xtype):