blob: 42ec37fe8867ef8d3a5df8a315ed379814dc546b (
plain) (
blame)
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
|
import six
from libcpp cimport bool
from util.generic.string cimport TString, TStringBuf
def to_bytes(s):
try:
return s.encode('utf-8')
except AttributeError:
pass
return s
def from_bytes(s):
if six.PY3:
return s.decode('utf-8')
return s
cdef extern from "library/cpp/blockcodecs/codecs.h" namespace "NBlockCodecs":
cdef cppclass ICodec:
void Encode(TStringBuf data, TString& res) nogil
void Decode(TStringBuf data, TString& res) nogil
cdef const ICodec* Codec(const TStringBuf& name) except +
cdef TString ListAllCodecsAsString() except +
def dumps(name, data):
name = to_bytes(name)
cdef const ICodec* codec = Codec(TStringBuf(name, len(name)))
cdef TString res
cdef TStringBuf cdata = TStringBuf(data, len(data))
with nogil:
codec.Encode(cdata, res)
return res.c_str()[:res.length()]
def loads(name, data):
name = to_bytes(name)
cdef const ICodec* codec = Codec(TStringBuf(name, len(name)))
cdef TString res
cdef TStringBuf cdata = TStringBuf(data, len(data))
with nogil:
codec.Decode(cdata, res)
return res.c_str()[:res.length()]
def list_all_codecs():
cdef TString res = ListAllCodecsAsString()
return from_bytes(res.c_str()[:res.length()]).split(',')
|