Fix some additional code smells
No functional change intended.
This commit is contained in:
		@ -23,7 +23,7 @@
 | 
			
		||||
import sys
 | 
			
		||||
import argparse
 | 
			
		||||
import stcgal
 | 
			
		||||
from stcgal.utils import Utils, BaudType
 | 
			
		||||
from stcgal.utils import BaudType
 | 
			
		||||
from stcgal.protocols import *
 | 
			
		||||
from stcgal.ihex import IHex
 | 
			
		||||
 | 
			
		||||
@ -55,13 +55,16 @@ class StcGal:
 | 
			
		||||
        self.protocol.debug = opts.debug
 | 
			
		||||
 | 
			
		||||
    def emit_options(self, options):
 | 
			
		||||
        for o in options:
 | 
			
		||||
        """Set options from command line to protocol handler."""
 | 
			
		||||
 | 
			
		||||
        for opt in options:
 | 
			
		||||
            try:
 | 
			
		||||
                kv = o.split("=", 1)
 | 
			
		||||
                if len(kv) < 2: raise ValueError("incorrect format")
 | 
			
		||||
                kv = opt.split("=", 1)
 | 
			
		||||
                if len(kv) < 2:
 | 
			
		||||
                    raise ValueError("incorrect format")
 | 
			
		||||
                self.protocol.set_option(kv[0], kv[1])
 | 
			
		||||
            except ValueError as e:
 | 
			
		||||
                raise NameError("invalid option '%s' (%s)" % (kv[0], e))
 | 
			
		||||
            except ValueError as ex:
 | 
			
		||||
                raise NameError("invalid option '%s' (%s)" % (kv[0], ex))
 | 
			
		||||
 | 
			
		||||
    def load_file_auto(self, fileobj):
 | 
			
		||||
        """Load file with Intel Hex autodetection."""
 | 
			
		||||
@ -74,14 +77,16 @@ class StcGal:
 | 
			
		||||
                binary = hexfile.extract_data()
 | 
			
		||||
                print("%d bytes (Intel HEX)" %len(binary))
 | 
			
		||||
                return binary
 | 
			
		||||
            except ValueError as e:
 | 
			
		||||
                raise IOError("invalid Intel HEX file (%s)" %e)
 | 
			
		||||
            except ValueError as ex:
 | 
			
		||||
                raise IOError("invalid Intel HEX file (%s)" %ex)
 | 
			
		||||
        else:
 | 
			
		||||
            binary = fileobj.read()
 | 
			
		||||
            print("%d bytes (Binary)" %len(binary))
 | 
			
		||||
            return binary
 | 
			
		||||
 | 
			
		||||
    def program_mcu(self):
 | 
			
		||||
        """Execute the standard programming flow."""
 | 
			
		||||
 | 
			
		||||
        code_size = self.protocol.model.code
 | 
			
		||||
        ee_size = self.protocol.model.eeprom
 | 
			
		||||
 | 
			
		||||
@ -124,6 +129,8 @@ class StcGal:
 | 
			
		||||
        self.protocol.disconnect()
 | 
			
		||||
 | 
			
		||||
    def run(self):
 | 
			
		||||
        """Run programmer, main entry point."""
 | 
			
		||||
 | 
			
		||||
        try:
 | 
			
		||||
            self.protocol.connect(autoreset=self.opts.autoreset)
 | 
			
		||||
 | 
			
		||||
