nicer status word printing

git-svn-id: svn+ssh://localhost/home/henryk/svn/cyberflex-shell/trunk@47 f711b948-2313-0410-aaa9-d29f33439f0b
This commit is contained in:
hploetz 2006-05-17 18:50:18 +00:00
parent a2d6c6a624
commit 7d3953d48d
2 changed files with 17 additions and 7 deletions

View File

@ -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]

View File

@ -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()