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.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: protobin
3
- Version: 0.0.46
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,
@@ -4,7 +4,7 @@ build-backend = "hatchling.build"
4
4
 
5
5
  [project]
6
6
  name = "protobin"
7
- version = "0.0.46"
7
+ version = "0.0.47"
8
8
  authors = [
9
9
  { name="Daniel Robles", email="drm.electronic@gmail.com" },
10
10
  ]
@@ -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