pyosmocom 0.0.2__tar.gz → 0.0.3__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.1
2
2
  Name: pyosmocom
3
- Version: 0.0.2
3
+ Version: 0.0.3
4
4
  Summary: Python implementation of core osmocom utilities / protocols
5
5
  Maintainer-email: openbsc@lists.osmocom.org
6
6
  License: GNU GENERAL PUBLIC LICENSE
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
4
4
 
5
5
  [project]
6
6
  name = "pyosmocom"
7
- version = "0.0.2"
7
+ version = "0.0.3"
8
8
  dependencies = [
9
9
  "gsm0338",
10
10
  "construct >= 2.9.51"
@@ -341,7 +341,7 @@ class Rpad(Adapter):
341
341
  return obj.rstrip(self.pattern)
342
342
 
343
343
  def _encode(self, obj, context, path):
344
- target_size = self.sizeof() * self.num_per_byte
344
+ target_size = self.sizeof(**context) * self.num_per_byte
345
345
  if len(obj) > target_size:
346
346
  raise SizeofError("Input ({}) exceeds target size ({})".format(
347
347
  len(obj), target_size))
@@ -492,7 +492,8 @@ def parse_construct(c, raw_bin_data: bytes, length: typing.Optional[int] = None,
492
492
 
493
493
  def build_construct(c, decoded_data, context: dict = {}):
494
494
  """Helper function to handle total_len."""
495
- return c.build(decoded_data, total_len=None, **context)
495
+ context.setdefault('total_len', None)
496
+ return c.build(decoded_data, **context)
496
497
 
497
498
  # here we collect some shared / common definitions of data types
498
499
  LV = Prefixed(Int8ub, HexAdapter(GreedyBytes))
@@ -138,6 +138,36 @@ def is_hex(string: str, minlen: int = 2, maxlen: Optional[int] = None) -> bool:
138
138
  except:
139
139
  return False
140
140
 
141
+ class hexstr(str):
142
+ """Class derived from 'str', represeting a string of hexadecimal digits. It differs in that
143
+ comparisons are case-insensitive, and it offers encoding-free conversion from hexstr to bytes
144
+ and vice-versa."""
145
+ def __new__(cls, s: str):
146
+ if not all(c in string.hexdigits for c in s):
147
+ raise ValueError('Input must be hexadecimal digits only')
148
+ # store as lower case digits
149
+ return super().__new__(cls, s.lower())
150
+
151
+ def __eq__(self, other: str) -> bool:
152
+ # make sure comparison is done case-insensitive
153
+ return str(self) == other.lower()
154
+
155
+ def __getitem__(self, val) -> 'hexstr':
156
+ # make sure slicing a hexstr will return a hexstr
157
+ return hexstr(super().__getitem__(val))
158
+
159
+ def to_bytes(self) -> bytes:
160
+ """return hex-string converted to bytes"""
161
+ s = str(self)
162
+ if len(s) & 1:
163
+ raise ValueError('Cannot convert hex string with odd number of digits')
164
+ return h2b(s)
165
+
166
+ @classmethod
167
+ def from_bytes(cls, bt: bytes) -> 'hexstr':
168
+ """instantiate hex-string from bytes"""
169
+ return cls(b2h(bt))
170
+
141
171
  #########################################################################
142
172
  # ARGPARSE HELPERS
143
173
  #########################################################################
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: pyosmocom
3
- Version: 0.0.2
3
+ Version: 0.0.3
4
4
  Summary: Python implementation of core osmocom utilities / protocols
5
5
  Maintainer-email: openbsc@lists.osmocom.org
6
6
  License: GNU GENERAL PUBLIC LICENSE
@@ -15,4 +15,5 @@ src/pyosmocom.egg-info/requires.txt
15
15
  src/pyosmocom.egg-info/top_level.txt
16
16
  tests/test_construct.py
17
17
  tests/test_gsup.py
18
- tests/test_tlv.py
18
+ tests/test_tlv.py
19
+ tests/test_utils.py
@@ -58,7 +58,6 @@ class Test_GSUP(unittest.TestCase):
58
58
  'desc': 'Purge MS Res',
59
59
  'encoded': h2b('0e010862424201495149f50700'),
60
60
  }, {
61
- 'desc': 'ePDG tunnel request',
62
61
  'desc': 'ePDG tunnel request',
63
62
  'encoded': h2b('50010862424201495149f5280101150780000d00000c000a0105'),
64
63
  }, {
@@ -0,0 +1,58 @@
1
+ #!/usr/bin/env python3
2
+
3
+ # (C) 2022 by Harald Welte <laforge@osmocom.org>
4
+ # All Rights Reserved
5
+ #
6
+ # This program is free software: you can redistribute it and/or modify
7
+ # it under the terms of the GNU General Public License as published by
8
+ # the Free Software Foundation, either version 2 of the License, or
9
+ # (at your option) any later version.
10
+ #
11
+ # This program is distributed in the hope that it will be useful,
12
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
+ # GNU General Public License for more details.
15
+ #
16
+ # You should have received a copy of the GNU General Public License
17
+ # along with this program. If not, see <http://www.gnu.org/licenses/>.
18
+
19
+ import unittest
20
+
21
+ from osmocom.utils import *
22
+
23
+ class TestHexstr(unittest.TestCase):
24
+ def test_cmp(self):
25
+ s = hexstr('aBcD')
26
+ self.assertEqual(s, 'abcd')
27
+ self.assertEqual(s, 'ABCD')
28
+ self.assertEqual(s, 'AbCd')
29
+ self.assertEqual(s, hexstr('AbCd'))
30
+
31
+ def test_tobytes(self):
32
+ s = hexstr('aBcDeF')
33
+ self.assertEqual(s.to_bytes(), b'\xab\xcd\xef')
34
+
35
+ def test_tobytes_odd(self):
36
+ s2 = hexstr('aBc')
37
+ with self.assertRaises(ValueError):
38
+ s2.to_bytes()
39
+
40
+ def test_frombytes(self):
41
+ s = hexstr.from_bytes(b'\x01\x02\xaa')
42
+ self.assertEqual(s, '0102aa')
43
+
44
+ def test_slice(self):
45
+ s = hexstr('abcdef')
46
+ slice1 = s[-2:]
47
+ self.assertTrue(isinstance(slice1, hexstr))
48
+ self.assertEqual(slice1, 'ef')
49
+ slice2 = s[1]
50
+ self.assertTrue(isinstance(slice2, hexstr))
51
+ self.assertEqual(slice2, 'b')
52
+
53
+ def test_str_lower(self):
54
+ self.assertEqual(str(hexstr('ABCD')), 'abcd')
55
+
56
+
57
+ if __name__ == "__main__":
58
+ unittest.main()
File without changes
File without changes
File without changes
File without changes
File without changes