1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
|
from .. import defines
from .base import Column
class String(Column):
ch_type = 'String'
py_types = (str, )
null_value = ''
default_encoding = defines.STRINGS_ENCODING
def __init__(self, encoding=default_encoding, **kwargs):
self.encoding = encoding
super(String, self).__init__(**kwargs)
def write_items(self, items, buf):
buf.write_strings(items, encoding=self.encoding)
def read_items(self, n_items, buf):
return buf.read_strings(n_items, encoding=self.encoding)
class ByteString(String):
py_types = (bytes, )
null_value = b''
def write_items(self, items, buf):
buf.write_strings(items)
def read_items(self, n_items, buf):
return buf.read_strings(n_items)
class FixedString(String):
ch_type = 'FixedString'
def __init__(self, length, **kwargs):
self.length = length
super(FixedString, self).__init__(**kwargs)
def read_items(self, n_items, buf):
return buf.read_fixed_strings(
n_items, self.length, encoding=self.encoding
)
def write_items(self, items, buf):
buf.write_fixed_strings(items, self.length, encoding=self.encoding)
class ByteFixedString(FixedString):
py_types = (bytearray, bytes)
null_value = b''
def read_items(self, n_items, buf):
return buf.read_fixed_strings(n_items, self.length)
def write_items(self, items, buf):
buf.write_fixed_strings(items, self.length)
def create_string_column(spec, column_options):
client_settings = column_options['context'].client_settings
strings_as_bytes = client_settings['strings_as_bytes']
encoding = client_settings.get('strings_encoding', String.default_encoding)
if spec == 'String':
cls = ByteString if strings_as_bytes else String
return cls(encoding=encoding, **column_options)
else:
length = int(spec[12:-1])
cls = ByteFixedString if strings_as_bytes else FixedString
return cls(length, encoding=encoding, **column_options)
|