blob: 03d284e451545de9e68f6ca7aa33584a45f7c75e [file] [log] [blame]
Harald Weltee0f9ef12021-04-10 17:22:35 +02001from construct import *
2from pySim.utils import b2h, h2b
3
4"""Utility code related to the integration of the 'construct' declarative parser."""
5
6# (C) 2021 by Harald Welte <laforge@osmocom.org>
7#
8# This program is free software: you can redistribute it and/or modify
9# it under the terms of the GNU General Public License as published by
10# the Free Software Foundation, either version 2 of the License, or
11# (at your option) any later version.
12#
13# This program is distributed in the hope that it will be useful,
14# but WITHOUT ANY WARRANTY; without even the implied warranty of
15# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16# GNU General Public License for more details.
17#
18# You should have received a copy of the GNU General Public License
19# along with this program. If not, see <http://www.gnu.org/licenses/>.
20
21
22class HexAdapter(Adapter):
23 """convert a bytes() type to a string of hex nibbles."""
24 def _decode(self, obj, context, path):
25 return b2h(obj)
26 def _encode(self, obj, context, path):
27 return h2b(obj)
28
29def filter_dict(d, exclude_prefix='_'):
30 """filter the input dict to ensure no keys starting with 'exclude_prefix' remain."""
31 res = {}
32 for (key, value) in d.items():
33 if key.startswith(exclude_prefix):
34 continue
35 if type(value) is dict:
36 res[key] = filter_dict(value)
37 else:
38 res[key] = value
39 return res
40
41# here we collect some shared / common definitions of data types
42LV = Prefixed(Int8ub, HexAdapter(GreedyBytes))