def init(self, *args, **kwargs): """Singleton instance initialization.""" name = self.__class__.__name__ self.call__init__(Logger, name) self._codec_klasses = copy.copy(CodecFactory.CODEC_MAP) # dict<str, Codec> # where: # - key is the codec format # - value is the codec object that supports the format self._codecs = CaselessDict()
class CodecFactory(Singleton, Logger): """The singleton CodecFactory class. To get the singleton object do:: from taurus.core.util.codecs import CodecFactory f = CodecFactory() The :class:`CodecFactory` class allows you to get a codec object for a given format and also to register new codecs. The :class:`CodecPipeline` is a special codec that is able to code/decode a sequence of codecs. This way you can have codecs 'inside' codecs. Example:: >>> from taurus.core.util.codecs import CodecFactory >>> cf = CodecFactory() >>> json_codec = cf.getCodec('json') >>> bz2_json_codec = cf.getCodec('bz2_json') >>> data = range(100000) >>> f1, enc_d1 = json_codec.encode(('', data)) >>> f2, enc_d2 = bz2_json_codec.encode(('', data)) >>> print len(enc_d1), len(enc_d2) 688890 123511 >>> >>> f1, dec_d1 = json_codec.decode((f1, enc_d1)) >>> f2, dec_d2 = bz2_json_codec.decode((f2, enc_d2)) A Taurus related example:: >>> # this example shows how to automatically get the data from a DEV_ENCODED attribute >>> import taurus >>> from taurus.core.util.codecs import CodecFactory >>> cf = CodecFactory() >>> devenc_attr = taurus.Attribute('a/b/c/devenc_attr') >>> v = devenc_attr.read() >>> codec = CodecFactory().getCodec(v.format) >>> f, d = codec.decode((v.format, v.value)) """ #: Default minimum map of registered codecs CODEC_MAP = CaselessDict({ 'json': JSONCodec, 'bson': BSONCodec, 'bz2': BZ2Codec, 'zip': ZIPCodec, 'pickle': PickleCodec, 'plot': PlotCodec, 'VIDEO_IMAGE': VideoImageCodec, # deprecated 'videoimage': VideoImageCodec, 'null': NullCodec, 'none': NullCodec, '': NullCodec }) def __init__(self): """ Initialization. Nothing to be done here for now.""" pass def init(self, *args, **kwargs): """Singleton instance initialization.""" name = self.__class__.__name__ self.call__init__(Logger, name) self._codec_klasses = copy.copy(CodecFactory.CODEC_MAP) # dict<str, Codec> # where: # - key is the codec format # - value is the codec object that supports the format self._codecs = CaselessDict() def registerCodec(self, format, klass): """Registers a new codec. If a codec already exists for the given format it is removed. :param format: (str) the codec id :param klass: (Codec class) the class that handles the format""" self._codec_klasses[format] = klass # del old codec if exists if self._codecs.has_key(format): del self._codecs[format] def unregisterCodec(self, format): """Unregisters the given format. If the format does not exist an exception is thrown. :param format: (str) the codec id :raises: KeyError""" if self._codec_klasses.has_key(format): del self._codec_klasses[format] if self._codecs.has_key(format): del self._codecs[format] def getCodec(self, format): """Returns the codec object for the given format or None if no suitable codec is found :param format: (str) the codec id :return: (Codec or None) the codec object for the given format""" codec = self._codecs.get(format) if codec is None: codec = self._getNewCodec(format) if not codec is None: self._codecs[format] = codec return codec def _getNewCodec(self, format): klass = self._codec_klasses.get(format) if not klass is None: ret = klass() else: try: ret = CodecPipeline(format) except: ret = self._codec_klasses.get('')() return ret def decode(self, data, *args, **kwargs): while len(data[0]): data = self.getCodec(data[0]).decode(data, *args, **kwargs) return data[1] def encode(self, format, data, *args, **kwargs): return self.getCodec(format).encode(data, *args, **kwargs)
class CodecFactory(Singleton, Logger): """The singleton CodecFactory class. To get the singleton object do:: from taurus.core.util.codecs import CodecFactory f = CodecFactory() The :class:`CodecFactory` class allows you to get a codec object for a given format and also to register new codecs. The :class:`CodecPipeline` is a special codec that is able to code/decode a sequence of codecs. This way you can have codecs 'inside' codecs. Example:: >>> from taurus.core.util.codecs import CodecFactory >>> cf = CodecFactory() >>> json_codec = cf.getCodec('json') >>> bz2_json_codec = cf.getCodec('bz2_json') >>> data = range(100000) >>> f1, enc_d1 = json_codec.encode(('', data)) >>> f2, enc_d2 = bz2_json_codec.encode(('', data)) >>> print len(enc_d1), len(enc_d2) 688890 123511 >>> >>> f1, dec_d1 = json_codec.decode((f1, enc_d1)) >>> f2, dec_d2 = bz2_json_codec.decode((f2, enc_d2)) A Taurus related example:: >>> # this example shows how to automatically get the data from a DEV_ENCODED attribute >>> import taurus >>> from taurus.core.util.codecs import CodecFactory >>> cf = CodecFactory() >>> devenc_attr = taurus.Attribute('a/b/c/devenc_attr') >>> v = devenc_attr.read() >>> codec = CodecFactory().getCodec(v.format) >>> f, d = codec.decode((v.format, v.value)) """ #: Default minimum map of registered codecs CODEC_MAP = CaselessDict({ 'json': JSONCodec, 'bson': BSONCodec, 'bz2': BZ2Codec, 'zip': ZIPCodec, 'pickle': PickleCodec, 'plot': PlotCodec, 'VIDEO_IMAGE': VideoImageCodec, # deprecated 'videoimage': VideoImageCodec, 'null': NullCodec, 'none': NullCodec, '': NullCodec}) def __init__(self): """ Initialization. Nothing to be done here for now.""" pass def init(self, *args, **kwargs): """Singleton instance initialization.""" name = self.__class__.__name__ self.call__init__(Logger, name) self._codec_klasses = copy.copy(CodecFactory.CODEC_MAP) # dict<str, Codec> # where: # - key is the codec format # - value is the codec object that supports the format self._codecs = CaselessDict() def registerCodec(self, format, klass): """Registers a new codec. If a codec already exists for the given format it is removed. :param format: (str) the codec id :param klass: (Codec class) the class that handles the format""" self._codec_klasses[format] = klass # del old codec if exists if self._codecs.has_key(format): del self._codecs[format] def unregisterCodec(self, format): """Unregisters the given format. If the format does not exist an exception is thrown. :param format: (str) the codec id :raises: KeyError""" if self._codec_klasses.has_key(format): del self._codec_klasses[format] if self._codecs.has_key(format): del self._codecs[format] def getCodec(self, format): """Returns the codec object for the given format or None if no suitable codec is found :param format: (str) the codec id :return: (Codec or None) the codec object for the given format""" codec = self._codecs.get(format) if codec is None: codec = self._getNewCodec(format) if not codec is None: self._codecs[format] = codec return codec def _getNewCodec(self, format): klass = self._codec_klasses.get(format) if not klass is None: ret = klass() else: try: ret = CodecPipeline(format) except: ret = self._codec_klasses.get('')() return ret def decode(self, data, *args, **kwargs): while len(data[0]): data = self.getCodec(data[0]).decode(data, *args, **kwargs) return data[1] def encode(self, format, data, *args, **kwargs): return self.getCodec(format).encode(data, *args, **kwargs)