Esempio n. 1
0
    def _reencode_if_necessary(self, message, output_encoding):
        '''Return a byte string that's valid in a specific charset.

        .. warning:: This method may mangle the message if the inpput encoding
            is not known or the message isn't represntable in the chosen
            output encoding.
        '''
        valid = False
        msg = None
        try:
            valid = byte_string_valid_encoding(message, output_encoding)
        except TypeError:
            # input was unicode, so it needs to be encoded
            pass

        if valid:
            return message
        try:
            # Decode to unicode so we can re-encode to desired encoding
            msg = to_unicode(message,
                             encoding=self.input_charset,
                             nonstring='strict')
        except TypeError:
            # Not a string; return an empty byte string
            return ''

        # Make sure that we're returning a str of the desired encoding
        return to_bytes(msg, encoding=output_encoding)
Esempio n. 2
0
    def _reencode_if_necessary(self, message, output_encoding):
        '''Return a byte string that's valid in a specific charset.

        .. warning:: This method may mangle the message if the inpput encoding
            is not known or the message isn't represntable in the chosen
            output encoding.
        '''
        valid = False
        msg = None
        try:
            valid = byte_string_valid_encoding(message, output_encoding)
        except TypeError:
            # input was unicode, so it needs to be encoded
            pass

        if valid:
            return message
        try:
            # Decode to unicode so we can re-encode to desired encoding
            msg = to_unicode(message, encoding=self.input_charset,
                    nonstring='strict')
        except TypeError:
            # Not a string; return an empty byte string
            return ''

        # Make sure that we're returning a str of the desired encoding
        return to_bytes(msg, encoding=output_encoding)
Esempio n. 3
0
def utf8_valid(msg):
    '''**Deprecated** Detect if a string is valid :term:`utf-8`

    Use :func:`kitchen.text.misc.byte_string_valid_encoding` instead.
    '''
    warnings.warn('kitchen.text.utf8.utf8_valid is deprecated.  Use'
            ' kitchen.text.misc.byte_string_valid_encoding(msg) instead',
            DeprecationWarning, stacklevel=2)
    return byte_string_valid_encoding(msg)
Esempio n. 4
0
 def test_byte_string_invalid_encoding(self):
     '''Test that we return False with non-encoded chars'''
     tools.ok_(misc.byte_string_valid_encoding('\xff') == False)
     tools.ok_(
         misc.byte_string_valid_encoding(self.euc_jp_japanese) == False)
Esempio n. 5
0
 def test_byte_string_valid_encoding(self):
     '''Test that a byte sequence is validated'''
     tools.ok_(misc.byte_string_valid_encoding(self.utf8_japanese) == True)
     tools.ok_(
         misc.byte_string_valid_encoding(self.euc_jp_japanese,
                                         encoding='euc_jp') == True)
Esempio n. 6
0
 def test_byte_string_invalid_encoding(self):
     '''Test that we return False with non-encoded chars'''
     tools.ok_(misc.byte_string_valid_encoding(b'\xff') == False)
     tools.ok_(misc.byte_string_valid_encoding(self.euc_jp_japanese) == False)
Esempio n. 7
0
 def test_byte_string_valid_encoding(self):
     '''Test that a byte sequence is validated'''
     tools.ok_(misc.byte_string_valid_encoding(self.utf8_japanese) == True)
     tools.ok_(misc.byte_string_valid_encoding(self.euc_jp_japanese, encoding='euc_jp') == True)