예제 #1
0
파일: exif.py 프로젝트: Namejs/workr
    def _convert_to_string(self, value):
        """
        Convert one value to its corresponding string representation, suitable
        to pass to libexiv2.

        :param value: the value to be converted

        :return: the value converted to its corresponding string representation
        :rtype: string

        :raise ExifValueError: if the conversion fails
        """
        if self.type == 'Ascii':
            if isinstance(value, datetime.datetime):
                return value.strftime(self._datetime_formats[0])
            elif isinstance(value, datetime.date):
                if self.key == 'Exif.GPSInfo.GPSDateStamp':
                    # Special case
                    return value.strftime(self._date_formats[0])
                else:
                    return value.strftime('%s 00:00:00' % self._date_formats[0])
            elif isinstance(value, unicode):
                try:
                    return value.encode('utf-8')
                except UnicodeEncodeError:
                    raise ExifValueError(value, self.type)
            elif isinstance(value, str):
                return value
            else:
                raise ExifValueError(value, self.type)

        elif self.type in ('Byte', 'SByte'):
            if isinstance(value, unicode):
                try:
                    return value.encode('utf-8')
                except UnicodeEncodeError:
                    raise ExifValueError(value, self.type)
            elif isinstance(value, str):
                return value
            else:
                raise ExifValueError(value, self.type)

        elif self.type == 'Comment':
            if value is not None and self.raw_value is not None and \
                self.raw_value.startswith('charset='):
                charset, val = self.raw_value.split(' ', 1)
                charset = charset.split('=')[1].strip('"')
                encoding = self._match_encoding(charset)
                try:
                    val = value.encode(encoding)
                except UnicodeError:
                    # Best effort, do not fail just because the original
                    # encoding of the tag cannot encode the new value.
                    pass
                else:
                    return 'charset="%s" %s' % (charset, val)

            if isinstance(value, unicode):
                try:
                    return value.encode('utf-8')
                except UnicodeEncodeError:
                    raise ExifValueError(value, self.type)
            elif isinstance(value, str):
                return value
            else:
                raise ExifValueError(value, self.type)

        elif self.type == 'Short':
            if isinstance(value, int) and value >= 0:
                return str(value)
            else:
                raise ExifValueError(value, self.type)

        elif self.type == 'SShort':
            if isinstance(value, int):
                return str(value)
            else:
                raise ExifValueError(value, self.type)

        elif self.type == 'Long':
            if isinstance(value, (int, long)) and value >= 0:
                return str(value)
            else:
                raise ExifValueError(value, self.type)

        elif self.type == 'SLong':
            if isinstance(value, (int, long)):
                return str(value)
            else:
                raise ExifValueError(value, self.type)

        elif self.type == 'Rational':
            if is_fraction(value) and value.numerator >= 0:
                return fraction_to_string(value)
            else:
                raise ExifValueError(value, self.type)

        elif self.type == 'SRational':
            if is_fraction(value):
                return fraction_to_string(value)
            else:
                raise ExifValueError(value, self.type)

        elif self.type == 'Undefined':
            if isinstance(value, unicode):
                try:
                    return string_to_undefined(value.encode('utf-8'))
                except UnicodeEncodeError:
                    raise ExifValueError(value, self.type)
            elif isinstance(value, str):
                return string_to_undefined(value)
            else:
                raise ExifValueError(value, self.type)

        raise ExifValueError(value, self.type)
예제 #2
0
 def test_fraction_to_string(self):
     self.assertEqual(fraction_to_string(make_fraction(3, 5)), '3/5')
     self.assertEqual(fraction_to_string(make_fraction(-3, 5)), '-3/5')
     self.assertEqual(fraction_to_string(make_fraction(0, 1)), '0/1')
     self.assertRaises(TypeError, fraction_to_string, None)
     self.assertRaises(TypeError, fraction_to_string, 'invalid')
예제 #3
0
파일: utils.py 프로젝트: Namejs/workr
 def test_fraction_to_string(self):
     self.assertEqual(fraction_to_string(make_fraction(3, 5)), '3/5')
     self.assertEqual(fraction_to_string(make_fraction(-3, 5)), '-3/5')
     self.assertEqual(fraction_to_string(make_fraction(0, 1)), '0/1')
     self.assertRaises(TypeError, fraction_to_string, None)
     self.assertRaises(TypeError, fraction_to_string, 'invalid')
