~osomon/pyexiv2/pyexiv2-0.3

« back to all changes in this revision

Viewing changes to src/pyexiv2.py

  • Committer: Olivier Tilloy
  • Author(s): Olivier Tilloy
  • Date: 2009-10-08 07:33:01 UTC
  • Revision ID: olivier@fluendo.com-20091008073301-7y7lf8xlr3c0ktjj
Make IptcTag._convert_to_string an instance method.

Show diffs side-by-side

added added

removed removed

Lines of Context:
786
786
 
787
787
    def _set_values(self, new_values):
788
788
        if self.metadata is not None:
789
 
            raw_values = map(lambda x: IptcTag._convert_to_string(x, self.type), new_values)
 
789
            raw_values = map(self._convert_to_string, new_values)
790
790
            self.metadata._set_iptc_tag_values(self.key, raw_values)
791
791
        # Make values a notifying list if needed
792
792
        if isinstance(new_values, NotifyingList):
815
815
 
816
816
    def _convert_to_python(self, value):
817
817
        """
818
 
        Convert a raw value to its corresponding python type.
 
818
        Convert one raw value to its corresponding python type.
819
819
 
820
820
        @param value: the raw value to be converted
821
821
        @type value:  C{str}
875
875
 
876
876
        raise IptcValueError(value, self.type)
877
877
 
878
 
    @staticmethod
879
 
    def _convert_to_string(value, xtype):
 
878
    def _convert_to_string(self, value):
880
879
        """
881
 
        Convert a value to its corresponding string representation, suitable to
882
 
        pass to libexiv2.
 
880
        Convert one value to its corresponding string representation, suitable
 
881
        to pass to libexiv2.
883
882
 
884
883
        @param value: the value to be converted
885
 
        @type value:  depends on xtype (DOCME)
886
 
        @param xtype: the IPTC type of the value
887
 
        @type xtype:  C{str}
 
884
        @type value:  depends on C{self.type} (DOCME)
888
885
 
889
886
        @return: the value converted to its corresponding string representation
890
887
        @rtype:  C{str}
891
888
 
892
889
        @raise IptcValueError: if the conversion fails
893
890
        """
894
 
        if xtype == 'Short':
 
891
        if self.type == 'Short':
895
892
            if type(value) is int:
896
893
                return str(value)
897
894
            else:
898
 
                raise IptcValueError(value, xtype)
 
895
                raise IptcValueError(value, self.type)
899
896
 
900
 
        elif xtype == 'String':
 
897
        elif self.type == 'String':
901
898
            if type(value) is unicode:
902
899
                try:
903
900
                    return value.encode('utf-8')
904
901
                except UnicodeEncodeError:
905
 
                    raise IptcValueError(value, xtype)
 
902
                    raise IptcValueError(value, self.type)
906
903
            elif type(value) is str:
907
904
                return value
908
905
            else:
909
 
                raise IptcValueError(value, xtype)
 
906
                raise IptcValueError(value, self.type)
910
907
 
911
 
        elif xtype == 'Date':
 
908
        elif self.type == 'Date':
912
909
            if type(value) in (datetime.date, datetime.datetime):
913
910
                # ISO 8601 date format
914
911
                return value.strftime('%Y%m%d')
915
912
            else:
916
 
                raise IptcValueError(value, xtype)
 
913
                raise IptcValueError(value, self.type)
917
914
 
918
 
        elif xtype == 'Time':
 
915
        elif self.type == 'Time':
919
916
            if type(value) in (datetime.time, datetime.datetime):
920
917
                r = value.strftime('%H%M%S')
921
918
                if value.tzinfo is not None:
924
921
                    r += '+0000'
925
922
                return r
926
923
            else:
927
 
                raise IptcValueError(value, xtype)
 
924
                raise IptcValueError(value, self.type)
928
925
 
929
 
        elif xtype == 'Undefined':
 
926
        elif self.type == 'Undefined':
930
927
            if type(value) is str:
931
928
                return value
932
929
            else:
933
 
                raise IptcValueError(value, xtype)
 
930
                raise IptcValueError(value, self.type)
934
931
 
935
 
        raise IptcValueError(value, xtype)
 
932
        raise IptcValueError(value, self.type)
936
933
 
937
934
    def to_string(self):
938
935
        """
941
938
 
942
939
        @rtype: C{list} of C{str}
943
940
        """
944
 
        return map(lambda x: IptcTag._convert_to_string(x, self.type),
945
 
                   self.values)
 
941
        return map(self._convert_to_string, self.values)
946
942
 
947
943
    def __str__(self):
948
944
        """