pylint fixes

This commit is contained in:
Otto winter 2021-10-29 12:35:54 +02:00
parent c795fa0d00
commit 246d001ac7
No known key found for this signature in database
GPG Key ID: 48ED2DDB96D7682C
4 changed files with 34 additions and 26 deletions

View File

@ -90,8 +90,8 @@ def show_logs(serial_port):
return
text = raw.decode(errors="ignore")
line = text.replace("\r", "").replace("\n", "")
time = datetime.now().time().strftime("[%H:%M:%S]")
message = time + line
time_ = datetime.now().time().strftime("[%H:%M:%S]")
message = time_ + line
try:
print(message)
except UnicodeEncodeError:
@ -108,9 +108,10 @@ def run_esphomeflasher(argv):
return
try:
# pylint: disable=consider-using-with
firmware = open(args.binary, "rb")
except IOError as err:
raise EsphomeflasherError(f"Error opening binary: {err}")
raise EsphomeflasherError(f"Error opening binary: {err}") from err
chip = detect_chip(port, args.esp8266, args.esp32)
info = read_chip_info(chip)
@ -122,9 +123,7 @@ def run_esphomeflasher(argv):
print(f" - Number of Cores: {info.num_cores}")
print(f" - Max CPU Frequency: {info.cpu_frequency}")
print(f" - Has Bluetooth: {'YES' if info.has_bluetooth else 'NO'}")
print(
f" - Has Embedded Flash: {'YES' if info.has_embedded_flash else 'NO'}"
)
print(f" - Has Embedded Flash: {'YES' if info.has_embedded_flash else 'NO'}")
print(
f" - Has Factory-Calibrated ADC: {'YES' if info.has_factory_calibrated_adc else 'NO'}"
)
@ -142,7 +141,7 @@ def run_esphomeflasher(argv):
except esptool.FatalError as err:
raise EsphomeflasherError(
f"Error changing ESP upload baud rate: {err}"
)
) from err
# Check if the higher baud rate works
try:
@ -152,6 +151,7 @@ def run_esphomeflasher(argv):
print(
f"Chip does not support baud rate {args.upload_baud_rate}, changing to 115200"
)
# pylint: disable=protected-access
stub_chip._port.close()
chip = detect_chip(port, args.esp8266, args.esp32)
stub_chip = chip_run_stub(chip)
@ -171,18 +171,18 @@ def run_esphomeflasher(argv):
try:
stub_chip.flash_set_parameters(esptool.flash_size_bytes(flash_size))
except esptool.FatalError as err:
raise EsphomeflasherError(f"Error setting flash parameters: {err}")
raise EsphomeflasherError(f"Error setting flash parameters: {err}") from err
if not args.no_erase:
try:
esptool.erase_flash(stub_chip, mock_args)
except esptool.FatalError as err:
raise EsphomeflasherError(f"Error while erasing flash: {err}")
raise EsphomeflasherError(f"Error while erasing flash: {err}") from err
try:
esptool.write_flash(stub_chip, mock_args)
except esptool.FatalError as err:
raise EsphomeflasherError(f"Error while writing flash: {err}")
raise EsphomeflasherError(f"Error while writing flash: {err}") from err
print("Hard Resetting...")
stub_chip.hard_reset()
@ -191,10 +191,13 @@ def run_esphomeflasher(argv):
print()
if args.upload_baud_rate != 115200:
# pylint: disable=protected-access
stub_chip._port.baudrate = 115200
time.sleep(0.05) # get rid of crap sent during baud rate change
# pylint: disable=protected-access
stub_chip._port.flushInput()
# pylint: disable=protected-access
show_logs(stub_chip._port)

View File

