Пример #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")
Пример #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
Пример #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,
    )
Пример #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"
         )
Пример #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,
    )
Пример #6
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)
Пример #7
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)
Пример #8
0
class LogFile(object):
    @_lock_logfile
    def __new__(cls,
                filename,
                mode='a',
                encoding=None,
                errors='backslashreplace',
                maxsize=float('inf'),
                rotation=1):
        '''
        :param filename: if the string is not an absolute path, it will
                         converted with os.path.abspath.
        :param mode:     if the mode not allowed write, a ValueError will be raise.
        :param encoding: if not set, use locale.getpreferredencoding(False).
        :param maxsize:  max file size of log file.
        :param rotation: max number of log file's rotation.
        '''
        mode.replace('x', 'w')
        if 'a' not in mode:
            if 'w' not in mode and '+' not in mode:
                raise ValueError('Open log file "%s" with mode "%s" can not '
                                 'be wrote!' % (filename, mode))
            warning('Log file "%s" will be overwrote!', filename)
        filename = os.path.abspath(filename)
        encoding = encoding or getpreferredencoding()
        try:
            logfile = _logFiles[filename]
        except:
            logfile = object.__new__(cls)
            logfile.stream = None
            logfile.codecInfo = None
        else:
            if logfile.encoding != encoding and logfile.stream:
                warning('File "%s" is in use, the write encoding will change, '
                        'encoding in use: %s, new encoding: %s' %
                        (filename, logfile.encoding, encoding))
            if logfile.encoding != encoding or logfile.mode != mode:
                logfile.close()
        logfile.filename = filename
        logfile.mode = mode
        logfile.errors = errors
        logfile.encoding = encoding
        logfile.maxsize = maxsize
        logfile.rotation = rotation
        logfile.open()
        logfile.flush()
        _logFiles[filename] = logfile
        return logfile

    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)

    def close(self):
        if self.stream:
            self.stream, stream = None, self.stream
            try:
                if hasattr(stream, 'flush'):
                    stream.flush()
            finally:
                if hasattr(stream, 'close'):
                    stream.close()

    def writable(self):
        return hasattr(self.stream, 'writable') and self.stream.writable()

    def encode(self, s, errors):
        return self.codecInfo.encode(s, errors)

    def _encode(self, s, errors):
        consumed = len(s)
        if isinstance(s, unicode):
            s = s.encode(self.encoding, errors)
        return s, consumed

    def write(self, s):
        if hasattr(self.stream, 'write'):
            data, consumed = self.encode(s, self.errors)
            size = len(data)
            if self.size + size > self.maxsize:
                self.rotate()
            self.stream.write(data)
            self.size += size
            return consumed
        return 0

    def flush(self):
        if hasattr(self.stream, 'flush'):
            self.stream.flush()

    def rotate(self):
        def rotate(i=0):
            old = fns[i]
            if i == self.rotation:
                if os.path.exists(old):
                    os.remove(old)
                return
            if os.path.exists(old):
                rotate(i + 1)
                new = fns[i + 1]
                os.rename(old, new)

        self.close()
        fns = [self.filename]
        fns += [
            '%s.%d' % (self.filename, i) for i in range(1, self.rotation + 1)
        ]
        rotate()
        self.open()
Пример #9
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')
Пример #10
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)
Пример #11
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,
Пример #12
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),
}