def encodedata(data, encoding):
    """ Encode data using the given encoding.

        Possible values for encoding include:
        'base64'   - BASE 64 encoding
        'hex'      - HEX encoding (no line breaks)
        'hexlines' - HEX encoding (with CR line breaks)

        In Python 2.0 and up, encoding may also be an encoding
        supported natively by Python via the codec registry.
    
    """
    encoding = encoding.lower()

    if encoding == 'base64':
        import base64
        return base64.encodestring(data)

    elif encoding == 'hex' or \
         encoding == 'hexlines':
        from mx.TextTools import str2hex
        result = str2hex(data)
        if encoding == 'hexlines':
            return _addlinebreaks(result, 72)
        return result

    else:
        # This works in Python >=2.0 only
        try:
            return data.encode(encoding)
        except AttributeError:
            raise ValueError, 'unknown encoding "%s"' % encoding
Пример #2
0
def encodedata(data, encoding):

    """ Encode data using the given encoding.

        Possible values for encoding include:
        'base64'   - BASE 64 encoding
        'hex'      - HEX encoding (no line breaks)
        'hexlines' - HEX encoding (with CR line breaks)

        In Python 2.0 and up, encoding may also be an encoding
        supported natively by Python via the codec registry.
    
    """
    encoding = encoding.lower()
    
    if encoding == 'base64':
        import base64
        return base64.encodestring(data)
    
    elif encoding == 'hex' or \
         encoding == 'hexlines':
        from mx.TextTools import str2hex
        result = str2hex(data)
        if encoding == 'hexlines':
            return _addlinebreaks(result, 72)
        return result

    else:
        # This works in Python >=2.0 only
        try:
            return data.encode(encoding)
        except AttributeError:
            raise ValueError, 'unknown encoding "%s"' % encoding
Пример #3
0
def encodedata(data, encoding,

               lower=string.lower):

    """ Encode data using the given encoding.

        Possible values for encoding include:
        'base64'   - BASE 64 encoding
        'hex'      - HEX encoding (no line breaks)
        'hexlines' - HEX encoding (with CR line breaks)

        In Python 2.0 and up, encoding may also be an encoding
        supported natively by Python via the codec registry.
    
    """
    encoding = lower(encoding)
    
    if encoding == 'base64':
        import base64
        return base64.encodestring(data)
    
    elif encoding == 'hex' or \
         encoding == 'hexlines':
        from mx.TextTools import str2hex
        import cStringIO
        result = str2hex(data)
        if encoding == 'hexlines':
            return _addlinebreaks(result, 72)
        return result

    elif encoding == 'uu':
        import binascii
        
        out_file.write('begin %o %s\n' % ((mode&0777),name))
        str = in_file.read(45)
        while len(str) > 0:
            out_file.write(binascii.b2a_uu(str))
            str = in_file.read(45)
        out_file.write(' \nend\n')

        return base64.encodestring(data)
    
    else:
        # This works in Python >=2.0 only
        try:
            return data.encode(encoding)
        except AttributeError:
            raise ValueError, 'unknown encoding "%s"' % encoding
Пример #4
0
def encodedata(data, encoding,

               lower=string.lower):

    """ Encode data using the given encoding.

        Possible values for encoding include:
        'base64'   - BASE 64 encoding
        'hex'      - HEX encoding (no line breaks)
        'hexlines' - HEX encoding (with CR line breaks)

        In Python 2.0 and up, encoding may also be an encoding
        supported natively by Python via the codec registry.
    
    """
    encoding = lower(encoding)
    
    if encoding == 'base64':
        import base64
        return base64.encodestring(data)
    
    elif encoding == 'hex' or \
         encoding == 'hexlines':
        from mx.TextTools import str2hex
        import cStringIO
        result = str2hex(data)
        if encoding == 'hexlines':
            return _addlinebreaks(result, 72)
        return result

    elif encoding == 'uu':
        import binascii
        
        out_file.write('begin %o %s\n' % ((mode&0777),name))
        str = in_file.read(45)
        while len(str) > 0:
            out_file.write(binascii.b2a_uu(str))
            str = in_file.read(45)
        out_file.write(' \nend\n')

        return base64.encodestring(data)
    
    else:
        # This works in Python >=2.0 only
        try:
            return data.encode(encoding)
        except AttributeError:
            raise ValueError, 'unknown encoding "%s"' % encoding