Skip to content

Commit

Permalink
lint with pylint
Browse files Browse the repository at this point in the history
  • Loading branch information
hmaarrfk committed Apr 5, 2024
1 parent d1f3da7 commit 09b2cab
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 17 deletions.
11 changes: 6 additions & 5 deletions teensytoany/programmer.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,17 @@ def teensytoany_programmer(
firmware_version=None,
download_only=False
):
"""Program a Teensy device with a given firmware version"""
if download_only:
for mcu in ['TEENSY40', 'TEENSY32']:
for mcu_to_download in ['TEENSY40', 'TEENSY32']:
if firmware_version is None:
firmware_version = teensytoany.TeensyToAny._get_latest_available_firmware(
mcu=mcu, online=True, local=False)
firmware_version = teensytoany.TeensyToAny.get_latest_available_firmware_version(
mcu=mcu_to_download, online=True, local=False
)
print(f"Downloading firmware version {firmware_version} for {mcu}.")
teensytoany.TeensyToAny._download_firmware(mcu=mcu, version=firmware_version)
teensytoany.TeensyToAny.download_firmware(mcu=mcu, version=firmware_version)
return

"""Program a Teensy device with a given firmware version"""
print(f'Programming irmware version {mcu} {firmware_version}', end='')
teensytoany.TeensyToAny.program_firmware(serial_number, mcu=mcu, version=firmware_version)
teensy = teensytoany.TeensyToAny(serial_number)
Expand Down
25 changes: 13 additions & 12 deletions teensytoany/teensytoany.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,9 @@ def list_all_serial_numbers(serial_numbers=None, *, device_name=None):
return serial_numbers

@staticmethod
def _get_latest_available_firmware(*, mcu='TEENSY40', online=True, local=True, timeout=2):
def get_latest_available_firmware_version(
*, mcu='TEENSY40', online=True, local=True, timeout=2
):
if local:
local_versions = TeensyToAny._find_local_versions(mcu=mcu)
if len(local_versions) > 0:
Expand All @@ -142,7 +144,7 @@ def _get_latest_available_firmware(*, mcu='TEENSY40', online=True, local=True, t
if online:
latest = TeensyToAny._get_latest_available_firmware_online(
timeout=timeout)
except Exception as e:
except Exception: # pylint: disable=broad-except
pass

if latest is None:
Expand Down Expand Up @@ -204,7 +206,7 @@ def _update_firmware(self, *, mcu=None, force=False, timeout=2):
"The current microcontroller is unknown, please specify it "
"before attempting to update the firmware.")

latest_version = self._get_latest_available_firmware(mcu=mcu, timeout=timeout)
latest_version = self.get_latest_available_firmware_version(mcu=mcu, timeout=timeout)
if not force:
if Version(current_version) >= Version(latest_version):
return
Expand Down Expand Up @@ -234,7 +236,7 @@ def _find_local_versions(*, mcu=None):
if d.is_dir() and (d / 'firmware.hex').is_file()
]

versions.sort(key=lambda x: Version(x))
versions.sort(key=Version)
return versions

@staticmethod
Expand All @@ -245,8 +247,8 @@ def _generate_firmware_filename(*, mcu, version):

@staticmethod
def _generate_firmware_directory(*, mcu):
from appdirs import AppDirs
from pathlib import Path
from appdirs import AppDirs # pylint: disable=import-outside-toplevel
from pathlib import Path # pylint: disable=import-outside-toplevel
app = AppDirs('teensytoany', 'ramonaoptics')
cache_dir = Path(app.user_cache_dir)
cache_dir.mkdir(parents=True, exist_ok=True)
Expand All @@ -255,7 +257,7 @@ def _generate_firmware_directory(*, mcu):
return firmware_dir

@staticmethod
def _download_firmware(*, mcu, version, timeout=2):
def download_firmware(*, mcu, version, timeout=2):
firmware_filename = TeensyToAny._generate_firmware_filename(mcu=mcu, version=version)

import requests # pylint: disable=import-outside-toplevel
Expand All @@ -271,10 +273,10 @@ def _download_firmware(*, mcu, version, timeout=2):
for chunk in response.iter_content(chunk_size=4096):
file.write(chunk)

return firmware_filename

@staticmethod
def program_firmware(serial_number=None, *, mcu=None, version=None, timeout=2):
from pathlib import Path # pylint: disable=import-outside-toplevel

if serial_number is None:
available_serial_numbers = TeensyToAny.list_all_serial_numbers()
if len(available_serial_numbers) == 0:
Expand All @@ -290,8 +292,7 @@ def program_firmware(serial_number=None, *, mcu=None, version=None, timeout=2):
raise RuntimeError("mcu must be provided and cannot be left as None.")

if version is None:
version = TeensyToAny._get_latest_available_firmware(
timeout=timeout)
version = TeensyToAny.get_latest_available_firmware_version(timeout=timeout)

if os.name == 'nt':
# We do supporting updating, but it is "scary" to do so since
Expand All @@ -301,7 +302,7 @@ def program_firmware(serial_number=None, *, mcu=None, version=None, timeout=2):
firmware_filename = TeensyToAny._generate_firmware_filename(mcu=mcu, version=version)

if not firmware_filename.is_file():
TeensyToAny._download_firmware(mcu=mcu, version=version, timeout=timeout)
TeensyToAny.download_firmware(mcu=mcu, version=version, timeout=timeout)

cmd_list = [
'teensy_loader_cli',
Expand Down

0 comments on commit 09b2cab

Please sign in to comment.