protobin 0.0.20__tar.gz → 0.0.22__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.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.3
2
2
  Name: protobin
3
- Version: 0.0.20
3
+ Version: 0.0.22
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
@@ -31,7 +31,7 @@ License: MIT License
31
31
  Classifier: License :: OSI Approved :: MIT License
32
32
  Classifier: Operating System :: OS Independent
33
33
  Classifier: Programming Language :: Python :: 3
34
- Requires-Python: >=3.9
34
+ Requires-Python: >=3.8
35
35
  Requires-Dist: crcmod==1.7
36
36
  Requires-Dist: pyyaml~=6.0
37
37
  Description-Content-Type: text/markdown
@@ -16,6 +16,7 @@ class Format:
16
16
  self.input_fields = []
17
17
  self.output_fields = []
18
18
  self.header = format.get('header')
19
+ self.codec = format.get('codec')
19
20
  self.crc = format.get('crc', True)
20
21
  if server is None:
21
22
  input_mode = 'fields'
@@ -31,12 +32,17 @@ class Format:
31
32
  self.output_fields.append(FIELD_MAP[f['type']](k, f))
32
33
 
33
34
  def __repr__(self):
34
- return f'Format: {self.name} <{self.header}>'
35
+ if self.header:
36
+ return f'Format: {self.name} <{self.header}>'
37
+ elif self.codec:
38
+ return f'Format: {self.name} <{self.codec}>'
35
39
 
36
40
  def encode(self, data):
37
41
  binary = b''
38
42
  if self.header:
39
43
  binary += self.header.encode('utf') + b'='
44
+ elif self.codec:
45
+ binary += self.codec.to_bytes()
40
46
  for f in self.output_fields:
41
47
  binary += f.encode(data)
42
48
  return binary
@@ -61,6 +67,7 @@ class Protocol:
61
67
  def __init__(self, server: bool = None, file=None, js=None):
62
68
  self.server = server
63
69
  self.headers = {}
70
+ self.codecs = {}
64
71
  if file:
65
72
  with open(file, 'r') as f:
66
73
  if 'json' in file:
@@ -85,10 +92,15 @@ class Protocol:
85
92
  self.formats[name] = Format(name=name, format=format, server=self.server)
86
93
  if 'header' in format:
87
94
  self.headers[format['header']] = name
95
+ if 'codec' in format:
96
+ self.codecs[format['codec']] = name
88
97
 
89
98
  def get_format(self, h):
90
99
  return self.formats[self.headers[h.decode('utf')]]
91
100
 
101
+ def get_codec(self, h):
102
+ return self.formats[self.codecs[int.from_bytes(h)]]
103
+
92
104
  def encode(self, data, format_key):
93
105
  if format_key not in self.formats:
94
106
  raise InputError(f'{format_key} is not available format, these are the all availables formats {self.formats.keys()}')
@@ -101,12 +113,18 @@ class Protocol:
101
113
  def decode(self, binary, codec=None):
102
114
  if codec is None:
103
115
  n = binary.find(b'=')
104
- if n > 4:
116
+ if n == -1:
105
117
  binary = self.check_crc(binary)
106
- n = binary.find(b'=')
107
- h = binary[0:n]
108
- binary = binary[n + 1:]
109
- format = self.get_format(h)
118
+ h = binary[:1]
119
+ binary = binary[1:]
120
+ format = self.get_codec(h)
121
+ else:
122
+ if n > 4:
123
+ binary = self.check_crc(binary)
124
+ n = binary.find(b'=')
125
+ h = binary[0:n]
126
+ binary = binary[n + 1:]
127
+ format = self.get_format(h)
110
128
  else:
111
129
  format = self.formats[codec]
112
130
  if format.crc and self.crc16:
@@ -4,7 +4,7 @@ build-backend = "hatchling.build"
4
4
 
5
5
  [project]
6
6
  name = "protobin"
