Change in pysim[master]: Move reader related argument parser to transport module

laforge gerrit-no-reply at lists.osmocom.org
Thu Apr 22 09:27:26 UTC 2021


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

Change subject: Move reader related argument parser to transport module
......................................................................

Move reader related argument parser to transport module

Ideally that shared definition would be used by all programs,
rather than copy+pasting it.  Unfortunately pySim-{read,prog}
are still using optparse and first need to be converted to
argparse.

Change-Id: If77f53850e1ca65f42cf1dca3e0f460dac1b0d1a
---
M pySim-shell.py
M pySim/transport/__init__.py
2 files changed, 29 insertions(+), 24 deletions(-)

Approvals:
  Jenkins Builder: Verified
  laforge: Looks good to me, approved
  fixeria: Looks good to me, but someone else must approve
  dexter: Looks good to me, but someone else must approve



diff --git a/pySim-shell.py b/pySim-shell.py
index d622128..659c12c 100755
--- a/pySim-shell.py
+++ b/pySim-shell.py
@@ -36,7 +36,7 @@
 
 from pySim.exceptions import *
 from pySim.commands import SimCardCommands
-from pySim.transport import init_reader, ApduTracer
+from pySim.transport import init_reader, ApduTracer, argparse_add_reader_args
 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, boxed_heading_str
@@ -432,32 +432,12 @@
 
 option_parser = argparse.ArgumentParser(prog='pySim-shell', description='interactive SIM card shell',
                                         formatter_class=argparse.ArgumentDefaultsHelpFormatter)
-
-serial_group = option_parser.add_argument_group('Serial Reader')
-serial_group.add_argument('-d', '--device', metavar='DEV', default='/dev/ttyUSB0',
-                          help='Serial Device for SIM access')
-serial_group.add_argument('-b', '--baud', dest='baudrate', type=int, metavar='BAUD', default=9600,
-                          help='Baud rate used for SIM access')
-
-pcsc_group = option_parser.add_argument_group('PC/SC Reader')
-pcsc_group.add_argument('-p', '--pcsc-device', type=int, dest='pcsc_dev', metavar='PCSC', default=None,
-                        help='PC/SC reader number to use for SIM access')
-
-modem_group = option_parser.add_argument_group('AT Command Modem Reader')
-modem_group.add_argument('--modem-device', dest='modem_dev', metavar='DEV', default=None,
-                         help='Serial port of modem for Generic SIM Access (3GPP TS 27.007)')
-modem_group.add_argument('--modem-baud', type=int, metavar='BAUD', default=115200,
-                         help='Baud rate used for modem port')
-
-osmobb_group = option_parser.add_argument_group('OsmocomBB Reader')
-osmobb_group.add_argument('--osmocon', dest='osmocon_sock', metavar='PATH', default=None,
-                           help='Socket path for Calypso (e.g. Motorola C1XX) based reader (via OsmocomBB)')
+argparse_add_reader_args(option_parser)
 
 global_group = option_parser.add_argument_group('General Options')
 global_group.add_argument('--script', metavar='PATH', default=None,
-                           help='script with pySim-shell commands to be executed automatically at start-up')
-global_group.add_argument('--csv', metavar='FILE', default=None,
-                           help='Read card data from CSV file')
+                          help='script with pySim-shell commands to be executed automatically at start-up')
+global_group.add_argument('--csv', metavar='FILE', default=None, help='Read card data from CSV file')
 
 adm_group = global_group.add_mutually_exclusive_group()
 adm_group.add_argument('-a', '--pin-adm', metavar='PIN_ADM1', dest='pin_adm', default=None,
diff --git a/pySim/transport/__init__.py b/pySim/transport/__init__.py
index 290bc7c..d4f7f3a 100644
--- a/pySim/transport/__init__.py
+++ b/pySim/transport/__init__.py
@@ -3,6 +3,7 @@
 """ pySim: PCSC reader transport link base
 """
 
+import argparse
 from typing import Optional
 
 from pySim.exceptions import *
@@ -175,6 +176,30 @@
 			raise SwMatchError(sw, sw_exp.lower(), self.sw_interpreter)
 		return (rsp, sw)
 
+def argparse_add_reader_args(arg_parser):
+	"""Add all reader related arguments to the given argparse.Argumentparser instance."""
+	serial_group = arg_parser.add_argument_group('Serial Reader')
+	serial_group.add_argument('-d', '--device', metavar='DEV', default='/dev/ttyUSB0',
+							  help='Serial Device for SIM access')
+	serial_group.add_argument('-b', '--baud', dest='baudrate', type=int, metavar='BAUD', default=9600,
+							  help='Baud rate used for SIM access')
+
+	pcsc_group = arg_parser.add_argument_group('PC/SC Reader')
+	pcsc_group.add_argument('-p', '--pcsc-device', type=int, dest='pcsc_dev', metavar='PCSC', default=None,
+							help='PC/SC reader number to use for SIM access')
+
+	modem_group = arg_parser.add_argument_group('AT Command Modem Reader')
+	modem_group.add_argument('--modem-device', dest='modem_dev', metavar='DEV', default=None,
+							 help='Serial port of modem for Generic SIM Access (3GPP TS 27.007)')
+	modem_group.add_argument('--modem-baud', type=int, metavar='BAUD', default=115200,
+							 help='Baud rate used for modem port')
+
+	osmobb_group = arg_parser.add_argument_group('OsmocomBB Reader')
+	osmobb_group.add_argument('--osmocon', dest='osmocon_sock', metavar='PATH', default=None,
+							  help='Socket path for Calypso (e.g. Motorola C1XX) based reader (via OsmocomBB)')
+
+	return arg_parser
+
 def init_reader(opts, **kwargs) -> Optional[LinkBase]:
 	"""
 	Init card reader driver

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

Gerrit-Project: pysim
Gerrit-Branch: master
Gerrit-Change-Id: If77f53850e1ca65f42cf1dca3e0f460dac1b0d1a
Gerrit-Change-Number: 23721
Gerrit-PatchSet: 3
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-MessageType: merged
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osmocom.org/pipermail/gerrit-log/attachments/20210422/8ab096c8/attachment.htm>


More information about the gerrit-log mailing list