-
Notifications
You must be signed in to change notification settings - Fork 0
/
cain.py
231 lines (197 loc) · 6.92 KB
/
cain.py
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
"""
cain
A small yet powerful data format!
"""
import typing
import cain.types
from cain.model import Datatype
from cain.types import retrieve_type
from cain.types.types import Type
# the Schema type
T = typing.TypeVar("T")
Schema = typing.Union[typing.Type[Datatype], Datatype, typing.Type[T]]
def dumps(obj: typing.Any,
schema: Schema,
include_header: typing.Union[bool, Type] = False) -> bytes:
"""
Encodes the given object `obj` as a Cain formatted data, following `schema`.
Parameters
----------
obj: typing.Any
The object to encode
schema: type[Datatype] | Datatype | type
The schema to use for the encoding
include_header: bool, default = False
This prepends a header containing the schema at the beginning of the content.
Warning: This will significantly increase the size of the result (especially for
originally small content)
Returns
-------
bytes
The encoded data.
Examples
--------
>>> import cain
>>> from cain.types import Object, Optional
>>> cain.dumps({"a": 2}, Object[{"a": int}])
b'\x00\x00\x02'
>>> class TestObject(Object):
... bar: tuple[str, Optional[str], float, int]
...
>>> cain.dumps(['foo', {'bar': ('baz', None, 1.0, 2)}], list[str, TestObject])
b'\x00foo\x00\x00\x00baz\x00\x00\x00\x00\x80?\x00\x02'
>>> print(cain.dumps("\"foo\bar", str))
b'"foo\x08ar\x00'
>>> print(cain.dumps('\u1234', str))
b'\xe1\x88\xb4\x00'
>>> print(cain.dumps('\\', str))
b'\\\x00'
"""
encoder, type_args = retrieve_type(schema)
value = encoder.encode(obj, *type_args)
if include_header:
header = encode_schema(schema)
# I wondered if we should include some kind of version to the header
# but I concluded that this should be up to the user choice to
# increase the content size
value = cain.types.Tuple[bytes, bytes].encode((header, value))
return value
def dump(obj: typing.Any,
handler: typing.BinaryIO,
schema: Schema,
include_header: bool = False) -> None:
"""
Encodes the given object `obj` as a Cain formatted data, following `schema`
and writes it to the given file-like object `fp`.
Parameters
----------
obj: Any
The object to encode
fp: BinaryIO
The file-like object to write the encoded data to
schema: type[Datatype] | Datatype | type
The schema to use for the encoding
include_header: bool, default = False
This prepends a header containing the schema at the beginning of the content.
Warning: This will significantly increase the size of the result (especially for
originally small content)
Examples
--------
>>> import cain
>>> from cain.types import Object, Optional
>>> schema = list[str, Object[{"bar": tuple[str, Optional[str], float, int]}]]
>>> with open('test.cain', 'w+b') as fp:
... cain.dump(['foo', {'bar': ('baz', None, 1.0, 2)}], fp, schema)
...
>>> with open('test.cain', 'r+b') as fp:
... print(fp.read())
...
b'\x00foo\x00\x00\x00baz\x00\x00\x00\x00\x80?\x00\x02'
"""
handler.write(dumps(obj, schema, include_header))
def loads(obj: bytes, schema: typing.Optional[Schema[T]] = None) -> T:
"""
Decodes the given Cain formatted data `obj` following `schema`.
Note:
Parameters
----------
obj: bytes
The Cain formatted data to decode.
schema: type[Datatype] | Datatype | type | None, default = None
The schema to use for the decoding.
When left empty, the given `obj` should contain a header with the schema to decode it.
Returns
-------
typing.Any
The decoded object.
Examples
--------
>>> import cain
>>> from cain.types import Optional, Object
>>> schema = list[str, Object[{"bar": tuple[str, Optional[str], float, int]}]]
>>> cain.loads(b'\x00foo\x00\x00\x00baz\x00\x00\x00\x00\x80?\x00\x02', schema)
['foo', {'bar': ('baz', None, 1.0, 2)}]
>>> print(cain.loads(b'"foo\x08ar\x00', str))
"foar
>>> print(cain.loads(b'\xe1\x88\xb4\x00', str))
ሴ
>>> print(cain.loads(b'\\\x00', str))
\
"""
if not schema:
schema, obj = cain.types.Tuple[bytes, bytes].decode(obj)
schema = Type.decode(schema)
encoder, type_args = retrieve_type(schema)
return encoder.decode(obj, *type_args)
def load(handler: typing.BinaryIO, schema: typing.Optional[Schema[T]] = None) -> T:
"""
Reads the Cain formatted data from `fp` and decodes it following `schema`.
Parameters
----------
fp: typing.Any
The file-like object to read the Cain formatted data from.
schema: type[Datatype] | Datatype | type | None, default = None
The schema to use for the decoding.
When left empty, the given file should contain a header with the schema to decode it.
Returns
-------
typing.Any
The decoded object.
Examples
--------
>>> import cain
>>> from cain.types import Optional, Object
>>> schema = list[str, Object[{"bar": tuple[str, Optional[str], float, int]}]]
>>> with open('test.cain', 'w+b') as fp:
... cain.dump(['foo', {'bar': ('baz', None, 1.0, 2)}], fp, schema)
...
>>> with open('test.cain', 'r+b') as fp:
... cain.load(fp, schema)
...
['foo', {'bar': ('baz', None, 1.0, 2)}]
"""
return loads(handler.read(), schema)
def encode_schema(schema: Schema) -> bytes:
"""
Encodes the given schema as a Cain formatted data, to dynamically encode data
Parameters
----------
schema: type[Datatype] | Datatype | type
The schema to encode
Returns
-------
bytes
The encoded schema
Examples
--------
>>> import cain
>>> from cain.types import Object, Optional
>>> cain.encode_schema(Object[{"a": int}])
b'\x00\x00\x01\x00\x00a\x00\x00\x01\x00\x00\x01\x03\x00\x01\x02\x00\x00\x00\x00\x06\x00\x00\x00\x00\x16'
>>> class TestObject(Object):
... bar: tuple[str, Optional[str], float, int]
...
>>> cain.encode_schema(list[str, TestObject])
b'\x01\x02\x00\x01\x00\x00\x00\x00\x00\x02\x00\x00...\x00\x16\x01\x00TestObject\x00\x00\x00'
"""
return Type.encode(schema)
def decode_schema(schema: bytes) -> typing.Type[Datatype]:
"""
Encodes the given schema as a Cain formatted data, to dynamically decode data
Parameters
----------
schema: bytes
The schema to decode
Returns
-------
type[Datatype]
The datatype
Examples
--------
>>> import cain
>>> cain.decode_schema(b'\x00\x00\x01\x00\x00a\x00\x00\x01\x00\x00\x01\x03\x00\x01\x02\x00\x00\x00\x00\x06\x00\x00\x00\x00\x00\x16\x00')
Object<{'a': Int}>
>>> cain.decode_schema(b'\x01\x02\x00\x01\x00\x00\x00\x00\x00\x02\x00\x00...\x00\x16\x01\x00TestObject\x00\x00\x00')
Array[String, TestObject]
"""
return Type.decode(schema)