7
- version = "0.0.20"
7
+ version = "0.0.22"
8
8
  authors = [
9
9
  { name="Daniel Robles", email="drm.electronic@gmail.com" },
10
10
  ]
@@ -15,7 +15,7 @@ dependencies = [
15
15
  description = "Python library for encode and decode data in protobin format"
16
16
  readme = "README.md"
17
17
  license = { file="LICENSE.txt" }
18
- requires-python = ">=3.9"
18
+ requires-python = ">=3.8"
19
19
  classifiers = [
20
20
  "Programming Language :: Python :: 3",
21
21
  "License :: OSI Approved :: MIT License",
@@ -16,8 +16,8 @@
16
16
  }
17
17
  },
18
18
  "report": {
19
+ "codec": 8,
19
20
  "fields": {
20
- "codec": {"type": "unsigned", "bytes": 1},
21
21
  "positions": {"type": "array", "array": {
22
22
  "time": {"type": "timestamp", "decimals": 3},
23
23
  "priority": {"type": "unsigned", "bytes": 1},
@@ -60,15 +60,9 @@
60
60
  }
61
61
  },
62
62
  "server": {
63
- "comando": {
64
- "type": "char"
65
- },
66
- "id": {
67
- "type": "string"
68
- },
69
- "value": {
70
- "type": "string"
71
- }
63
+ "comando": {"type": "char"},
64
+ "id": {"type": "string"},
65
+ "value": {"type": "string"}
72
66
  }
73
67
  }
74
68
  }
@@ -568,9 +568,8 @@ report:
568
568
  hexdata = '000000000000003608010000016B40D8EA30010000000000000000000000000000000105021503010101425E0F01F10000601A014E0000000000000000010000C7CF'
569
569
  binary = bytes.fromhex(hexdata)
570
570
  client = Protocol(file='codec8.json', server=None)
571
- recv = client.decode(binary, 'report')
571
+ header, recv = client.decode(binary)
572
572
  self.assertEqual(recv, {
573
- 'codec': 8,
574
573
  'positions': [{
575
574
  'time': datetime.datetime(2019, 6, 10, 5, 4, 46),
576
575
  'priority': 1,
@@ -613,11 +612,11 @@ report:
613
612
  self.assertEqual(data, recv)
614
613
 
615
614
  def test_crc(self):
616
- data = {'codec': 8, 'positions': [{'time': datetime.datetime(2024, 10, 19, 9, 37, 57, 555000), 'priority': 1, 'lng': -77.0155334, 'lat': -12.0613651, 'alt': 0, 'angle': 0, 'satellites': 3, 'speed': 105, 'event_io': 0, '#events': 0, 'events1b': [], 'events2b': [], 'events4b': [], 'events8b': [], '#reports': 1}]}
615
+ data = {'positions': [{'time': datetime.datetime(2024, 10, 19, 9, 37, 57, 555000), 'priority': 1, 'lng': -77.0155334, 'lat': -12.0613651, 'alt': 0, 'angle': 0, 'satellites': 3, 'speed': 105, 'event_io': 0, '#events': 0, 'events1b': [], 'events2b': [], 'events4b': [], 'events8b': [], '#reports': 1}]}
617
616
  client = Protocol(file='codec8.json')
618
617
  binary = client.encode(data, 'report')
619
618
  self.assertEqual(b'\x00\x00\x00\x00\x00\x00\x00!\x08\x01\x00\x00\x01\x92\xa56\xaf\xb3\x01\xd2\x18\\\xba\xf8\xcf\x94\xed\x00\x00\x00\x00\x03\x00i\x00\x00\x00\x00\x00\x00\x01\x00\x00\xa3\xe4', binary)
620
- recv = client.decode(binary, 'report')
619
+ header, recv = client.decode(binary)
621
620
  self.assertEqual(data, recv)
622
621
 
623
622
  def test_status_crc(self):
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes