Exemplo n.º 1
0
def gsmcodec(name):
    #=========================================================================#
    if name == "gsm_default":
        return CodecInfo(gsm_default_encode,
                         gsm_default_decode,
                         name="gsm_default")
    elif name == "gsm_ucs2":
        return CodecInfo(UnicodeToucs2hex, ucs2hexToUnicode, name="gsm_ucs2")
Exemplo n.º 2
0
def nmea_search(encoding):
    if encoding == 'nmea_sentence':
        codec = NMEASentenceCodec()
        return CodecInfo(codec.encode, codec.decode, name=encoding)
    if encoding == 'nmea_characters':
        codec = NMEACharacterCodec()
        return CodecInfo(codec.encode, None, name=encoding)
    return None
Exemplo n.º 3
0
def ucs2(name):
    if name.lower() not in ("ucs2", "ucs-2"):
        return None

    def ucs2_encode(data, errors="replace"):
        data = u"".join(i if ord(i) < 65536 else u"?" for i in data)
        return utf_16_be_encode(data, errors)

    ucs2_decode = utf_16_be_decode

    class UCS2IncrementalEncoder(IncrementalEncoder):
        def encode(self, input, final=False):
            return ucs2_encode(input, self.errors)[0]

    class UCS2IncrementalDecoder(BufferedIncrementalDecoder):
        _buffer_decode = ucs2_decode

    class UCS2StreamWriter(StreamWriter):
        encode = ucs2_encode

    class UCS2StreamReader(StreamReader):
        decode = ucs2_decode

    return CodecInfo(
        name="ucs2",
        encode=ucs2_encode,
        decode=ucs2_decode,
        incrementalencoder=UCS2IncrementalEncoder,
        incrementaldecoder=UCS2IncrementalDecoder,
        streamwriter=UCS2StreamWriter,
        streamreader=UCS2StreamReader,
    )
Exemplo n.º 4
0
 def lookup(name):
     if name == "big5-uao" or name == "big5uao" or name == "big5_uao":
         return CodecInfo(
             Big5UAOCodec().encode,
             Big5UAOCodec().decode,
             name="big5-uao"
         )
Exemplo n.º 5
0
def search_function(encoding):
    if encoding != 'quasiquotes':
        return None

    return CodecInfo(
        name='quasiquotes',
        encode=utf8.encode,
        decode=decode,
        incrementalencoder=utf8.incrementalencoder,
        incrementaldecoder=IncrementalDecoder,
        streamreader=StreamReader,
        streamwriter=utf8.streamwriter,
    )
Exemplo n.º 6
0
    def get_codec_info(cls) -> CodecInfo:
        """
        Returns information used by the codecs library to configure the
        codec for use.
        """
        codec = cls()

        codec_info = {
            'encode': codec.encode,
            'decode': codec.decode,

            # In Python 2, all codecs are made equal.
            # In Python 3, some codecs are more equal than others.
            '_is_text_encoding': False
        }

        return CodecInfo(**codec_info)
Exemplo n.º 7
0
 def open(self):
     if self.stream:
         return
     dir = os.path.dirname(self.filename)
     if not os.path.exists(dir):
         os.makedirs(dir)
     mode = self.mode
     if 'b' not in mode:
         mode += 'b'
     self.stream = open(self.filename, mode)
     if 'a' in mode:
         self.size = os.path.getsize(self.filename)
     else:
         self.size = 0
     if not self.codecInfo or self.encoding != self.codecInfo.name:
         self.codecInfo = searchCodecInfo(self.encoding)
         if self.codecInfo:
             self.encoding = self.codecInfo.name
         else:
             self.codecInfo = CodecInfo(name=self.encoding,
                                        encode=self._encode,
                                        decode=None)
Exemplo n.º 8
0
            return decode(inp, errors)
        else:
            return '', 0


class SyntaxExtensionStreamReader(utf_8.StreamReader, object):
    """decode is deferred to support better error messages"""
    _stream = None
    _decoded = False

    @property
    def stream(self):
        if not self._decoded:
            text, _ = decode(self._stream.read())
            self._stream = BytesIO(text.encode('UTF-8'))
            self._decoded = True
        return self._stream

    @stream.setter
    def stream(self, stream):
        self._stream = stream
        self._decoded = False


codec_info = CodecInfo(encode=utf_8.encode,
                       decode=decode,
                       streamreader=SyntaxExtensionStreamReader,
                       streamwriter=utf_8.StreamWriter,
                       incrementalencoder=utf_8.IncrementalEncoder,
                       incrementaldecoder=SyntaxExtensionIncrementalDecoder,
                       name='syntax-extensions')
Exemplo n.º 9
0
def rotx_codec_generator(rotval):
    name = "rot%d"  % rotval
    rx_enc = lambda data: (rotx(data, rotval), len(data))
    rx_dec = lambda data: (rotx(data, -rotval), len(data))
    return CodecInfo(name=name, encode=rx_enc, decode=rx_dec)
Exemplo n.º 10
0
    #we're going to ignore parity for now
    print [chr(int(c[:0:-1], 2)+48) for c in blocks(input, 5)]
    output = ''.join(chr(int(c[:0:-1], 2)+48) for c in blocks(input, 5))
    output = output[-1:]
    return output, len(input)





###############################################################################
# Codec Registration
###############################################################################

CODECS_IN_FILE = {"morse": CodecInfo(name='morse',
                                     encode=morse_encode,
                                     decode=morse_decode),
                  "bin": CodecInfo(name='bin',
                                   encode=bin_encode,
                                   decode=bin_decode),
                  "url": CodecInfo(name='url',
                                   encode=url_encode,
                                   decode=url_decode),
                  "entity": CodecInfo(name='entity',
                                   encode=entity_encode,
                                   decode=entity_decode),
                  "entityhex": CodecInfo(name='entityhex',
                                   encode=entity_encode_hex,
                                   decode=entity_decode_hex),
                  "ascii85": CodecInfo(name='ascii85',
                                       encode=ascii85_encode,
Exemplo n.º 11
0
    assert not len_in % 5, "Input must be divisible by 5"
    assert not len_in > (5 * 40), "String too long: cannot be ABA Track 2"
    #we're going to ignore parity for now
    print[chr(int(c[:0:-1], 2) + 48) for c in blocks(input, 5)]
    output = ''.join(chr(int(c[:0:-1], 2) + 48) for c in blocks(input, 5))
    output = output[-1:]
    return output, len(input)


###############################################################################
# Codec Registration
###############################################################################

CODECS_IN_FILE = {
    "morse":
    CodecInfo(name='morse', encode=morse_encode, decode=morse_decode),
    "bin":
    CodecInfo(name='bin', encode=bin_encode, decode=bin_decode),
    "url":
    CodecInfo(name='url', encode=url_encode, decode=url_decode),
    "entity":
    CodecInfo(name='entity', encode=entity_encode, decode=entity_decode),
    "entityhex":
    CodecInfo(name='entityhex',
              encode=entity_encode_hex,
              decode=entity_decode_hex),
    "ascii85":
    CodecInfo(name='ascii85', encode=ascii85_encode, decode=ascii85_decode),
    "yenc":
    CodecInfo(name='yenc', encode=y_encode, decode=y_decode),
}