<p>laforge has uploaded this change for <strong>review</strong>.</p><p><a href="https://gerrit.osmocom.org/c/pysim/+/23681">View Change</a></p><pre style="font-family: monospace,monospace; white-space: pre-wrap;">shell: add edit_{record,binary}_decoded shell commands<br><br>This allows the user to edit the file/record contents in its<br>JSON representation inside the standard system text editor.<br><br>Change-Id: Icf6a6e8529e7664c5645519fb4bdd55b35f34664<br>---<br>M docs/shell.rst<br>M pySim/filesystem.py<br>2 files changed, 86 insertions(+), 0 deletions(-)<br><br></pre><pre style="font-family: monospace,monospace; white-space: pre-wrap;">git pull ssh://gerrit.osmocom.org:29418/pysim refs/changes/81/23681/1</pre><pre style="font-family: monospace,monospace; white-space: pre-wrap;"><span>diff --git a/docs/shell.rst b/docs/shell.rst</span><br><span>index 652e572..3ea6c56 100644</span><br><span>--- a/docs/shell.rst</span><br><span>+++ b/docs/shell.rst</span><br><span>@@ -206,6 +206,24 @@</span><br><span>    :func: LinFixedEF.ShellCommands.upd_rec_dec_parser</span><br><span> </span><br><span> </span><br><span style="color: hsl(120, 100%, 40%);">+edit_record_decoded</span><br><span style="color: hsl(120, 100%, 40%);">+~~~~~~~~~~~~~~~~~~~</span><br><span style="color: hsl(120, 100%, 40%);">+.. argparse::</span><br><span style="color: hsl(120, 100%, 40%);">+   :module: pySim.filesystem</span><br><span style="color: hsl(120, 100%, 40%);">+   :func: LinFixedEF.ShellCommands.edit_rec_dec_parser</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+This command will read the selected record, decode it to its JSON representation, save</span><br><span style="color: hsl(120, 100%, 40%);">+that JSON to a temporary file on your computer, and launch your configured text editor.</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+You may then perform whatever modifications to the JSON representation, save + leave your</span><br><span style="color: hsl(120, 100%, 40%);">+text editor.</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+Afterwards, the modified JSON will be re-encoded to the binary format, and the result written</span><br><span style="color: hsl(120, 100%, 40%);">+back to the record on the SIM card.</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+This allows for easy interactive modification of records.</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span> </span><br><span> Transparent EF commands</span><br><span> -----------------------</span><br><span>@@ -268,6 +286,23 @@</span><br><span>   }</span><br><span> </span><br><span> </span><br><span style="color: hsl(120, 100%, 40%);">+edit_binary_decoded</span><br><span style="color: hsl(120, 100%, 40%);">+~~~~~~~~~~~~~~~~~~~</span><br><span style="color: hsl(120, 100%, 40%);">+.. argparse::</span><br><span style="color: hsl(120, 100%, 40%);">+   :module: pySim.filesystem</span><br><span style="color: hsl(120, 100%, 40%);">+   :func: TransparentEF.ShellCommands.edit_bin_dec_parser</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+This command will read the selected binary EF, decode it to its JSON representation, save</span><br><span style="color: hsl(120, 100%, 40%);">+that JSON to a temporary file on your computer, and launch your configured text editor.</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+You may then perform whatever modifications to the JSON representation, save + leave your</span><br><span style="color: hsl(120, 100%, 40%);">+text editor.</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+Afterwards, the modified JSON will be re-encoded to the binary format, and the result written</span><br><span style="color: hsl(120, 100%, 40%);">+to the SIM card.</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+This allows for easy interactive modification of file contents.</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span> </span><br><span> cmd2 settable parameters</span><br><span> ------------------------</span><br><span>diff --git a/pySim/filesystem.py b/pySim/filesystem.py</span><br><span>index e97fbf6..ad93cff 100644</span><br><span>--- a/pySim/filesystem.py</span><br><span>+++ b/pySim/filesystem.py</span><br><span>@@ -25,6 +25,8 @@</span><br><span> # along with this program.  If not, see <http://www.gnu.org/licenses/>.</span><br><span> </span><br><span> import code</span><br><span style="color: hsl(120, 100%, 40%);">+import tempfile</span><br><span style="color: hsl(120, 100%, 40%);">+import shutil</span><br><span> import json</span><br><span> </span><br><span> import cmd2</span><br><span>@@ -433,6 +435,29 @@</span><br><span>             if data:</span><br><span>                 self._cmd.poutput_json(data)</span><br><span> </span><br><span style="color: hsl(120, 100%, 40%);">+        def do_edit_binary_decoded(self, opts):</span><br><span style="color: hsl(120, 100%, 40%);">+            """Edit the JSON representation of the EF contents in an editor."""</span><br><span style="color: hsl(120, 100%, 40%);">+            (orig_json, sw) = self._cmd.rs.read_binary_dec()</span><br><span style="color: hsl(120, 100%, 40%);">+            dirname = tempfile.mkdtemp(prefix='pysim_')</span><br><span style="color: hsl(120, 100%, 40%);">+            try:</span><br><span style="color: hsl(120, 100%, 40%);">+                filename = '%s/file' % dirname</span><br><span style="color: hsl(120, 100%, 40%);">+                # write existing data as JSON to file</span><br><span style="color: hsl(120, 100%, 40%);">+                with open(filename, 'w') as text_file:</span><br><span style="color: hsl(120, 100%, 40%);">+                    json.dump(orig_json, text_file, indent=4)</span><br><span style="color: hsl(120, 100%, 40%);">+                # run a text editor</span><br><span style="color: hsl(120, 100%, 40%);">+                self._cmd._run_editor(filename)</span><br><span style="color: hsl(120, 100%, 40%);">+                with open(filename, 'r') as text_file:</span><br><span style="color: hsl(120, 100%, 40%);">+                    edited_json = json.load(text_file)</span><br><span style="color: hsl(120, 100%, 40%);">+                if edited_json == orig_json:</span><br><span style="color: hsl(120, 100%, 40%);">+                    self._cmd.poutput("Data not modified, skipping write")</span><br><span style="color: hsl(120, 100%, 40%);">+                else:</span><br><span style="color: hsl(120, 100%, 40%);">+                    (data, sw) = self._cmd.rs.update_binary_dec(edited_json)</span><br><span style="color: hsl(120, 100%, 40%);">+                    if data:</span><br><span style="color: hsl(120, 100%, 40%);">+                        self._cmd.poutput_json(data)</span><br><span style="color: hsl(120, 100%, 40%);">+            finally:</span><br><span style="color: hsl(120, 100%, 40%);">+                shutil.rmtree(dirname)</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span>     def __init__(self, fid:str, sfid:str=None, name:str=None, desc:str=None, parent:CardDF=None,</span><br><span>                  size={1,None}):</span><br><span>         """</span><br><span>@@ -622,6 +647,32 @@</span><br><span>             if data:</span><br><span>                 self._cmd.poutput(data)</span><br><span> </span><br><span style="color: hsl(120, 100%, 40%);">+        edit_rec_dec_parser = argparse.ArgumentParser()</span><br><span style="color: hsl(120, 100%, 40%);">+        edit_rec_dec_parser.add_argument('record_nr', type=int, help='Number of record to be edited')</span><br><span style="color: hsl(120, 100%, 40%);">+        @cmd2.with_argparser(edit_rec_dec_parser)</span><br><span style="color: hsl(120, 100%, 40%);">+        def do_edit_record_decoded(self, opts):</span><br><span style="color: hsl(120, 100%, 40%);">+            """Edit the JSON representation of one record in an editor."""</span><br><span style="color: hsl(120, 100%, 40%);">+            (orig_json, sw) = self._cmd.rs.read_record_dec(opts.record_nr)</span><br><span style="color: hsl(120, 100%, 40%);">+            dirname = tempfile.mkdtemp(prefix='pysim_')</span><br><span style="color: hsl(120, 100%, 40%);">+            try:</span><br><span style="color: hsl(120, 100%, 40%);">+                filename = '%s/file' % dirname</span><br><span style="color: hsl(120, 100%, 40%);">+                # write existing data as JSON to file</span><br><span style="color: hsl(120, 100%, 40%);">+                with open(filename, 'w') as text_file:</span><br><span style="color: hsl(120, 100%, 40%);">+                    json.dump(orig_json, text_file, indent=4)</span><br><span style="color: hsl(120, 100%, 40%);">+                # run a text editor</span><br><span style="color: hsl(120, 100%, 40%);">+                self._cmd._run_editor(filename)</span><br><span style="color: hsl(120, 100%, 40%);">+                with open(filename, 'r') as text_file:</span><br><span style="color: hsl(120, 100%, 40%);">+                    edited_json = json.load(text_file)</span><br><span style="color: hsl(120, 100%, 40%);">+                if edited_json == orig_json:</span><br><span style="color: hsl(120, 100%, 40%);">+                    self._cmd.poutput("Data not modified, skipping write")</span><br><span style="color: hsl(120, 100%, 40%);">+                else:</span><br><span style="color: hsl(120, 100%, 40%);">+                    (data, sw) = self._cmd.rs.update_record_dec(opts.record_nr, edited_json)</span><br><span style="color: hsl(120, 100%, 40%);">+                    if data:</span><br><span style="color: hsl(120, 100%, 40%);">+                        self._cmd.poutput_json(data)</span><br><span style="color: hsl(120, 100%, 40%);">+            finally:</span><br><span style="color: hsl(120, 100%, 40%);">+                shutil.rmtree(dirname)</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span>     def __init__(self, fid:str, sfid:str=None, name:str=None, desc:str=None,</span><br><span>                  parent:Optional[CardDF]=None, rec_len={1,None}):</span><br><span>         """</span><br><span></span><br></pre><p>To view, visit <a href="https://gerrit.osmocom.org/c/pysim/+/23681">change 23681</a>. To unsubscribe, or for help writing mail filters, visit <a href="https://gerrit.osmocom.org/settings">settings</a>.</p><div itemscope itemtype="http://schema.org/EmailMessage"><div itemscope itemprop="action" itemtype="http://schema.org/ViewAction"><link itemprop="url" href="https://gerrit.osmocom.org/c/pysim/+/23681"/><meta itemprop="name" content="View Change"/></div></div>

<div style="display:none"> Gerrit-Project: pysim </div>
<div style="display:none"> Gerrit-Branch: master </div>
<div style="display:none"> Gerrit-Change-Id: Icf6a6e8529e7664c5645519fb4bdd55b35f34664 </div>
<div style="display:none"> Gerrit-Change-Number: 23681 </div>
<div style="display:none"> Gerrit-PatchSet: 1 </div>
<div style="display:none"> Gerrit-Owner: laforge <laforge@osmocom.org> </div>
<div style="display:none"> Gerrit-MessageType: newchange </div>