@ -140,21 +147,21 @@ class StcGal:
 | 
			
		||||
 | 
			
		||||
            self.protocol.initialize(base_protocol)
 | 
			
		||||
        except KeyboardInterrupt:
 | 
			
		||||
            sys.stdout.flush();
 | 
			
		||||
            sys.stdout.flush()
 | 
			
		||||
            print("interrupted")
 | 
			
		||||
            return 2
 | 
			
		||||
        except (StcFramingException, StcProtocolException) as e:
 | 
			
		||||
            sys.stdout.flush();
 | 
			
		||||
            print("Protocol error: %s" % e, file=sys.stderr)
 | 
			
		||||
        except (StcFramingException, StcProtocolException) as ex:
 | 
			
		||||
            sys.stdout.flush()
 | 
			
		||||
            print("Protocol error: %s" % ex, file=sys.stderr)
 | 
			
		||||
            self.protocol.disconnect()
 | 
			
		||||
            return 1
 | 
			
		||||
        except serial.SerialException as e:
 | 
			
		||||
            sys.stdout.flush();
 | 
			
		||||
            print("Serial port error: %s" % e, file=sys.stderr)
 | 
			
		||||
        except serial.SerialException as ex:
 | 
			
		||||
            sys.stdout.flush()
 | 
			
		||||
            print("Serial port error: %s" % ex, file=sys.stderr)
 | 
			
		||||
            return 1
 | 
			
		||||
        except IOError as e:
 | 
			
		||||
            sys.stdout.flush();
 | 
			
		||||
            print("I/O error: %s" % e, file=sys.stderr)
 | 
			
		||||
        except IOError as ex:
 | 
			
		||||
            sys.stdout.flush()
 | 
			
		||||
            print("I/O error: %s" % ex, file=sys.stderr)
 | 
			
		||||
            return 1
 | 
			
		||||
 | 
			
		||||
        try:
 | 
			
		||||
@ -164,27 +171,27 @@ class StcGal:
 | 
			
		||||
            else:
 | 
			
		||||
                self.protocol.disconnect()
 | 
			
		||||
                return 0
 | 
			
		||||
        except NameError as e:
 | 
			
		||||
            sys.stdout.flush();
 | 
			
		||||
            print("Option error: %s" % e, file=sys.stderr)
 | 
			
		||||
        except NameError as ex:
 | 
			
		||||
            sys.stdout.flush()
 | 
			
		||||
            print("Option error: %s" % ex, file=sys.stderr)
 | 
			
		||||
            self.protocol.disconnect()
 | 
			
		||||
            return 1
 | 
			
		||||
        except (StcFramingException, StcProtocolException) as e:
 | 
			
		||||
            sys.stdout.flush();
 | 
			
		||||
            print("Protocol error: %s" % e, file=sys.stderr)
 | 
			
		||||
        except (StcFramingException, StcProtocolException) as ex:
 | 
			
		||||
            sys.stdout.flush()
 | 
			
		||||
            print("Protocol error: %s" % ex, file=sys.stderr)
 | 
			
		||||
            self.protocol.disconnect()
 | 
			
		||||
            return 1
 | 
			
		||||
        except KeyboardInterrupt:
 | 
			
		||||
            sys.stdout.flush();
 | 
			
		||||
            sys.stdout.flush()
 | 
			
		||||
            print("interrupted", file=sys.stderr)
 | 
			
		||||
            self.protocol.disconnect()
 | 
			
		||||
            return 2
 | 
			
		||||
        except serial.SerialException as e:
 | 
			
		||||
            print("Serial port error: %s" % e, file=sys.stderr)
 | 
			
		||||
        except serial.SerialException as ex:
 | 
			
		||||
            print("Serial port error: %s" % ex, file=sys.stderr)
 | 
			
		||||
            return 1
 | 
			
		||||
        except IOError as e:
 | 
			
		||||
            sys.stdout.flush();
 | 
			
		||||
            print("I/O error: %s" % e, file=sys.stderr)
 | 
			
		||||
        except IOError as ex:
 | 
			
		||||
            sys.stdout.flush()
 | 
			
		||||
            print("I/O error: %s" % ex, file=sys.stderr)
 | 
			
		||||
            self.protocol.disconnect()
 | 
			
		||||
            return 1
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@ -422,7 +422,7 @@ class Stc89Protocol(StcBaseProtocol):
 | 
			
		||||
 | 
			
		||||
        bl_version, bl_stepping = struct.unpack("BB", packet[17:19])
 | 
			
		||||
        self.mcu_bsl_version = "%d.%d%s" % (bl_version >> 4, bl_version & 0x0f,
 | 
			
		||||
                                           chr(bl_stepping))
 | 
			
		||||
                                            chr(bl_stepping))
 | 
			
		||||
 | 
			
		||||
    def handshake(self):
 | 
			
		||||
        """Switch to transfer baudrate
 | 
			
		||||
@ -579,7 +579,7 @@ class Stc12AProtocol(Stc12AOptionsMixIn, Stc89Protocol):
 | 
			
		||||
 | 
			
		||||
        bl_version, bl_stepping = struct.unpack("BB", packet[17:19])
 | 
			
		||||
        self.mcu_bsl_version = "%d.%d%s" % (bl_version >> 4, bl_version & 0x0f,
 | 
			
		||||
                                           chr(bl_stepping))
 | 
			
		||||
                                            chr(bl_stepping))
 | 
			
		||||
 | 
			
		||||
        self.bsl_version = bl_version
 | 
			
		||||
 | 
			
		||||
@ -768,7 +768,7 @@ class Stc12BaseProtocol(StcBaseProtocol):
 | 
			
		||||
 | 
			
		||||
        bl_version, bl_stepping = struct.unpack("BB", packet[17:19])
 | 
			
		||||
        self.mcu_bsl_version = "%d.%d%s" % (bl_version >> 4, bl_version & 0x0f,
 | 
			
		||||
                                           chr(bl_stepping))
 | 
			
		||||
                                            chr(bl_stepping))
 | 
			
		||||
 | 
			
		||||
        self.bsl_version = bl_version
 | 
			
		||||
 | 
			
		||||
@ -1018,7 +1018,8 @@ class Stc15AProtocol(Stc12Protocol):
 | 
			
		||||
        """
 | 
			
		||||
 | 
			
		||||
        user_speed = self.trim_frequency
 | 
			
		||||
        if user_speed <= 0: user_speed = self.mcu_clock_hz
 | 
			
		||||
        if user_speed <= 0:
 | 
			
		||||
            user_speed = self.mcu_clock_hz
 | 
			
		||||
        program_speed = 22118400
 | 
			
		||||
 | 
			
		||||
        user_count = int(self.freq_counter * (user_speed / self.mcu_clock_hz))
 | 
			
		||||
