|
From: <ja...@ce...> - 2016-03-29 13:41:57
|
From: Jordi Andreu <ja...@ce...>
The character used as separator to generate the format names of pipelined
codecs now can be customized. The new split caracter can be set by
using the tauruscustomsettings module. The harcoded "_" character has been
taken as the default value. Feature related to ticket #276.
---
lib/taurus/core/util/codecs.py | 41 ++++++++++++++++++++------------------
lib/taurus/tauruscustomsettings.py | 4 ++++
2 files changed, 26 insertions(+), 19 deletions(-)
diff --git a/lib/taurus/core/util/codecs.py b/lib/taurus/core/util/codecs.py
index 0fde2e0..9dceb1b 100644
--- a/lib/taurus/core/util/codecs.py
+++ b/lib/taurus/core/util/codecs.py
@@ -78,6 +78,9 @@ from singleton import Singleton
from log import Logger
from containers import CaselessDict
+import taurus.tauruscustomsettings as tcs
+SEP = getattr(tcs, 'CODEC_PIPELINE_SEP', '_')
+
class Codec(Logger):
"""The base class for all codecs"""
@@ -127,7 +130,7 @@ class NullCodec(Codec):
:return: (sequence[str, obj]) a sequence of two elements where the first item is the encoding format of the second item object"""
format = 'null'
if len(data[0]):
- format += '_%s' % data[0]
+ format += '%s%s' % (SEP, data[0])
return format, data[1]
def decode(self, data, *args, **kwargs):
@@ -138,7 +141,7 @@ class NullCodec(Codec):
:return: (sequence[str, obj]) a sequence of two elements where the first item is the encoding format of the second item object"""
if not data[0].startswith('null'):
return data
- format = data[0].partition('_')[2]
+ format = data[0].partition(SEP)[2]
return format, data[1]
@@ -169,7 +172,7 @@ class ZIPCodec(Codec):
import zlib
format = 'zip'
if len(data[0]):
- format += '_%s' % data[0]
+ format += '%s%s' % (SEP, data[0])
return format, zlib.compress(data[1])
def decode(self, data, *args, **kwargs):
@@ -181,7 +184,7 @@ class ZIPCodec(Codec):
import zlib
if not data[0].startswith('zip'):
return data
- format = data[0].partition('_')[2]
+ format = data[0].partition(SEP)[2]
return format, zlib.decompress(data[1])
@@ -212,7 +215,7 @@ class BZ2Codec(Codec):
import bz2
format = 'bz2'
if len(data[0]):
- format += '_%s' % data[0]
+ format += '%s%s' % (SEP, data[0])
return format, bz2.compress(data[1])
def decode(self, data, *args, **kwargs):
@@ -224,7 +227,7 @@ class BZ2Codec(Codec):
import bz2
if not data[0].startswith('bz2'):
return data
- format = data[0].partition('_')[2]
+ format = data[0].partition(SEP)[2]
return format, bz2.decompress(data[1])
@@ -260,7 +263,7 @@ class PickleCodec(Codec):
import pickle
format = 'pickle'
if len(data[0]):
- format += '_%s' % data[0]
+ format += '%s%s' % (SEP, data[0])
# make it compact by default
kwargs['protocol'] = kwargs.get('protocol', pickle.HIGHEST_PROTOCOL)
return format, pickle.dumps(data[1], *args, **kwargs)
@@ -276,7 +279,7 @@ class PickleCodec(Codec):
import pickle
if not data[0].startswith('pickle'):
return data
- format = data[0].partition('_')[2]
+ format = data[0].partition(SEP)[2]
if isinstance(data[1], buffer):
data = data[0], str(data[1])
@@ -318,7 +321,7 @@ class JSONCodec(Codec):
import json
format = 'json'
if len(data[0]):
- format += '_%s' % data[0]
+ format += '%s%s' % (SEP, data[0])
# make it compact by default
kwargs['separators'] = kwargs.get('separators', (',', ':'))
return format, json.dumps(data[1], *args, **kwargs)
@@ -334,7 +337,7 @@ class JSONCodec(Codec):
import json
if not data[0].startswith('json'):
return data
- format = data[0].partition('_')[2]
+ format = data[0].partition(SEP)[2]
ensure_ascii = kwargs.pop('ensure_ascii', False)
@@ -400,7 +403,7 @@ class BSONCodec(Codec):
import bson
format = 'bson'
if len(data[0]):
- format += '_%s' % data[0]
+ format += '%s%s' % (SEP, data[0])
return format, bson.BSON.encode(data[1], *args, **kwargs)
def decode(self, data, *args, **kwargs):
@@ -414,7 +417,7 @@ class BSONCodec(Codec):
import bson
if not data[0].startswith('bson'):
return data
- format = data[0].partition('_')[2]
+ format = data[0].partition(SEP)[2]
ensure_ascii = kwargs.pop('ensure_ascii', False)
data = data[0], bson.BSON(data[1])
@@ -456,13 +459,13 @@ class FunctionCodec(Codec):
def encode(self, data, *args, **kwargs):
format = self._func_name
if len(data[0]):
- format += '_%s' % data[0]
+ format += '%s%s' % (SEP, data[0])
return format, {'type': self._func_name, 'data': data[1]}
def decode(self, data, *args, **kwargs):
if not data[0].startswith(self._func_name):
return data
- format = data[0].partition('_')[2]
+ format = data[0].partition(SEP)[2]
return format, data[1]
@@ -510,7 +513,7 @@ class VideoImageCodec(Codec):
fmt = 'videoimage'
if len(data[0]):
- fmt += '_%s' % data[0]
+ format += '%s%s' % (SEP, data[0])
# imgMode depends on numpy.array dtype
imgMode = self.__getModeId(str(data[1].dtype))
# frameNumber, unknown then -1
@@ -530,9 +533,9 @@ class VideoImageCodec(Codec):
self.warning(('"VIDEO_IMAGE" format name is deprecated.' +
'Use "videoimage" instead'))
fixedformat = data[0].replace('VIDEO_IMAGE', 'videoimage')
- _, _, fmt = fixedformat.partition('_')
+ _, _, fmt = fixedformat.partition(SEP)
elif data[0].startswith('videoimage'):
- _, _, fmt = data[0].partition('_')
+ _, _, fmt = data[0].partition(SEP)
else:
return data
header = self.__unpackHeader(
@@ -748,7 +751,7 @@ class VideoImageCodec(Codec):
class CodecPipeline(Codec, list):
"""The codec class used when encoding/decoding data with multiple encoders
- Example usage::
+ Example usage (assuming "_" as separator SEP)::
>>> from taurus.core.util.codecs import CodecPipeline
@@ -771,7 +774,7 @@ class CodecPipeline(Codec, list):
list.__init__(self)
f = CodecFactory()
- for i in format.split('_'):
+ for i in format.split(SEP):
codec = f.getCodec(i)
self.debug("Appending %s => %s" % (i, codec))
if codec is None:
diff --git a/lib/taurus/tauruscustomsettings.py b/lib/taurus/tauruscustomsettings.py
index 1835a20..3730a0e 100755
--- a/lib/taurus/tauruscustomsettings.py
+++ b/lib/taurus/tauruscustomsettings.py
@@ -76,6 +76,10 @@ DEFAULT_SCHEME = "tango"
PLY_OPTIMIZE = 1
+# Setup the default split character used to separate the encoding names when
+# using pipeline encoding.
+CODEC_PIPELINE_SEP = '_'
+
# ----------------------------------------------------------------------------
# Taurus namespace
# ----------------------------------------------------------------------------
--
2.1.4
|