@ -11,7 +11,7 @@ class EsphomeflasherError(Exception):
pass
class MockEsptoolArgs(object):
class MockEsptoolArgs:
def __init__(self, flash_size, addr_filename, flash_mode, flash_freq):
self.compress = True
self.no_compress = False
@ -26,7 +26,7 @@ class MockEsptoolArgs(object):
self.encrypt_files = None
class ChipInfo(object):
class ChipInfo:
def __init__(self, family, model, mac):
self.family = family
self.model = model
@ -53,7 +53,7 @@ class ESP32ChipInfo(ChipInfo):
has_embedded_flash,
has_factory_calibrated_adc,
):
super(ESP32ChipInfo, self).__init__("ESP32", model, mac)
super().__init__("ESP32", model, mac)
self.num_cores = num_cores
self.cpu_frequency = cpu_frequency
self.has_bluetooth = has_bluetooth
@ -76,7 +76,7 @@ class ESP32ChipInfo(ChipInfo):
class ESP8266ChipInfo(ChipInfo):
def __init__(self, model, mac, chip_id):
super(ESP8266ChipInfo, self).__init__("ESP8266", model, mac)
super().__init__("ESP8266", model, mac)
self.chip_id = chip_id
def as_dict(self):
@ -93,7 +93,7 @@ def read_chip_property(func, *args, **kwargs):
try:
return prevent_print(func, *args, **kwargs)
except esptool.FatalError as err:
raise EsphomeflasherError(f"Reading chip details failed: {err}")
raise EsphomeflasherError(f"Reading chip details failed: {err}") from err
def read_chip_info(chip):
@ -115,7 +115,7 @@ def read_chip_info(chip):
has_embedded_flash,
has_factory_calibrated_adc,
)
elif isinstance(chip, esptool.ESP8266ROM):
if isinstance(chip, esptool.ESP8266ROM):
model = read_chip_property(chip.get_chip_description)
chip_id = read_chip_property(chip.chip_id)
return ESP8266ChipInfo(model, mac, chip_id)
@ -128,7 +128,7 @@ def chip_run_stub(chip):
except esptool.FatalError as err:
raise EsphomeflasherError(
f"Error putting ESP in stub flash mode: {err}"
)
) from err
def detect_flash_size(stub_chip):
@ -165,11 +165,11 @@ def open_downloadable_binary(path):
except requests.exceptions.Timeout as err:
raise EsphomeflasherError(
f"Timeout while retrieving firmware file '{path}': {err}"
)
) from err
except requests.exceptions.RequestException as err:
raise EsphomeflasherError(
f"Error while retrieving firmware file '{path}': {err}"
)
) from err
binary = io.BytesIO()
binary.write(response.content)
@ -179,7 +179,7 @@ def open_downloadable_binary(path):
try:
return open(path, "rb")
except IOError as err:
raise EsphomeflasherError(f"Error opening binary '{path}': {err}")
raise EsphomeflasherError(f"Error opening binary '{path}': {err}") from err
def format_bootloader_path(path, flash_mode, flash_freq):
@ -229,6 +229,6 @@ def detect_chip(port, force_esp8266=False, force_esp32=False):
try:
chip.connect()
except esptool.FatalError as err:
raise EsphomeflasherError(f"Error connecting to ESP: {err}")
raise EsphomeflasherError(f"Error connecting to ESP: {err}") from err
return chip

View File

@ -12,6 +12,8 @@ from wx.lib.embeddedimage import PyEmbeddedImage
from esphomeflasher.helpers import list_serial_ports
# pylint: disable=no-member
COLOR_RE = re.compile(r"(?:\033)(?:\[(.*?)[@-~]|\].*?(?:\007|\033\\))")
COLORS = {
"black": wx.BLACK,
@ -30,6 +32,7 @@ BACK_COLORS = {**COLORS, None: wx.BLACK}
# See discussion at http://stackoverflow.com/q/41101897/131929
class RedirectText(TextIOBase):
def __init__(self, text_ctrl):
super().__init__()
self._out = text_ctrl
self._i = 0
self._line = ""
@ -125,6 +128,7 @@ class RedirectText(TextIOBase):
self._add_content(self._line[pos:])
def write(self, string):
# pylint: disable=invalid-name
for s in string:
if s == "\r":
current_value = self._out.GetValue()
@ -164,8 +168,8 @@ class FlashingThread(threading.Thread):
if self._show_logs:
argv.append("--show-logs")
run_esphomeflasher(argv)
except Exception as e:
print(f"Unexpected error: {e}")
except Exception as err:
print(f"Unexpected error: {err}")
raise
@ -290,7 +294,7 @@ class MainFrame(wx.Frame):
def _get_serial_ports(self):
ports = []
for port, desc in list_serial_ports():
for port, _ in list_serial_ports():
ports.append(port)
if not self._port and ports:
self._port = ports[0]
@ -307,6 +311,7 @@ class MainFrame(wx.Frame):
class App(wx.App, wx.lib.mixins.inspection.InspectionMixin):
# pylint: disable=invalid-name
def OnInit(self):
wx.SystemOptions.SetOption("mac.window-plain-transition", 1)
self.SetAppName("esphome-flasher (Based on NodeMCU PyFlasher)")

View File

@ -5,6 +5,7 @@ import sys
import serial
# pylint: disable=unspecified-encoding,consider-using-with
DEVNULL = open(os.devnull, "w")
@ -32,7 +33,6 @@ def prevent_print(func, *args, **kwargs):
except serial.SerialException as err:
from esphomeflasher.common import EsphomeflasherError
raise EsphomeflasherError(f"Serial port closed: {err}")
raise EsphomeflasherError(f"Serial port closed: {err}") from err
finally:
sys.stdout = orig_sys_stdout
pass