laforge has submitted this change. (
https://gerrit.osmocom.org/c/pysim/+/37620?usp=email
)
Change subject: pySim.tlv: Add convenience methods to IE class
......................................................................
pySim.tlv: Add convenience methods to IE class
The new methods allow programmatic resolution of nested IEs from
a parent, assuming there's only one child of a given type (which is
often but not always the case).
Change-Id: Ic95b74437647ae8d4bf3cdc481832afb622e3cf0
---
M pySim/tlv.py
1 file changed, 32 insertions(+), 1 deletion(-)
Approvals:
fixeria: Looks good to me, but someone else must approve
laforge: Looks good to me, approved
Jenkins Builder: Verified
diff --git a/pySim/tlv.py b/pySim/tlv.py
index 6dc5301..ed3be5d 100644
--- a/pySim/tlv.py
+++ b/pySim/tlv.py
@@ -19,7 +19,7 @@
import inspect
import abc
import re
-from typing import List, Tuple
+from typing import List, Tuple, Optional
from pySim.utils import bertlv_encode_len, bertlv_parse_len, bertlv_encode_tag,
bertlv_parse_tag
from pySim.utils import comprehensiontlv_encode_tag, comprehensiontlv_parse_tag
@@ -189,6 +189,24 @@
self.children = []
return super().from_bytes(do, context=context)
+ def child_by_name(self, name: str) -> Optional['IE']:
+ """Return a child IE instance of given snake-case/json type name.
This only works in case
+ there is no more than one child IE of the given type."""
+ children = list(filter(lambda c: camel_to_snake(type(c).__name__) == name,
self.children))
+ if len(children) > 1:
+ raise KeyError('There are multiple children of class %s' % name)
+ elif len(children) == 1:
+ return children[0]
+
+ def child_by_type(self, cls) -> Optional['IE']:
+ """Return a child IE instance of given type (class). This only
works in case
+ there is no more than one child IE of the given type."""
+ children = list(filter(lambda c: isinstance(c, cls), self.children))
+ if len(children) > 1:
+ raise KeyError('There are multiple children of class %s' % cls)
+ elif len(children) == 1:
+ return children[0]
+
class TLV_IE(IE):
"""Abstract base class for various TLV type Information
Elements."""
--
To view, visit
https://gerrit.osmocom.org/c/pysim/+/37620?usp=email
To unsubscribe, or for help writing mail filters, visit
https://gerrit.osmocom.org/settings
Gerrit-Project: pysim
Gerrit-Branch: master
Gerrit-Change-Id: Ic95b74437647ae8d4bf3cdc481832afb622e3cf0
Gerrit-Change-Number: 37620
Gerrit-PatchSet: 3
Gerrit-Owner: laforge <laforge(a)osmocom.org>
Gerrit-Reviewer: Jenkins Builder
Gerrit-Reviewer: dexter <pmaier(a)sysmocom.de>
Gerrit-Reviewer: fixeria <vyanitskiy(a)sysmocom.de>
Gerrit-Reviewer: laforge <laforge(a)osmocom.org>
Gerrit-MessageType: merged