@ -1405,7 +1406,7 @@ class Stc15Protocol(Stc15AProtocol):
 | 
			
		||||
        configuration."""
 | 
			
		||||
 | 
			
		||||
        msr = self.options.get_msr()
 | 
			
		||||
        packet  = bytes([0xff] * 23)
 | 
			
		||||
        packet = bytes([0xff] * 23)
 | 
			
		||||
        packet += bytes([(self.trim_frequency >> 24) & 0xff,
 | 
			
		||||
                         0xff,
 | 
			
		||||
                         (self.trim_frequency >> 16) & 0xff,
 | 
			
		||||
@ -1459,8 +1460,9 @@ class StcUsb15Protocol(Stc15Protocol):
 | 
			
		||||
 | 
			
		||||
    def dump_packet(self, data, request=0, value=0, index=0, receive=True):
 | 
			
		||||
        if self.debug:
 | 
			
		||||
            print("%s bRequest=%02X wValue=%04X wIndex=%04X data: %s" % (("<-" if receive else "->"),
 | 
			
		||||
                  request, value, index, Utils.hexstr(data, " ")), file=sys.stderr)
 | 
			
		||||
            print("%s bRequest=%02X wValue=%04X wIndex=%04X data: %s" %
 | 
			
		||||
                  (("<-" if receive else "->"), request, value, index,
 | 
			
		||||
                   Utils.hexstr(data, " ")), file=sys.stderr)
 | 
			
		||||
 | 
			
		||||
    def read_packet(self):
 | 
			
		||||
        """Read a packet from the MCU"""
 | 
			
		||||
@ -1506,9 +1508,10 @@ class StcUsb15Protocol(Stc15Protocol):
 | 
			
		||||
        """Connect to USB device and read info packet"""
 | 
			
		||||
 | 
			
		||||
        # USB support is optional. Provide an error if pyusb is not available.
 | 
			
		||||
        if _usb_available == False:
 | 
			
		||||
            raise StcProtocolException("USB support not available. "
 | 
			
		||||
                + "pyusb is not installed or not working correctly.")
 | 
			
		||||
        if not _usb_available:
 | 
			
		||||
            raise StcProtocolException(
 | 
			
		||||
                "USB support not available. " +
 | 
			
		||||
                "pyusb is not installed or not working correctly.")
 | 
			
		||||
 | 
			
		||||
        print("Waiting for MCU, please cycle power: ", end="")
 | 
			
		||||
        sys.stdout.flush()
 | 
			
		||||
 | 
			
		||||
@ -19,28 +19,36 @@
 | 
			
		||||
# SOFTWARE.
 | 
			
		||||
#
 | 
			
		||||
 | 
			
		||||
import serial
 | 
			
		||||
import argparse
 | 
			
		||||
import serial
 | 
			
		||||
 | 
			
		||||
class Utils:
 | 
			
		||||
    """Common utility functions"""
 | 
			
		||||
 | 
			
		||||
    @classmethod
 | 
			
		||||
    def to_bool(self, val):
 | 
			
		||||
    def to_bool(cls, val):
 | 
			
		||||
        """make sensible boolean from string or other type value"""
 | 
			
		||||
 | 
			
		||||
        if isinstance(val, bool): return val
 | 
			
		||||
        if isinstance(val, int): return bool(val)
 | 
			
		||||
        if len(val) == 0: return False
 | 
			
		||||
        return True if val[0].lower() == "t" or val[0] == "1" else False
 | 
			
		||||
        if isinstance(val, bool):
 | 
			
		||||
            return val
 | 
			
		||||
        elif isinstance(val, int):
 | 
			
		||||
            return bool(val)
 | 
			
		||||
        elif len(val) == 0:
 | 
			
		||||
            return False
 | 
			
		||||
        else:
 | 
			
		||||
            return True if val[0].lower() == "t" or val[0] == "1" else False
 | 
			
		||||
 | 
			
		||||
    @classmethod
 | 
			
		||||
    def to_int(self, val):
 | 
			
		||||
    def to_int(cls, val):
 | 
			
		||||
        """make int from any value, nice error message if not possible"""
 | 
			
		||||
 | 
			
		||||
        try: return int(val, 0)
 | 
			
		||||
        except: raise ValueError("invalid integer")
 | 
			
		||||
        try:
 | 
			
		||||
            return int(val, 0)
 | 
			
		||||
        except:
 | 
			
		||||
            raise ValueError("invalid integer")
 | 
			
		||||
 | 
			
		||||
    @classmethod
 | 
			
		||||
    def hexstr(self, bytestr, sep=""):
 | 
			
		||||
    def hexstr(cls, bytestr, sep=""):
 | 
			
		||||
        """make formatted hex string output from byte sequence"""
 | 
			
		||||
 | 
			
		||||
        return sep.join(["%02X" % x for x in bytestr])
 | 
			
		||||
@ -55,5 +63,5 @@ class BaudType:
 | 
			
		||||
            raise argparse.ArgumentTypeError("illegal baudrate")
 | 
			
		||||
        return baud
 | 
			
		||||
 | 
			
		||||
    def __repr__(self): return "baudrate"
 | 
			
		||||
 | 
			
		||||
    def __repr__(self):
 | 
			
		||||
        return "baudrate"
 | 
			
		||||
 | 
			
		||||
		Reference in New Issue
	
	Block a user