protobin 0.0.46__tar.gz → 0.0.47__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {protobin-0.0.46 → protobin-0.0.47}/PKG-INFO +1 -1
- {protobin-0.0.46 → protobin-0.0.47}/protobin/fields.py +22 -0
- {protobin-0.0.46 → protobin-0.0.47}/pyproject.toml +1 -1
- {protobin-0.0.46 → protobin-0.0.47}/tests/tests.py +43 -0
- {protobin-0.0.46 → protobin-0.0.47}/.gitignore +0 -0
- {protobin-0.0.46 → protobin-0.0.47}/LICENSE.txt +0 -0
- {protobin-0.0.46 → protobin-0.0.47}/README.md +0 -0
- {protobin-0.0.46 → protobin-0.0.47}/protobin/__init__.py +0 -0
- {protobin-0.0.46 → protobin-0.0.47}/protobin/errors.py +0 -0
- {protobin-0.0.46 → protobin-0.0.47}/protobin/interfaces.py +0 -0
- {protobin-0.0.46 → protobin-0.0.47}/protobin/protocol.py +0 -0
- {protobin-0.0.46 → protobin-0.0.47}/protobin/singleton.py +0 -0
- {protobin-0.0.46 → protobin-0.0.47}/tests/codec8.json +0 -0
- {protobin-0.0.46 → protobin-0.0.47}/tests/demo.json +0 -0
- {protobin-0.0.46 → protobin-0.0.47}/tests/demo.yaml +0 -0
- {protobin-0.0.46 → protobin-0.0.47}/tests/teltonika.json +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: protobin
|
|
3
|
-
Version: 0.0.
|
|
3
|
+
Version: 0.0.47
|
|
4
4
|
Summary: Python library for encode and decode data in protobin format
|
|
5
5
|
Project-URL: Homepage, https://github.com/drmelectronic/protobin-python
|
|
6
6
|
Project-URL: Bug Tracker, https://github.com/drmelectronic/protobin-python/issues
|
|
@@ -130,6 +130,27 @@ class ArrayField(FieldBase):
|
|
|
130
130
|
return lista, binary
|
|
131
131
|
|
|
132
132
|
|
|
133
|
+
|
|
134
|
+
class BinaryField(FieldBase):
|
|
135
|
+
|
|
136
|
+
def __init__(self, k, js):
|
|
137
|
+
super().__init__(k, js)
|
|
138
|
+
self.length_size = js.get('length_size', 1)
|
|
139
|
+
|
|
140
|
+
def __repr__(self):
|
|
141
|
+
return f'BinaryField<key: {self.key}, bytes: {self.bytes}>'
|
|
142
|
+
|
|
143
|
+
def from_binary(self, binary):
|
|
144
|
+
return binary
|
|
145
|
+
|
|
146
|
+
def split(self, binary):
|
|
147
|
+
bytes = int.from_bytes(binary[:self.length_size], 'big', signed=False)
|
|
148
|
+
return binary[self.length_size:bytes + self.length_size], binary[bytes + self.length_size:]
|
|
149
|
+
|
|
150
|
+
def to_binary(self, val):
|
|
151
|
+
return len(val).to_bytes(self.length_size, 'big') + val
|
|
152
|
+
|
|
153
|
+
|
|
133
154
|
class BitsField(FieldBase):
|
|
134
155
|
length: int
|
|
135
156
|
|
|
@@ -525,6 +546,7 @@ class UnsignedField(FieldBase):
|
|
|
525
546
|
|
|
526
547
|
FIELD_MAP = {
|
|
527
548
|
'array': ArrayField,
|
|
549
|
+
'binary': BinaryField,
|
|
528
550
|
'bits': BitsField,
|
|
529
551
|
'bool': BoolField,
|
|
530
552
|
'char': CharField,
|
|
@@ -83,6 +83,10 @@ class BasicTest(unittest.TestCase):
|
|
|
83
83
|
"header": "b",
|
|
84
84
|
"fields": {'test': {'type': 'bits', 'length': 7}}
|
|
85
85
|
},
|
|
86
|
+
'binary': {
|
|
87
|
+
"header": "bin",
|
|
88
|
+
"fields": {'test': {'type': 'binary'}}
|
|
89
|
+
},
|
|
86
90
|
'bool': {
|
|
87
91
|
"header": "BO",
|
|
88
92
|
"fields": {'test': {'bytes': 1, 'type': 'bool'}}
|
|
@@ -182,6 +186,45 @@ class BasicTest(unittest.TestCase):
|
|
|
182
186
|
h, recv = self.protocol.decode(binary)
|
|
183
187
|
self.assertEqual(data, recv)
|
|
184
188
|
|
|
189
|
+
def test_binary(self):
|
|
190
|
+
data = {'test': b'S=RA\x05'}
|
|
191
|
+
binary = self.protocol.encode(data, 'binary')
|
|
192
|
+
h, recv = self.protocol.decode(binary)
|
|
193
|
+
self.assertEqual(data, recv)
|
|
194
|
+
|
|
195
|
+
def test_command_binary(self):
|
|
196
|
+
data = {'test': None}
|
|
197
|
+
with self.assertRaises(ValueError) as er:
|
|
198
|
+
self.protocol.encode(data, 'unsigned')
|
|
199
|
+
|
|
200
|
+
protocol = Protocol(js={
|
|
201
|
+
"length": 8,
|
|
202
|
+
"fake_prefix": True,
|
|
203
|
+
"crc": {"poly": "0x18005", "init": "0x0000", "reverse": True, "byte_order": "big", "size": 4},
|
|
204
|
+
'formats': {
|
|
205
|
+
"command": {
|
|
206
|
+
"codec": 12,
|
|
207
|
+
"fields": {
|
|
208
|
+
"#commands1": {"bytes": 1, "type": "unsigned"},
|
|
209
|
+
"type": {"bytes": 1, "type": "unsigned"},
|
|
210
|
+
"command": {"length_size": 4, "type": "binary"},
|
|
211
|
+
"#commands2": {"bytes": 1, "type": "unsigned"}
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
}})
|
|
215
|
+
binary = b'S=RA\x05CERES07:42\x08HUACHIPA2\nPZA VITART\x00>\n\x00\x07\x01\x00\x041\x00&\r\x00\x00\x00\x00\x00\x00(\xc4'
|
|
216
|
+
data = {
|
|
217
|
+
'#commands1': 1,
|
|
218
|
+
'type': 5,
|
|
219
|
+
'command': binary,
|
|
220
|
+
'#commands2': 1,
|
|
221
|
+
}
|
|
222
|
+
binary = protocol.encode(data, 'command')
|
|
223
|
+
print(binary)
|
|
224
|
+
h, recv = protocol.decode(binary)
|
|
225
|
+
print(recv)
|
|
226
|
+
self.assertEqual(data, recv)
|
|
227
|
+
|
|
185
228
|
def test_bits_false(self):
|
|
186
229
|
data = {'test': [False, False]}
|
|
187
230
|
binary = self.protocol.encode(data, 'bits')
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|