Change in pysim[master]: shell: add edit_{record, binary}_decoded shell commands

This is merely a historical archive of years 2008-2021, before the migration to mailman3.

A maintained and still updated list archive can be found at https://lists.osmocom.org/hyperkitty/list/gerrit-log@lists.osmocom.org/.

laforge gerrit-no-reply at lists.osmocom.org
Sat Apr 10 22:12:56 UTC 2021


laforge has submitted this change. ( https://gerrit.osmocom.org/c/pysim/+/23681 )

Change subject: shell: add edit_{record,binary}_decoded shell commands
......................................................................

shell: add edit_{record,binary}_decoded shell commands

This allows the user to edit the file/record contents in its
JSON representation inside the standard system text editor.

Change-Id: Icf6a6e8529e7664c5645519fb4bdd55b35f34664
---
M docs/shell.rst
M pySim/filesystem.py
2 files changed, 78 insertions(+), 0 deletions(-)

Approvals:
  Jenkins Builder: Verified
  laforge: Looks good to me, approved



diff --git a/docs/shell.rst b/docs/shell.rst
index 652e572..06e7f42 100644
--- a/docs/shell.rst
+++ b/docs/shell.rst
@@ -206,6 +206,24 @@
    :func: LinFixedEF.ShellCommands.upd_rec_dec_parser
 
 
+edit_record_decoded
+~~~~~~~~~~~~~~~~~~~
+.. argparse::
+   :module: pySim.filesystem
+   :func: LinFixedEF.ShellCommands.edit_rec_dec_parser
+
+This command will read the selected record, decode it to its JSON representation, save
+that JSON to a temporary file on your computer, and launch your configured text editor.
+
+You may then perform whatever modifications to the JSON representation, save + leave your
+text editor.
+
+Afterwards, the modified JSON will be re-encoded to the binary format, and the result written
+back to the record on the SIM card.
+
+This allows for easy interactive modification of records.
+
+
 
 Transparent EF commands
 -----------------------
@@ -268,6 +286,19 @@
   }
 
 
+edit_binary_decoded
+~~~~~~~~~~~~~~~~~~~
+This command will read the selected binary EF, decode it to its JSON representation, save
+that JSON to a temporary file on your computer, and launch your configured text editor.
+
+You may then perform whatever modifications to the JSON representation, save + leave your
+text editor.
+
+Afterwards, the modified JSON will be re-encoded to the binary format, and the result written
+to the SIM card.
+
+This allows for easy interactive modification of file contents.
+
 
 cmd2 settable parameters
 ------------------------
diff --git a/pySim/filesystem.py b/pySim/filesystem.py
index d2b8443..e771f3c 100644
--- a/pySim/filesystem.py
+++ b/pySim/filesystem.py
@@ -25,6 +25,7 @@
 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
 import code
+import tempfile
 import json
 
 import cmd2
@@ -433,6 +434,26 @@
             if data:
                 self._cmd.poutput_json(data)
 
+        def do_edit_binary_decoded(self, opts):
+            """Edit the JSON representation of the EF contents in an editor."""
+            (orig_json, sw) = self._cmd.rs.read_binary_dec()
+            with tempfile.TemporaryDirectory(prefix='pysim_') as dirname:
+                filename = '%s/file' % dirname
+                # write existing data as JSON to file
+                with open(filename, 'w') as text_file:
+                    json.dump(orig_json, text_file, indent=4)
+                # run a text editor
+                self._cmd._run_editor(filename)
+                with open(filename, 'r') as text_file:
+                    edited_json = json.load(text_file)
+                if edited_json == orig_json:
+                    self._cmd.poutput("Data not modified, skipping write")
+                else:
+                    (data, sw) = self._cmd.rs.update_binary_dec(edited_json)
+                    if data:
+                        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}):
         """
@@ -622,6 +643,32 @@
             if data:
                 self._cmd.poutput(data)
 
+        edit_rec_dec_parser = argparse.ArgumentParser()
+        edit_rec_dec_parser.add_argument('record_nr', type=int, help='Number of record to be edited')
+        @cmd2.with_argparser(edit_rec_dec_parser)
+        def do_edit_record_decoded(self, opts):
+            """Edit the JSON representation of one record in an editor."""
+            (orig_json, sw) = self._cmd.rs.read_record_dec(opts.record_nr)
+            dirname = tempfile.mkdtemp(prefix='pysim_')
+            try:
+                filename = '%s/file' % dirname
+                # write existing data as JSON to file
+                with open(filename, 'w') as text_file:
+                    json.dump(orig_json, text_file, indent=4)
+                # run a text editor
+                self._cmd._run_editor(filename)
+                with open(filename, 'r') as text_file:
+                    edited_json = json.load(text_file)
+                if edited_json == orig_json:
+                    self._cmd.poutput("Data not modified, skipping write")
+                else:
+                    (data, sw) = self._cmd.rs.update_record_dec(opts.record_nr, edited_json)
+                    if data:
+                        self._cmd.poutput_json(data)
+            finally:
+                shutil.rmtree(dirname)
+
+
     def __init__(self, fid:str, sfid:str=None, name:str=None, desc:str=None,
                  parent:Optional[CardDF]=None, rec_len={1,None}):
         """

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

Gerrit-Project: pysim
Gerrit-Branch: master
Gerrit-Change-Id: Icf6a6e8529e7664c5645519fb4bdd55b35f34664
Gerrit-Change-Number: 23681
Gerrit-PatchSet: 5
Gerrit-Owner: laforge <laforge at osmocom.org>
Gerrit-Reviewer: Jenkins Builder
Gerrit-Reviewer: dexter <pmaier at sysmocom.de>
Gerrit-Reviewer: fixeria <vyanitskiy at sysmocom.de>
Gerrit-Reviewer: laforge <laforge at osmocom.org>
Gerrit-Reviewer: tnt <tnt at 246tNt.com>
Gerrit-MessageType: merged
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osmocom.org/pipermail/gerrit-log/attachments/20210410/a3365c42/attachment.htm>


More information about the gerrit-log mailing list