pylint: transport/modem_atcmd.py

pySim/transport/modem_atcmd.py:70:0: C0325: Unnecessary parens after 'assert' keyword (superfluous-parens)
pySim/transport/modem_atcmd.py:28:0: W0401: Wildcard import pySim.exceptions (wildcard-import)
pySim/transport/modem_atcmd.py:60:22: C0123: Use isinstance() rather than type() for a typecheck. (unidiomatic-typecheck)
pySim/transport/modem_atcmd.py:72:12: W0707: Consider explicitly re-raising using 'except Exception as exc' and 'raise ReaderError('Failed to send AT command: %s' % cmd) from exc' (raise-missing-from)
pySim/transport/modem_atcmd.py:120:12: R1705: Unnecessary "elif" after "return", remove the leading "el" from "elif" (no-else-return)
pySim/transport/modem_atcmd.py:138:8: W1201: Use lazy % formatting in logging functions (logging-not-lazy)
pySim/transport/modem_atcmd.py:170:12: W0707: Consider explicitly re-raising using 'except Exception as exc' and 'raise ReaderError('Failed to parse response from modem: %s' % rsp) from exc' (raise-missing-from)
pySim/transport/modem_atcmd.py:168:13: W0612: Unused variable 'rsp_pdu_len' (unused-variable)
pySim/transport/modem_atcmd.py:21:0: C0411: standard import "import time" should be placed before "import serial" (wrong-import-order)
pySim/transport/modem_atcmd.py:22:0: C0411: standard import "import re" should be placed before "import serial" (wrong-import-order)
pySim/transport/modem_atcmd.py:23:0: C0411: standard import "import argparse" should be placed before "import serial" (wrong-import-order)
pySim/transport/modem_atcmd.py:24:0: C0411: standard import "from typing import Optional" should be placed before "import serial" (wrong-import-order)
pySim/transport/modem_atcmd.py:28:0: W0614: Unused import(s) NoCardError and SwMatchError from wildcard import of pySim.exceptions (unused-wildcard-import)

Change-Id: I2c8994eabd973b65132af1030429b1021d0c20df
diff --git a/pySim/transport/modem_atcmd.py b/pySim/transport/modem_atcmd.py
index a05d6c4..5943e3a 100644
--- a/pySim/transport/modem_atcmd.py
+++ b/pySim/transport/modem_atcmd.py
@@ -17,15 +17,15 @@
 #
 
 import logging as log
-import serial
 import time
 import re
 import argparse
 from typing import Optional
+import serial
 
 from pySim.utils import Hexstr, ResTuple
 from pySim.transport import LinkBase
-from pySim.exceptions import *
+from pySim.exceptions import ReaderError, ProtocolError
 
 # HACK: if somebody needs to debug this thing
 # log.root.setLevel(log.DEBUG)
@@ -57,7 +57,7 @@
 
     def send_at_cmd(self, cmd, timeout=0.2, patience=0.002):
         # Convert from string to bytes, if needed
-        bcmd = cmd if type(cmd) is bytes else cmd.encode()
+        bcmd = cmd if isinstance(cmd, bytes) else cmd.encode()
         bcmd += b'\r'
 
         # Clean input buffer from previous/unexpected data
@@ -67,9 +67,9 @@
         log.debug('Sending AT command: %s', cmd)
         try:
             wlen = self._sl.write(bcmd)
-            assert(wlen == len(bcmd))
-        except:
-            raise ReaderError('Failed to send AT command: %s' % cmd)
+            assert wlen == len(bcmd)
+        except Exception as exc:
+            raise ReaderError('Failed to send AT command: %s' % cmd) from exc
 
         rsp = b''
         its = 1
@@ -91,8 +91,7 @@
                 break
             time.sleep(patience)
             its += 1
-        log.debug('Command took %0.6fs (%d cycles a %fs)',
-                  time.time() - t_start, its, patience)
+        log.debug('Command took %0.6fs (%d cycles a %fs)', time.time() - t_start, its, patience)
 
         if self._echo:
             # Skip echo chars
@@ -120,11 +119,10 @@
             if result[-1] == b'OK':
                 self._echo = False
                 return
-            elif result[-1] == b'AT\r\r\nOK':
+            if result[-1] == b'AT\r\r\nOK':
                 self._echo = True
                 return
-        raise ReaderError(
-            'Interface \'%s\' does not respond to \'AT\' command' % self._device)
+        raise ReaderError('Interface \'%s\' does not respond to \'AT\' command' % self._device)
 
     def reset_card(self):
         # Reset the modem, just to be sure
@@ -135,7 +133,7 @@
         if self.send_at_cmd('AT+CSIM=?') != [b'OK']:
             raise ReaderError('The modem does not seem to support SIM access')
 
-        log.info('Modem at \'%s\' is ready!' % self._device)
+        log.info('Modem at \'%s\' is ready!', self._device)
 
     def connect(self):
         pass  # Nothing to do really ...
@@ -165,9 +163,9 @@
         # Make sure that the response has format: b'+CSIM: %d,\"%s\"'
         try:
             result = re.match(b'\+CSIM: (\d+),\"([0-9A-F]+)\"', rsp)
-            (rsp_pdu_len, rsp_pdu) = result.groups()
-        except:
-            raise ReaderError('Failed to parse response from modem: %s' % rsp)
+            (_rsp_pdu_len, rsp_pdu) = result.groups()
+        except Exception as exc:
+            raise ReaderError('Failed to parse response from modem: %s' % rsp) from exc
 
         # TODO: make sure we have at least SW
         data = rsp_pdu[:-4].decode().lower()