From 91c971bf82648ef5073589aa50fac9d9cec9aa82 Mon Sep 17 00:00:00 2001 From: Philipp Maier Date: Mon, 9 Oct 2023 10:48:46 +0200 Subject: [PATCH] pySim-prog, pySim-shell do not use global variables When __main__ runs different variables get assigned. In particular opts, scc, sl and ch. Those variables are available in any scope and technically it is possible to access them. However, lets not do this since it leads to confusion. Also, pylint will complain about those code locations. In pySim-shell.py - Let's use the proper locations (sl and ch are stored in PysimApp. - Scc can be assigned in init_card. - In method walk, the use of the variable opts to call ection_df is wrong, lets use **kwargs (see also usage of action_ef). - The constructor of Cmd2ApduTracer has a parameter cmd2_app, but usese the global variable app. Let's use cmd2_app instead. In pySim-prog.py - Do not use opts.num in find_row_in_csv_file, use num instead. - Pass scc to process_card as parameter so that it won't access scc in the global scope. Change-Id: I7f09e9a6a6bfc658de75e86f7383ce73726f6666 Related: OS#6210 --- pySim-prog.py | 6 +++--- pySim-shell.py | 18 +++++++++--------- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/pySim-prog.py b/pySim-prog.py index c07bfd18..a16928c0 100755 --- a/pySim-prog.py +++ b/pySim-prog.py @@ -567,7 +567,7 @@ def find_row_in_csv_file(csv_file_name:str, num=None, iccid=None, imsi=None): for row in cr: # Pick a specific row by line number (num) if num is not None and iccid is None and imsi is None: - if opts.num == i: + if num == i: f.close() return row @@ -727,7 +727,7 @@ def save_batch(opts): fh.close() -def process_card(opts, first, ch): +def process_card(scc, opts, first, ch): # Connect transport ch.get(first) @@ -821,7 +821,7 @@ if __name__ == '__main__': while 1: try: - rc = process_card(opts, first, ch) + rc = process_card(scc, opts, first, ch) except (KeyboardInterrupt): print("") print("Terminated by user!") diff --git a/pySim-shell.py b/pySim-shell.py index c7b26074..3b6945cc 100755 --- a/pySim-shell.py +++ b/pySim-shell.py @@ -88,6 +88,9 @@ def init_card(sl): state object (rs) is required for all pySim-shell commands. """ + # Create command layer + scc = SimCardCommands(transport=sl) + # Wait up to three seconds for a card in reader and try to detect # the card type. print("Waiting for card...") @@ -276,7 +279,7 @@ class PysimApp(Cmd2Compat): class Cmd2ApduTracer(ApduTracer): def __init__(self, cmd2_app): - self.cmd2 = app + self.cmd2 = cmd2_app def trace_response(self, cmd, sw, resp): self.cmd2.poutput("-> %s %s" % (cmd[:10], cmd[10:])) @@ -307,7 +310,7 @@ class PysimApp(Cmd2Compat): if self.rs and self.rs.profile: for cmd_set in self.rs.profile.shell_cmdsets: self.unregister_command_set(cmd_set) - rs, card = init_card(sl) + rs, card = init_card(self.sl) self.equip(card, rs) apdu_cmd_parser = argparse.ArgumentParser() @@ -440,7 +443,7 @@ class PysimApp(Cmd2Compat): # In case of failure, try multiple times. for i in range(opts.tries): # fetch card into reader bay - ch.get(first) + self.ch.get(first) # if necessary execute an action before we start processing the card if(opts.pre_card_action): @@ -463,9 +466,9 @@ class PysimApp(Cmd2Compat): # Depending on success or failure, the card goes either in the "error" bin or in the # "done" bin. if rc < 0: - ch.error() + self.ch.error() else: - ch.done() + self.ch.done() # In most cases it is possible to proceed with the next card, but the # user may decide to halt immediately when an error occurs @@ -559,7 +562,7 @@ class PySimCommands(CommandSet): if isinstance(self._cmd.lchan.selected_file, CardDF): if action_df: - action_df(context, opts) + action_df(context, **kwargs) files = self._cmd.lchan.selected_file.get_selectables( flags=['FNAMES', 'ANAMES']) @@ -1015,9 +1018,6 @@ if __name__ == '__main__': if sl is None: exit(1) - # Create command layer - scc = SimCardCommands(transport=sl) - # Create a card handler (for bulk provisioning) if opts.card_handler_config: ch = CardHandlerAuto(None, opts.card_handler_config)