From 7d3953d48d92430aa503cb6758b04ddb9c7fe71b Mon Sep 17 00:00:00 2001 From: hploetz Date: Wed, 17 May 2006 18:50:18 +0000 Subject: [PATCH] nicer status word printing git-svn-id: svn+ssh://localhost/home/henryk/svn/cyberflex-shell/trunk@47 f711b948-2313-0410-aaa9-d29f33439f0b --- cards/generic_card.py | 18 +++++++++++++----- cyberflex-shell.py | 6 ++++-- 2 files changed, 17 insertions(+), 7 deletions(-) diff --git a/cards/generic_card.py b/cards/generic_card.py index 1c720d0..0abbfe7 100644 --- a/cards/generic_card.py +++ b/cards/generic_card.py @@ -122,22 +122,30 @@ class Card: def decode_statusword(self): if self.last_sw is None: return "No command executed so far" - else: + else: + retval = None + desc = self.STATUS_WORDS.get(self.last_sw) if desc is not None: - return desc + retval = desc else: target = binascii.b2a_hex(self.last_sw).upper() for (key, value) in self.STATUS_WORDS.items(): if fnmatch.fnmatch(target, key): if isinstance(value, str): - return value % { "SW1": ord(self.last_sw[0]), + retval = value % { "SW1": ord(self.last_sw[0]), "SW2": ord(self.last_sw[1]) } + break + elif callable(value): - return value( ord(self.last_sw[0]), + retval = value( ord(self.last_sw[0]), ord(self.last_sw[1]) ) + break - return "Unknown SW: %s" % binascii.b2a_hex(self.last_sw) + if retval is None: + return "%s: Unknown SW" % binascii.b2a_hex(self.last_sw) + else: + return "%s: %s" % (binascii.b2a_hex(self.last_sw), retval) def get_protocol(self): return ((self.card.status()["Protocol"] == pycsc.SCARD_PROTOCOL_T0) and (0,) or (1,))[0] diff --git a/cyberflex-shell.py b/cyberflex-shell.py index f7a0baa..d328af1 100755 --- a/cyberflex-shell.py +++ b/cyberflex-shell.py @@ -92,13 +92,15 @@ if __name__ == "__main__": apdu_binary = binascii.a2b_hex("".join(apdu_string.split())) apdu = utils.APDU(apdu_binary) response = card.send_apdu(apdu) - print utils.hexdump(response) + + if len(response) > 2: ## The SW is already printed by _print_sw as a post_hook + print utils.hexdump(response[:-2]) shell.fallback = do_raw_apdu def _print_sw(): if card.sw_changed: - print card.decode_statusword() + print "\n", card.decode_statusword() shell.register_post_hook(_print_sw) shell.run()