protobin 0.0.45__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.45 → protobin-0.0.47}/PKG-INFO +1 -1
- {protobin-0.0.45 → protobin-0.0.47}/protobin/fields.py +32 -6
- {protobin-0.0.45 → protobin-0.0.47}/pyproject.toml +1 -1
- {protobin-0.0.45 → protobin-0.0.47}/tests/tests.py +65 -4
- {protobin-0.0.45 → protobin-0.0.47}/.gitignore +0 -0
- {protobin-0.0.45 → protobin-0.0.47}/LICENSE.txt +0 -0
- {protobin-0.0.45 → protobin-0.0.47}/README.md +0 -0
- {protobin-0.0.45 → protobin-0.0.47}/protobin/__init__.py +0 -0
- {protobin-0.0.45 → protobin-0.0.47}/protobin/errors.py +0 -0
- {protobin-0.0.45 → protobin-0.0.47}/protobin/interfaces.py +0 -0
- {protobin-0.0.45 → protobin-0.0.47}/protobin/protocol.py +0 -0
- {protobin-0.0.45 → protobin-0.0.47}/protobin/singleton.py +0 -0
- {protobin-0.0.45 → protobin-0.0.47}/tests/codec8.json +0 -0
- {protobin-0.0.45 → protobin-0.0.47}/tests/demo.json +0 -0
- {protobin-0.0.45 → protobin-0.0.47}/tests/demo.yaml +0 -0
- {protobin-0.0.45 → 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
|
|
|
@@ -159,11 +180,13 @@ class BitsField(FieldBase):
|
|
|
159
180
|
return b'\x00'
|
|
160
181
|
binary = b''
|
|
161
182
|
if len(val) % 8:
|
|
162
|
-
|
|
183
|
+
final = '0' * (8 - (len(val) % 8))
|
|
163
184
|
else:
|
|
164
|
-
|
|
185
|
+
final = ''
|
|
186
|
+
bits = ''
|
|
165
187
|
for b in val:
|
|
166
188
|
bits += '1' if b else '0'
|
|
189
|
+
bits += final
|
|
167
190
|
bts = int(len(bits) / 8)
|
|
168
191
|
if not self.length:
|
|
169
192
|
binary += len(val).to_bytes(1, 'big', signed=False)
|
|
@@ -173,10 +196,12 @@ class BitsField(FieldBase):
|
|
|
173
196
|
def from_binary_bits(self, binary, length):
|
|
174
197
|
numero = int.from_bytes(binary, 'big', signed=False)
|
|
175
198
|
bits = bin(numero)[2:]
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
val =
|
|
199
|
+
max_length = (length // 8) * 8 + (8 if (length % 8) else 0)
|
|
200
|
+
if len(bits) < max_length:
|
|
201
|
+
bits = '0' * (max_length - len(bits)) + bits
|
|
202
|
+
val = []
|
|
203
|
+
for i in range(length):
|
|
204
|
+
val.append(bits[i] == '1')
|
|
180
205
|
return val
|
|
181
206
|
|
|
182
207
|
|
|
@@ -521,6 +546,7 @@ class UnsignedField(FieldBase):
|
|
|
521
546
|
|
|
522
547
|
FIELD_MAP = {
|
|
523
548
|
'array': ArrayField,
|
|
549
|
+
'binary': BinaryField,
|
|
524
550
|
'bits': BitsField,
|
|
525
551
|
'bool': BoolField,
|
|
526
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'}}
|
|
@@ -177,6 +181,65 @@ class BasicTest(unittest.TestCase):
|
|
|
177
181
|
binary = self.protocol.encode(data, 'bits')
|
|
178
182
|
h, recv = self.protocol.decode(binary)
|
|
179
183
|
self.assertEqual(data, recv)
|
|
184
|
+
data = {'test': [True, False]}
|
|
185
|
+
binary = self.protocol.encode(data, 'bits')
|
|
186
|
+
h, recv = self.protocol.decode(binary)
|
|
187
|
+
self.assertEqual(data, recv)
|
|
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
|
+
|
|
228
|
+
def test_bits_false(self):
|
|
229
|
+
data = {'test': [False, False]}
|
|
230
|
+
binary = self.protocol.encode(data, 'bits')
|
|
231
|
+
h, recv = self.protocol.decode(binary)
|
|
232
|
+
self.assertEqual(data, recv)
|
|
233
|
+
|
|
234
|
+
def test_bits_false2(self):
|
|
235
|
+
data = {'test': [False, True]}
|
|
236
|
+
binary = self.protocol.encode(data, 'bits')
|
|
237
|
+
h, recv = self.protocol.decode(binary)
|
|
238
|
+
self.assertEqual(data, recv)
|
|
239
|
+
data = {'test': [False, True, False, False, False, False, False, False, False, False, False, False, False, False]}
|
|
240
|
+
binary = self.protocol.encode(data, 'bits')
|
|
241
|
+
h, recv = self.protocol.decode(binary)
|
|
242
|
+
self.assertEqual(data, recv)
|
|
180
243
|
|
|
181
244
|
def test_bits_fixed(self):
|
|
182
245
|
data = {'test': [True, False, True, False, False, True, True]}
|
|
@@ -210,10 +273,8 @@ class BasicTest(unittest.TestCase):
|
|
|
210
273
|
|
|
211
274
|
def test_char_utf(self):
|
|
212
275
|
data = {'test': 'ñ'}
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
h, recv = self.protocol.decode(binary)
|
|
216
|
-
self.assertEqual(data, recv)
|
|
276
|
+
with self.assertRaises(ValueError):
|
|
277
|
+
self.protocol.encode(data, 'char')
|
|
217
278
|
|
|
218
279
|
def test_char2(self):
|
|
219
280
|
data = {'test': 'FA', 'prueba': 'DO'}
|
|
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
|