Change in pysim[master]: pySim-shell: add "dir" command.

laforge gerrit-no-reply at lists.osmocom.org
Wed Mar 3 07:51:43 UTC 2021


laforge has uploaded this change for review. ( https://gerrit.osmocom.org/c/pysim/+/23207 )


Change subject: pySim-shell: add "dir" command.
......................................................................

pySim-shell: add "dir" command.

pysim-shell does not have a convinient way to list the files available
in one directory. Tab completion currently is the only way to obtain a
list of the available files. Lets add a dir command to print a file
list.

Change-Id: Ic06a60e0a0ec54d9bb26e151453ceb49d69e3df7
Related: OS#4963
---
M pySim-shell.py
M pySim/utils.py
2 files changed, 37 insertions(+), 1 deletion(-)



  git pull ssh://gerrit.osmocom.org:29418/pysim refs/changes/07/23207/1

diff --git a/pySim-shell.py b/pySim-shell.py
index ce9630a..a254121 100755
--- a/pySim-shell.py
+++ b/pySim-shell.py
@@ -38,7 +38,7 @@
 from pySim.commands import SimCardCommands
 from pySim.cards import card_detect, Card
 from pySim.utils import h2b, swap_nibbles, rpad, h2s
-from pySim.utils import dec_st, init_reader, sanitize_pin_adm
+from pySim.utils import dec_st, init_reader, sanitize_pin_adm, tabulate_str_list
 from pySim.card_handler import card_handler
 
 from pySim.filesystem import CardMF, RuntimeState
@@ -112,6 +112,24 @@
 		(data, sw) = self._cmd.card._scc.verify_chv(opts.chv_nr, opts.code)
 		self._cmd.poutput(data)
 
+	dir_parser = argparse.ArgumentParser()
+	dir_parser.add_argument('--fids', help='Show file identifiers', action='store_true')
+
+	@cmd2.with_argparser(dir_parser)
+	def do_dir(self, opts):
+		"""Show a listing of files available in currently selected DF or MF"""
+		if opts.fids:
+			files = self._cmd.rs.selected_file.get_selectable_names(flags = ['FIDS', 'SELF', 'PARENT', 'APPS'])
+		else:
+			files = self._cmd.rs.selected_file.get_selectable_names(flags = ['NAMES', 'SELF', 'PARENT', 'APPS'])
+		file_list = list(files)
+		directory_str = tabulate_str_list(file_list, width = 79, hspace = 2, lspace = 1, align_left = True)
+		path_list = self._cmd.rs.selected_file.fully_qualified_path(True)
+		self._cmd.poutput('/'.join(path_list))
+		path_list = self._cmd.rs.selected_file.fully_qualified_path(False)
+		self._cmd.poutput('/'.join(path_list))
+		self._cmd.poutput(directory_str)
+		self._cmd.poutput("%d files" % len(file_list))
 
 
 
diff --git a/pySim/utils.py b/pySim/utils.py
index 5320b59..8a49087 100644
--- a/pySim/utils.py
+++ b/pySim/utils.py
@@ -777,3 +777,21 @@
 		else:
 			sw_masked = sw_masked + sw_lower[i]
 	return sw_masked == pattern
+
+def tabulate_str_list(str_list, width = 79, hspace = 2, lspace = 1, align_left = True):
+	"""Pretty print a list of strings into a tabulated form"""
+	longest_str = max(str_list, key=len)
+	cellwith = len(longest_str) + hspace
+	cols = width // cellwith
+	rows = (len(str_list) - 1) // cols + 1
+	table = []
+	for i in iter(range(rows)):
+		str_list_row = str_list[i::rows]
+		if (align_left):
+			format_str_cell = '%%-%ds'
+		else:
+			format_str_cell = '%%%ds'
+		format_str_row = (format_str_cell % cellwith) * len(str_list_row)
+		format_str_row = (" " * lspace) + format_str_row
+		table.append(format_str_row % tuple(str_list_row))
+	return '\n'.join(table)

-- 
To view, visit https://gerrit.osmocom.org/c/pysim/+/23207
To unsubscribe, or for help writing mail filters, visit https://gerrit.osmocom.org/settings

Gerrit-Project: pysim
Gerrit-Branch: master
Gerrit-Change-Id: Ic06a60e0a0ec54d9bb26e151453ceb49d69e3df7
Gerrit-Change-Number: 23207
Gerrit-PatchSet: 1
Gerrit-Owner: laforge <laforge at osmocom.org>
Gerrit-MessageType: newchange
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osmocom.org/pipermail/gerrit-log/attachments/20210303/00177fe2/attachment.htm>


More information about the gerrit-log mailing list