Source

misc / codec_pipeline.py

import codecs

class CodecPipeline(object):
    """Chains multiple codecs into a single encode/decode operation"""
    def __init__(self, *names, **kwds):
       self.default_errors = self._bind_kwds(**kwds)
       encoders = []
       decoders = []
       self.codecs = names
       for name in names:
           info = self._lookup_codec(name)
           encoders.append(info.encode)
           decoders.append(info.decode)
       self.encoders = encoders
       decoders.reverse()
       self.decoders = decoders

    def _bind_kwds(self, errors=None):
       if errors is None:
           errors = "strict"
       return errors

    def _lookup_codec(self, name):
       # Work around for http://bugs.python.org/issue15331 in 3.x
       try:
           return codecs.lookup(name)
       except LookupError:
           return codecs.lookup(name + "_codec")

    def __repr__(self):
       names = self.codecs
       errors = self.default_errors
       if not names:
           return "{}(errors={!r})".format(type(self).__name__, errors)
       return "{}({}, errors={!r})".format(type(self).__name__,
                                           ", ".join(map(repr, names)),
                                           errors)

    def encode(self, input, errors=None):
       """Apply all encoding operations in the pipeline"""
       if errors is None:
           errors = self.default_errors
       result = input
       for encode in self.encoders:
           result, __ = encode(result, errors)
       return result

    def decode(self, input, errors=None):
       """Apply all decoding operations in the pipeline"""
       if errors is None:
           errors = self.default_errors
       result = input
       for decode in self.decoders:
           result,__ = decode(result, errors)
       return result


if __name__ == "__main__":
    # Quick sanity check
   cp = CodecPipeline("rot-13", "koi8-r", "bz2")
   print(cp)
   x = cp.encode('Hello World!')
   print(repr(x))
   print(repr(cp.decode(x)))
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.