pySim-shell: separate export summary with a headline

the export summary is printed after the log entry for the last file
without separation. This is confusing because it looks like if the
summary would refer to the last file only. Lets add a headline to make
clear that the last few lines are the "Export summary"

Change-Id: I90771e525b2b114bdb41a8e90d298ca991c09c3d
Related: OS#4963
This commit is contained in:
Philipp Maier 2021-04-19 21:24:23 +02:00
parent 04be9d6033
commit 80ce71f58c
2 changed files with 18 additions and 5 deletions

View File

@ -39,7 +39,7 @@ from pySim.commands import SimCardCommands
from pySim.transport import init_reader, ApduTracer
from pySim.cards import card_detect, Card
from pySim.utils import h2b, swap_nibbles, rpad, h2s, JsonEncoder
from pySim.utils import dec_st, sanitize_pin_adm, tabulate_str_list, is_hex
from pySim.utils import dec_st, sanitize_pin_adm, tabulate_str_list, is_hex, boxed_heading_str
from pySim.card_handler import card_handler
from pySim.filesystem import CardMF, RuntimeState, CardDF, CardADF
@ -237,10 +237,8 @@ class PySimCommands(CommandSet):
df_path_list = df.fully_qualified_path(True)
df_path_list_fid = df.fully_qualified_path(False)
self._cmd.poutput("#" * 80)
file_str = '/'.join(df_path_list) + "/" + str(filename) + " " * 80
self._cmd.poutput("# " + file_str[0:77] + "#")
self._cmd.poutput("#" * 80)
file_str = '/'.join(df_path_list) + "/" + str(filename)
self._cmd.poutput(boxed_heading_str(file_str))
self._cmd.poutput("# directory: %s (%s)" % ('/'.join(df_path_list), '/'.join(df_path_list_fid)))
try:
@ -288,6 +286,9 @@ class PySimCommands(CommandSet):
self.export(opts.filename, context)
else:
self.walk(0, self.export, context)
self._cmd.poutput(boxed_heading_str("Export summary"))
self._cmd.poutput("# total files visited: %u" % context['COUNT'])
self._cmd.poutput("# bad files: %u" % context['ERR'])
for b in context['BAD']:

View File

@ -841,3 +841,15 @@ class JsonEncoder(json.JSONEncoder):
if isinstance(o, BytesIO) or isinstance(o, bytes) or isinstance(o, bytearray):
return b2h(o)
return json.JSONEncoder.default(self, o)
def boxed_heading_str(heading, width=80):
"""Generate a string that contains a boxed heading."""
# Auto-enlarge box if heading exceeds length
if len(heading) > width - 4:
width = len(heading) + 4
res = "#" * width
fstr = "\n# %-" + str(width - 4) + "s #\n"
res += fstr % (heading)
res += "#" * width
return res