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