예제 #4
0
파일: exif.py 프로젝트: enaut/py3exiv2
    def _convert_to_string(self, value):
        """
        Convert one value to its corresponding string representation, suitable
        to pass to libexiv2.

        :param value: the value to be converted

        :return: the value converted to its corresponding string representation
        :rtype: string

        :raise ExifValueError: if the conversion fails
        """
        if self.type == 'Ascii':
            if isinstance(value, datetime.datetime):
                return DateTimeFormatter.exif(value)

            elif isinstance(value, datetime.date):
                if self.key == 'Exif.GPSInfo.GPSDateStamp':
                    # Special case
                    return DateTimeFormatter.exif(value)

                else:
                    return '%s 00:00:00' % DateTimeFormatter.exif(value)

            else:
                return value

        elif self.type in ('Byte', 'SByte'):
            if isinstance(value, str):
                try:
                    return value.encode('utf-8')
                except UnicodeEncodeError:
                    raise ExifValueError(value, self.type)

            elif isinstance(value, bytes):
                return value

            else:
                raise ExifValueError(value, self.type)

        elif self.type == 'Comment':
            return self._convert_to_bytes(value)

        elif self.type == 'Short':
            if isinstance(value, int) and value >= 0:
                return str(value)

            else:
                raise ExifValueError(value, self.type)

        elif self.type == 'SShort':
            if isinstance(value, int):
                return str(value)

            else:
                raise ExifValueError(value, self.type)

        elif self.type == 'Long':
            if isinstance(value, int) and value >= 0:
                return str(value)

            else:
                raise ExifValueError(value, self.type)

        elif self.type == 'SLong':
            if isinstance(value, int):
                return str(value)

            else:
                raise ExifValueError(value, self.type)

        elif self.type == 'Rational':
            if is_fraction(value) and value.numerator >= 0:
                return fraction_to_string(value)

            else:
                raise ExifValueError(value, self.type)

        elif self.type == 'SRational':
            if is_fraction(value):
                return fraction_to_string(value)

            else:
                raise ExifValueError(value, self.type)

        elif self.type == 'Undefined':
            if isinstance(value, str):
                try:
                    return string_to_undefined(value)
                except UnicodeEncodeError:
                    raise ExifValueError(value, self.type)

            elif isinstance(value, bytes):
                return string_to_undefined(value)

            else:
                raise ExifValueError(value, self.type)

        raise ExifValueError(value, self.type)
예제 #5
0
파일: test_utils.py 프로젝트: Czaki/cyexiv2
def test_fraction_to_string_invalid(bad_args):
    with pytest.raises(TypeError):
        fraction_to_string(*bad_args)
예제 #6
0
파일: test_utils.py 프로젝트: Czaki/cyexiv2
def test_fraction_to_string_valid(frac, as_str):
    assert fraction_to_string(Fraction(*frac)) == as_str
예제 #7
0
    def _convert_to_string(self, value):
        """
        Convert one value to its corresponding string representation, suitable
        to pass to libexiv2.

        :param value: the value to be converted

        :return: the value converted to its corresponding string representation
        :rtype: string

        :raise ExifValueError: if the conversion fails
        """
        if self.type == 'Ascii':
            if isinstance(value, datetime.datetime):
                return DateTimeFormatter.exif(value)
            elif isinstance(value, datetime.date):
                if self.key == 'Exif.GPSInfo.GPSDateStamp':
                    # Special case
                    return DateTimeFormatter.exif(value)
                else:
                    return '%s 00:00:00' % DateTimeFormatter.exif(value)
            elif isinstance(value, unicode):
                try:
                    return value.encode('utf-8')
                except UnicodeEncodeError:
                    raise ExifValueError(value, self.type)
            elif isinstance(value, str):
                return value
            else:
                raise ExifValueError(value, self.type)

        elif self.type in ('Byte', 'SByte'):
            if isinstance(value, unicode):
                try:
                    return value.encode('utf-8')
                except UnicodeEncodeError:
                    raise ExifValueError(value, self.type)
            elif isinstance(value, str):
                return value
            else:
                raise ExifValueError(value, self.type)

        elif self.type == 'Comment':
            if value is not None and self.raw_value is not None and \
                self.raw_value.startswith('charset='):
                charset, val = self.raw_value.split(' ', 1)
                charset = charset.split('=')[1].strip('"')
                encoding = self._match_encoding(charset)
                try:
                    val = value.encode(encoding)
                except UnicodeError:
                    # Best effort, do not fail just because the original
                    # encoding of the tag cannot encode the new value.
                    pass
                else:
                    return 'charset="%s" %s' % (charset, val)

            if isinstance(value, unicode):
                try:
                    return value.encode('utf-8')
                except UnicodeEncodeError:
                    raise ExifValueError(value, self.type)
            elif isinstance(value, str):
                return value
            else:
                raise ExifValueError(value, self.type)

        elif self.type == 'Short':
            if isinstance(value, int) and value >= 0:
                return str(value)
            else:
                raise ExifValueError(value, self.type)

        elif self.type == 'SShort':
            if isinstance(value, int):
                return str(value)
            else:
                raise ExifValueError(value, self.type)

        elif self.type == 'Long':
            if isinstance(value, (int, long)) and value >= 0:
                return str(value)
            else:
                raise ExifValueError(value, self.type)

        elif self.type == 'SLong':
            if isinstance(value, (int, long)):
                return str(value)
            else:
                raise ExifValueError(value, self.type)

        elif self.type == 'Rational':
            if is_fraction(value) and value.numerator >= 0:
                return fraction_to_string(value)
            else:
                raise ExifValueError(value, self.type)

        elif self.type == 'SRational':
            if is_fraction(value):
                return fraction_to_string(value)
            else:
                raise ExifValueError(value, self.type)

        elif self.type == 'Undefined':
            if isinstance(value, unicode):
                try:
                    return string_to_undefined(value.encode('utf-8'))
                except UnicodeEncodeError:
                    raise ExifValueError(value, self.type)
            elif isinstance(value, str):
                return string_to_undefined(value)
            else:
                raise ExifValueError(value, self.type)

        raise ExifValueError(value, self.type)