laforge has uploaded this change for review. ( 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(-)
git pull ssh://gerrit.osmocom.org:29418/pysim refs/changes/20/37620/1
diff --git a/pySim/tlv.py b/pySim/tlv.py
index 6dc5301..4d542da 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' % cls)
+ 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: 1
Gerrit-Owner: laforge <laforge(a)osmocom.org>
Gerrit-MessageType: newchange
laforge has uploaded this change for review. ( https://gerrit.osmocom.org/c/pysim/+/37621?usp=email )
Change subject: pySim.runtime: Be more verbose if incompatible method is called
......................................................................
pySim.runtime: Be more verbose if incompatible method is called
Change-Id: I57190d50a63e0c22a8c5921e1348fae31b23e3d4
---
M pySim/runtime.py
1 file changed, 21 insertions(+), 6 deletions(-)
git pull ssh://gerrit.osmocom.org:29418/pysim refs/changes/21/37621/1
diff --git a/pySim/runtime.py b/pySim/runtime.py
index a56d884..22845bc 100644
--- a/pySim/runtime.py
+++ b/pySim/runtime.py
@@ -423,7 +423,8 @@
binary data read from the file
"""
if not isinstance(self.selected_file, TransparentEF):
- raise TypeError("Only works with TransparentEF")
+ raise TypeError("Only works with TransparentEF, but %s is %s" % (self.selected_file,
+ self.selected_file.__class__.__mro__))
return self.scc.read_binary(self.selected_file.fid, length, offset)
def read_binary_dec(self) -> Tuple[dict, str]:
@@ -447,7 +448,8 @@
offset : Offset into the file from which to write 'data_hex'
"""
if not isinstance(self.selected_file, TransparentEF):
- raise TypeError("Only works with TransparentEF")
+ raise TypeError("Only works with TransparentEF, but %s is %s" % (self.selected_file,
+ self.selected_file.__class__.__mro__))
return self.scc.update_binary(self.selected_file.fid, data_hex, offset, conserve=self.rs.conserve_write)
def update_binary_dec(self, data: dict):
@@ -469,7 +471,8 @@
hex string of binary data contained in record
"""
if not isinstance(self.selected_file, LinFixedEF):
- raise TypeError("Only works with Linear Fixed EF")
+ raise TypeError("Only works with Linear Fixed EF, but %s is %s" % (self.selected_file,
+ self.selected_file.__class__.__mro__))
# returns a string of hex nibbles
return self.scc.read_record(self.selected_file.fid, rec_nr)
@@ -492,7 +495,8 @@
data_hex : Hex string binary data to be written
"""
if not isinstance(self.selected_file, LinFixedEF):
- raise TypeError("Only works with Linear Fixed EF")
+ raise TypeError("Only works with Linear Fixed EF, but %s is %s" % (self.selected_file,
+ self.selected_file.__class__.__mro__))
return self.scc.update_record(self.selected_file.fid, rec_nr, data_hex,
conserve=self.rs.conserve_write,
leftpad=self.selected_file.leftpad)
@@ -528,7 +532,8 @@
list of integer tags contained in EF
"""
if not isinstance(self.selected_file, BerTlvEF):
- raise TypeError("Only works with BER-TLV EF")
+ raise TypeError("Only works with BER-TLV EF, but %s is %s" % (self.selected_file,
+ self.selected_file.__class__.__mro__))
data, _sw = self.scc.retrieve_data(self.selected_file.fid, 0x5c)
_tag, _length, value, _remainder = bertlv_parse_one(h2b(data))
return list(value)
@@ -541,7 +546,8 @@
data_hex : Hex string binary data to be written (value portion)
"""
if not isinstance(self.selected_file, BerTlvEF):
- raise TypeError("Only works with BER-TLV EF")
+ raise TypeError("Only works with BER-TLV EF, but %s is %s" % (self.selected_file,
+ self.selected_file.__class__.__mro__))
return self.scc.set_data(self.selected_file.fid, tag, data_hex, conserve=self.rs.conserve_write)
def unregister_cmds(self, cmd_app=None):
--
To view, visit https://gerrit.osmocom.org/c/pysim/+/37621?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: I57190d50a63e0c22a8c5921e1348fae31b23e3d4
Gerrit-Change-Number: 37621
Gerrit-PatchSet: 1
Gerrit-Owner: laforge <laforge(a)osmocom.org>
Gerrit-MessageType: newchange
laforge has uploaded this change for review. ( https://gerrit.osmocom.org/c/pysim/+/37622?usp=email )
Change subject: pySim.commands: Don't convert SwMatchError to ValueError
......................................................................
pySim.commands: Don't convert SwMatchError to ValueError
In the read and write command implementations, we used to catch
lower-layer exceptions (usually SwMatchError) and "translate" that into
a value error, only to add more information to the exception. This
meant that higher-layer code could no longer detect this was actually
a SwMatchError exception type.
Let's instead use the add_note() method to amend the existing exception,
rather than raising a new one of different type.
Change-Id: Ic94d0fe60a8a5e15aade56ec418192ecf31ac5e7
---
M pySim/commands.py
1 file changed, 22 insertions(+), 4 deletions(-)
git pull ssh://gerrit.osmocom.org:29418/pysim refs/changes/22/37622/1
diff --git a/pySim/commands.py b/pySim/commands.py
index 40b0980..3f659eb 100644
--- a/pySim/commands.py
+++ b/pySim/commands.py
@@ -334,8 +334,8 @@
try:
data, sw = self.send_apdu_checksw(pdu)
except Exception as e:
- raise ValueError('%s, failed to read (offset %d)' %
- (str_sanitize(str(e)), offset)) from e
+ e.add_note('failed to read (offset %d)' % offset)
+ raise e
total_data += data
chunk_offset += chunk_len
return total_data, sw
@@ -394,8 +394,8 @@
try:
chunk_data, chunk_sw = self.send_apdu_checksw(pdu)
except Exception as e:
- raise ValueError('%s, failed to write chunk (chunk_offset %d, chunk_len %d)' %
- (str_sanitize(str(e)), chunk_offset, chunk_len)) from e
+ e.add_note('failed to write chunk (chunk_offset %d, chunk_len %d)' % (chunk_offset, chunk_len))
+ raise e
total_data += data
chunk_offset += chunk_len
if verify:
--
To view, visit https://gerrit.osmocom.org/c/pysim/+/37622?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: Ic94d0fe60a8a5e15aade56ec418192ecf31ac5e7
Gerrit-Change-Number: 37622
Gerrit-PatchSet: 1
Gerrit-Owner: laforge <laforge(a)osmocom.org>
Gerrit-MessageType: newchange
Attention is currently required from: dexter.
laforge has posted comments on this change. ( https://gerrit.osmocom.org/c/pysim/+/37614?usp=email )
Change subject: pySim-shell: fix reset command
......................................................................
Patch Set 1:
(1 comment)
File pySim/runtime.py:
https://gerrit.osmocom.org/c/pysim/+/37614/comment/2e2e586c_1bccff2b
PS1, Line 142: if (cmd_app):
python uses no parenthesis around an if statement
--
To view, visit https://gerrit.osmocom.org/c/pysim/+/37614?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: I1ad29c9e7ce7d80bebc92fa173ed7a44ee4c2998
Gerrit-Change-Number: 37614
Gerrit-PatchSet: 1
Gerrit-Owner: dexter <pmaier(a)sysmocom.de>
Gerrit-Reviewer: Jenkins Builder
Gerrit-CC: laforge <laforge(a)osmocom.org>
Gerrit-Attention: dexter <pmaier(a)sysmocom.de>
Gerrit-Comment-Date: Fri, 26 Jul 2024 06:27:47 +0000
Gerrit-HasComments: Yes
Gerrit-Has-Labels: No
Gerrit-MessageType: comment
laforge has submitted this change. ( https://gerrit.osmocom.org/c/pysim/+/37611?usp=email )
Change subject: pySim-shell: fix comment formatting
......................................................................
pySim-shell: fix comment formatting
Related: OS#6092
Change-Id: I101868a6f0220b62977c5e633df2607467cfba91
---
M pySim-shell.py
1 file changed, 12 insertions(+), 3 deletions(-)
Approvals:
Jenkins Builder: Verified
laforge: Looks good to me, approved
diff --git a/pySim-shell.py b/pySim-shell.py
index 7523ca2..0e7e63c 100755
--- a/pySim-shell.py
+++ b/pySim-shell.py
@@ -734,9 +734,8 @@
@cmd2.with_argparser(verify_adm_parser)
def do_verify_adm(self, opts):
"""Verify the ADM (Administrator) PIN specified as argument. This is typically needed in order
-to get write/update permissions to most of the files on SIM cards.
-
-Currently only ADM1 is supported."""
+ to get write/update permissions to most of the files on SIM cards. Currently only ADM1 is supported.
+ """
if opts.ADM1:
# use specified ADM-PIN
pin_adm = sanitize_pin_adm(opts.ADM1)
--
To view, visit https://gerrit.osmocom.org/c/pysim/+/37611?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: I101868a6f0220b62977c5e633df2607467cfba91
Gerrit-Change-Number: 37611
Gerrit-PatchSet: 2
Gerrit-Owner: dexter <pmaier(a)sysmocom.de>
Gerrit-Reviewer: Jenkins Builder
Gerrit-Reviewer: laforge <laforge(a)osmocom.org>
Gerrit-MessageType: merged