Change in pysim[master]: pySim-shell: Add settable parameter on JSON pretty-printing

laforge gerrit-no-reply at lists.osmocom.org
Tue Apr 6 19:16:26 UTC 2021


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


Change subject: pySim-shell: Add settable parameter on JSON pretty-printing
......................................................................

pySim-shell: Add settable parameter on JSON pretty-printing

Change-Id: Ic095c96733de2b0f359bfe067cd719d38712faff
---
M docs/shell.rst
M pySim-shell.py
M pySim/filesystem.py
3 files changed, 21 insertions(+), 11 deletions(-)



  git pull ssh://gerrit.osmocom.org:29418/pysim refs/changes/59/23659/1

diff --git a/docs/shell.rst b/docs/shell.rst
index 314f633..ddb21d5 100644
--- a/docs/shell.rst
+++ b/docs/shell.rst
@@ -208,6 +208,14 @@
 
 If disabled, pySim will always write irrespective of the current/new value.
 
+json_pretty_print
+~~~~~~~~~~~~~~~~~
+
+This parameter determines if generated JSON output should (by default) be pretty-printed (multi-line
+output with indent level of 4 spaces) or not.
+
+The default value of this parameter is 'true'.
+
 debug
 ~~~~~
 
diff --git a/pySim-shell.py b/pySim-shell.py
index e73ec35..7f48d58 100755
--- a/pySim-shell.py
+++ b/pySim-shell.py
@@ -72,6 +72,16 @@
 		self.add_settable(cmd2.Settable('conserve_write', bool, 'Read and compare before write',
 						  onchange_cb=self._onchange_conserve_write))
 		self.update_prompt()
+		self.json_pretty_print = True
+		self.add_settable(cmd2.Settable('json_pretty_print', bool, 'Pretty-Print JSON output'))
+
+	def poutput_json(self, data, force_no_pretty = False):
+		"""line cmd2.putput() but for a json serializable dict."""
+		if force_no_pretty or self.json_pretty_print == False:
+			output = json.dumps(data)
+		else:
+			output = json.dumps(data, indent=4)
+		self.poutput(output)
 
 	def _onchange_numeric_path(self, param_name, old, new):
 		self.update_prompt()
diff --git a/pySim/filesystem.py b/pySim/filesystem.py
index 73348e1..ab9b2f3 100644
--- a/pySim/filesystem.py
+++ b/pySim/filesystem.py
@@ -404,11 +404,7 @@
         def do_read_binary_decoded(self, opts):
             """Read + decode data from a transparent EF"""
             (data, sw) = self._cmd.rs.read_binary_dec()
-            if opts.oneline:
-                output = json.dumps(data)
-            else:
-                output = json.dumps(data, indent=4)
-            self._cmd.poutput(output)
+            self._cmd.poutput_json(data, opts.oneline)
 
         upd_bin_parser = argparse.ArgumentParser()
         upd_bin_parser.add_argument('--offset', type=int, default=0, help='Byte offset for start of read')
@@ -428,7 +424,7 @@
             data_json = json.loads(opts.data)
             (data, sw) = self._cmd.rs.update_binary_dec(data_json)
             if data:
-                self._cmd.poutput(json.dumps(data, indent=4))
+                self._cmd.poutput_json(data)
 
     def __init__(self, fid:str, sfid:str=None, name:str=None, desc:str=None, parent:CardDF=None,
                  size={1,None}):
@@ -563,11 +559,7 @@
         def do_read_record_decoded(self, opts):
             """Read + decode a record from a record-oriented EF"""
             (data, sw) = self._cmd.rs.read_record_dec(opts.record_nr)
-            if opts.oneline:
-                output = json.dumps(data)
-            else:
-                output = json.dumps(data, indent=4)
-            self._cmd.poutput(output)
+            self._cmd.poutput_json(data, opts.oneline)
 
         upd_rec_parser = argparse.ArgumentParser()
         upd_rec_parser.add_argument('record_nr', type=int, help='Number of record to be read')

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

Gerrit-Project: pysim
Gerrit-Branch: master
Gerrit-Change-Id: Ic095c96733de2b0f359bfe067cd719d38712faff
Gerrit-Change-Number: 23659
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/20210406/c7888341/attachment.htm>


More information about the gerrit-log mailing list