protobin 0.0.9__tar.gz → 0.0.11__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.9 → protobin-0.0.11}/PKG-INFO +1 -1
- {protobin-0.0.9 → protobin-0.0.11}/protobin/fields.py +15 -9
- {protobin-0.0.9 → protobin-0.0.11}/protobin/protocol.py +4 -4
- {protobin-0.0.9 → protobin-0.0.11}/pyproject.toml +1 -1
- {protobin-0.0.9 → protobin-0.0.11}/tests/demo.json +2 -2
- {protobin-0.0.9 → protobin-0.0.11}/tests/tests.py +44 -13
- {protobin-0.0.9 → protobin-0.0.11}/.gitignore +0 -0
- {protobin-0.0.9 → protobin-0.0.11}/LICENSE.txt +0 -0
- {protobin-0.0.9 → protobin-0.0.11}/README.md +0 -0
- {protobin-0.0.9 → protobin-0.0.11}/protobin/__init__.py +0 -0
- {protobin-0.0.9 → protobin-0.0.11}/protobin/errors.py +0 -0
- {protobin-0.0.9 → protobin-0.0.11}/protobin/interfaces.py +0 -0
- {protobin-0.0.9 → protobin-0.0.11}/tests/demo.yaml +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.3
|
|
2
2
|
Name: protobin
|
|
3
|
-
Version: 0.0.
|
|
3
|
+
Version: 0.0.11
|
|
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
|
|
@@ -25,11 +25,13 @@ class FieldEnum(enum.StrEnum):
|
|
|
25
25
|
|
|
26
26
|
class FieldBase:
|
|
27
27
|
bytes: int | None
|
|
28
|
-
|
|
28
|
+
keys: List[str] | None
|
|
29
|
+
key: str
|
|
29
30
|
type: FieldEnum
|
|
30
31
|
|
|
31
32
|
def __init__(self, k, js):
|
|
32
33
|
self.key = k
|
|
34
|
+
self.keys = None
|
|
33
35
|
self.type = js['type']
|
|
34
36
|
self.bytes = js.get('bytes')
|
|
35
37
|
|
|
@@ -79,7 +81,6 @@ class FieldBase:
|
|
|
79
81
|
return binary[1:binary[0] + 1], binary[binary[0] + 1:]
|
|
80
82
|
|
|
81
83
|
|
|
82
|
-
|
|
83
84
|
class FieldArray(FieldBase):
|
|
84
85
|
length: int
|
|
85
86
|
|
|
@@ -203,7 +204,9 @@ class FieldDate(FieldBase):
|
|
|
203
204
|
self.bytes = 3
|
|
204
205
|
|
|
205
206
|
def to_binary(self, val: datetime.datetime | str):
|
|
206
|
-
if
|
|
207
|
+
if val is None:
|
|
208
|
+
val = datetime.datetime(2000, 1, 1)
|
|
209
|
+
elif isinstance(val, str):
|
|
207
210
|
val = datetime.datetime.strptime(val, '%Y-%m-%d').date()
|
|
208
211
|
return bytes([
|
|
209
212
|
self.to_nible(val.day),
|
|
@@ -254,7 +257,7 @@ class FieldFlags(FieldBase):
|
|
|
254
257
|
|
|
255
258
|
def __init__(self, k, js):
|
|
256
259
|
super().__init__(k, js)
|
|
257
|
-
self.
|
|
260
|
+
self.keys = self.key.split(',')
|
|
258
261
|
self.length = len(self.key)
|
|
259
262
|
self.bytes = math.ceil(self.length / 8)
|
|
260
263
|
|
|
@@ -268,15 +271,18 @@ class FieldFlags(FieldBase):
|
|
|
268
271
|
for b in bits:
|
|
269
272
|
flags.append(b == '1')
|
|
270
273
|
val = {}
|
|
271
|
-
order = -len(self.
|
|
272
|
-
for k in self.
|
|
274
|
+
order = -len(self.keys)
|
|
275
|
+
for k in self.keys:
|
|
273
276
|
val[k] = flags[order]
|
|
274
277
|
order += 1
|
|
275
278
|
return val
|
|
276
279
|
|
|
277
|
-
def to_binary(self, data):
|
|
278
|
-
bits = '0' * (8 - (len(self.
|
|
279
|
-
for k in self.
|
|
280
|
+
def to_binary(self, data, allow_none=False):
|
|
281
|
+
bits = '0' * (8 - (len(self.keys) % 8))
|
|
282
|
+
for k in self.keys:
|
|
283
|
+
val = data.get(k)
|
|
284
|
+
if val is None:
|
|
285
|
+
raise ValueError(f'Error en el campo "{self.key}" se espera un boolean pero se recibe None')
|
|
280
286
|
bits += '1' if data[k] else '0'
|
|
281
287
|
return int(bits, 2).to_bytes(self.bytes, 'big', signed=False)
|
|
282
288
|
|
|
@@ -17,8 +17,8 @@ class Format:
|
|
|
17
17
|
input_mode = 'fields'
|
|
18
18
|
output_mode = 'fields'
|
|
19
19
|
else:
|
|
20
|
-
input_mode = '
|
|
21
|
-
output_mode = 'server' if server else '
|
|
20
|
+
input_mode = 'client' if server else 'server'
|
|
21
|
+
output_mode = 'server' if server else 'client'
|
|
22
22
|
for k, f in format[input_mode].items():
|
|
23
23
|
self.input_fields.append(FIELD_MAP[f['type']](k, f))
|
|
24
24
|
for k, f in format[output_mode].items():
|
|
@@ -34,8 +34,8 @@ class Format:
|
|
|
34
34
|
data = {}
|
|
35
35
|
for f in self.input_fields:
|
|
36
36
|
val, binary = f.decode(binary)
|
|
37
|
-
if
|
|
38
|
-
for k in f.
|
|
37
|
+
if f.keys:
|
|
38
|
+
for k in f.keys:
|
|
39
39
|
data[k] = val[k]
|
|
40
40
|
else:
|
|
41
41
|
data[f.key] = val
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"login": {
|
|
3
3
|
"header": "T",
|
|
4
|
-
"
|
|
4
|
+
"client": {
|
|
5
5
|
"serial": {"type": "string"}
|
|
6
6
|
},
|
|
7
7
|
"server": {
|
|
@@ -17,7 +17,7 @@
|
|
|
17
17
|
},
|
|
18
18
|
"report": {
|
|
19
19
|
"header": "P",
|
|
20
|
-
"
|
|
20
|
+
"client": {
|
|
21
21
|
"positions": {"bytes": 19, "type": "array", "array": {
|
|
22
22
|
"time": {"bytes": 6, "type": "datetime"},
|
|
23
23
|
"lng": {"bytes": 4, "type": "float", "decimals": 6},
|
|
@@ -105,6 +105,10 @@ class BasicTest(unittest.TestCase):
|
|
|
105
105
|
"header": "f",
|
|
106
106
|
"fields": {'test': {'bytes': 4, 'decimals': 6, 'type': 'float'}}
|
|
107
107
|
},
|
|
108
|
+
'id': {
|
|
109
|
+
"header": "i",
|
|
110
|
+
"fields": {'test': {'bytes': 4, 'type': 'id'}}
|
|
111
|
+
},
|
|
108
112
|
'signed': {
|
|
109
113
|
"header": "+",
|
|
110
114
|
"fields": {'test': {'bytes': 2, 'type': 'signed'}}
|
|
@@ -225,6 +229,16 @@ class BasicTest(unittest.TestCase):
|
|
|
225
229
|
h, recv = self.protocol.decode(binary)
|
|
226
230
|
self.assertEqual(data, recv)
|
|
227
231
|
|
|
232
|
+
def test_flags_none(self):
|
|
233
|
+
data = {'f1': True, 'f2': False, 'f3': False, 'f4': None}
|
|
234
|
+
with self.assertRaises(ValueError):
|
|
235
|
+
self.protocol.encode(data, 'flags')
|
|
236
|
+
|
|
237
|
+
def test_flags_incomplete(self):
|
|
238
|
+
data = {'f1': True, 'f2': False, 'f3': False}
|
|
239
|
+
with self.assertRaises(ValueError):
|
|
240
|
+
self.protocol.encode(data, 'flags')
|
|
241
|
+
|
|
228
242
|
def test_float(self):
|
|
229
243
|
data = {'test': -11.659812}
|
|
230
244
|
binary = self.protocol.encode(data, 'float')
|
|
@@ -244,6 +258,23 @@ class BasicTest(unittest.TestCase):
|
|
|
244
258
|
h, recv = self.protocol.decode(binary)
|
|
245
259
|
self.assertEqual(data, recv)
|
|
246
260
|
|
|
261
|
+
def test_unsigned_zero(self):
|
|
262
|
+
data = {'test': None}
|
|
263
|
+
with self.assertRaises(ValueError) as er:
|
|
264
|
+
self.protocol.encode(data, 'unsigned')
|
|
265
|
+
|
|
266
|
+
def test_id(self):
|
|
267
|
+
data = {'test': 254}
|
|
268
|
+
binary = self.protocol.encode(data, 'id')
|
|
269
|
+
h, recv = self.protocol.decode(binary)
|
|
270
|
+
self.assertEqual(data, recv)
|
|
271
|
+
|
|
272
|
+
def test_id_zero(self):
|
|
273
|
+
data = {'test': None}
|
|
274
|
+
binary = self.protocol.encode(data, 'id')
|
|
275
|
+
h, recv = self.protocol.decode(binary)
|
|
276
|
+
self.assertEqual(data, recv)
|
|
277
|
+
|
|
247
278
|
def test_string(self):
|
|
248
279
|
data = {'test': 'texto de prueba'}
|
|
249
280
|
binary = self.protocol.encode(data, 'string')
|
|
@@ -264,8 +295,8 @@ class BasicTest(unittest.TestCase):
|
|
|
264
295
|
class AdvancedTest(unittest.TestCase):
|
|
265
296
|
|
|
266
297
|
def test_file_json(self):
|
|
267
|
-
|
|
268
|
-
binary =
|
|
298
|
+
client = Protocol(file='demo.json', server=False)
|
|
299
|
+
binary = client.encode(DATA, 'report')
|
|
269
300
|
server = Protocol(file='demo.json', server=True)
|
|
270
301
|
name, recv = server.decode(binary)
|
|
271
302
|
self.assertEqual(DATA, recv)
|
|
@@ -291,7 +322,7 @@ class AdvancedTest(unittest.TestCase):
|
|
|
291
322
|
def test_new_yaml(self):
|
|
292
323
|
ym = yaml.full_load("""
|
|
293
324
|
login:
|
|
294
|
-
|
|
325
|
+
client:
|
|
295
326
|
serial:
|
|
296
327
|
type: string
|
|
297
328
|
header: T
|
|
@@ -321,7 +352,7 @@ login:
|
|
|
321
352
|
bytes: 4
|
|
322
353
|
type: id
|
|
323
354
|
report:
|
|
324
|
-
|
|
355
|
+
client:
|
|
325
356
|
direction:
|
|
326
357
|
bytes: 1
|
|
327
358
|
type: bool
|
|
@@ -386,11 +417,11 @@ report:
|
|
|
386
417
|
print(ym)
|
|
387
418
|
|
|
388
419
|
def test_login(self):
|
|
389
|
-
|
|
420
|
+
client = Protocol(file='demo.json', server=False)
|
|
390
421
|
server = Protocol(file='demo.json', server=True)
|
|
391
422
|
|
|
392
423
|
data = {'serial': '15984316545'}
|
|
393
|
-
binary =
|
|
424
|
+
binary = client.encode(data, 'login')
|
|
394
425
|
print('client', binary)
|
|
395
426
|
header, recv = server.decode(binary)
|
|
396
427
|
self.assertEqual(header, 'login')
|
|
@@ -407,15 +438,15 @@ report:
|
|
|
407
438
|
}
|
|
408
439
|
binary = server.encode(data, 'login')
|
|
409
440
|
print('server', binary)
|
|
410
|
-
header, recv =
|
|
441
|
+
header, recv = client.decode(binary)
|
|
411
442
|
self.assertEqual(header, 'login')
|
|
412
443
|
self.assertEqual(data, recv)
|
|
413
444
|
|
|
414
445
|
def test_file_yaml(self):
|
|
415
|
-
|
|
446
|
+
client = Protocol(file='demo.yaml', server=False)
|
|
416
447
|
server = Protocol(file='demo.yaml', server=True)
|
|
417
448
|
data = {'serial': '15984316545'}
|
|
418
|
-
binary =
|
|
449
|
+
binary = client.encode(data, 'login')
|
|
419
450
|
header, recv = server.decode(binary)
|
|
420
451
|
self.assertEqual(header, 'login')
|
|
421
452
|
self.assertEqual(data, recv)
|
|
@@ -430,15 +461,15 @@ report:
|
|
|
430
461
|
'driver': 1432
|
|
431
462
|
}
|
|
432
463
|
binary = server.encode(data, 'login')
|
|
433
|
-
header, recv =
|
|
464
|
+
header, recv = client.decode(binary)
|
|
434
465
|
self.assertEqual(header, 'login')
|
|
435
466
|
self.assertEqual(data, recv)
|
|
436
467
|
|
|
437
468
|
def test_file_json_yaml(self):
|
|
438
|
-
|
|
469
|
+
client = Protocol(file='demo.json', server=False)
|
|
439
470
|
server = Protocol(file='demo.yaml', server=True)
|
|
440
471
|
data = {'serial': '15984316545'}
|
|
441
|
-
binary =
|
|
472
|
+
binary = client.encode(data, 'login')
|
|
442
473
|
header, recv = server.decode(binary)
|
|
443
474
|
self.assertEqual(header, 'login')
|
|
444
475
|
self.assertEqual(data, recv)
|
|
@@ -453,7 +484,7 @@ report:
|
|
|
453
484
|
'driver': 1432
|
|
454
485
|
}
|
|
455
486
|
binary = server.encode(data, 'login')
|
|
456
|
-
header, recv =
|
|
487
|
+
header, recv = client.decode(binary)
|
|
457
488
|
self.assertEqual(header, 'login')
|
|
458
489
|
self.assertEqual(data, recv)
|
|
459
490
|
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|