From 289d2343fa0f8c2f6df153d24b85593933efd2c5 Mon Sep 17 00:00:00 2001 From: Harald Welte Date: Fri, 19 Jul 2024 18:21:12 +0200 Subject: [PATCH] pySim.apdu: Refactor cmd_to_dict() method Let's factor out the "automatic processing using _tlv / _construct" as a separate method. This way we enable a derived class to first call that automatic processing method, and then amend its output in a second step. Change-Id: I1f066c0f1502020c88d99026c25bf2e283c3b4f5 --- pySim/apdu/__init__.py | 34 +++++++++++++++++++--------------- 1 file changed, 19 insertions(+), 15 deletions(-) diff --git a/pySim/apdu/__init__.py b/pySim/apdu/__init__.py index 02ccbadc..6613e912 100644 --- a/pySim/apdu/__init__.py +++ b/pySim/apdu/__init__.py @@ -292,22 +292,26 @@ class ApduCommand(Apdu, metaclass=ApduCommandMeta): if callable(method): return method() else: - r = {} - method = getattr(self, '_decode_p1p2', None) - if callable(method): - r = self._decode_p1p2() + return self._cmd_to_dict() + + def _cmd_to_dict(self) -> Dict: + """back-end function performing automatic decoding using _construct / _tlv.""" + r = {} + method = getattr(self, '_decode_p1p2', None) + if callable(method): + r = self._decode_p1p2() + else: + r['p1'] = parse_construct(self._construct_p1, self.p1.to_bytes(1, 'big')) + r['p2'] = parse_construct(self._construct_p2, self.p2.to_bytes(1, 'big')) + r['p3'] = self.p3 + if self.cmd_data: + if self._tlv: + ie = self._tlv() + ie.from_tlv(self.cmd_data) + r['body'] = ie.to_dict() else: - r['p1'] = parse_construct(self._construct_p1, self.p1.to_bytes(1, 'big')) - r['p2'] = parse_construct(self._construct_p2, self.p2.to_bytes(1, 'big')) - r['p3'] = self.p3 - if self.cmd_data: - if self._tlv: - ie = self._tlv() - ie.from_tlv(self.cmd_data) - r['body'] = ie.to_dict() - else: - r['body'] = parse_construct(self._construct, self.cmd_data) - return r + r['body'] = parse_construct(self._construct, self.cmd_data) + return r def rsp_to_dict(self) -> Dict: """Convert the Response part of the APDU to a dict."""