Skip to content

Commit

Permalink
is_deice_present() for WebBackend. pylint fixes. equalize behavior wh…
Browse files Browse the repository at this point in the history
…en device is not present.
  • Loading branch information
FoamyGuy committed Dec 14, 2023
1 parent a96fd8e commit 31270a0
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 18 deletions.
10 changes: 6 additions & 4 deletions circup/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -954,7 +954,7 @@ def libraries_from_code_py(code_py, mod_names):
try:
found_imports = findimports.find_imports(code_py)
except Exception as ex: # broad exception because anything could go wrong
self.logger.exception(ex)
logger.exception(ex)
click.secho('Unable to read the auto file: "{}"'.format(str(ex)), fg="red")
sys.exit(2)
# pylint: enable=broad-except
Expand Down Expand Up @@ -1017,9 +1017,11 @@ def main(ctx, verbose, path, host, password, board_id, cpy_version): # pragma:
if using_webworkflow:
ctx.obj["backend"] = WebBackend(host=host, password=password, logger=logger)
else:
ctx.obj["backend"] = DiskBackend(device_path, logger)
try:
ctx.obj["backend"] = DiskBackend(device_path, logger)
except ValueError as e:
print(e)

print(f'device is present ? {ctx.obj["backend"].is_device_present()}')
if verbose:
# Configure additional logging to stdout.
global VERBOSE
Expand All @@ -1046,7 +1048,7 @@ def main(ctx, verbose, path, host, password, board_id, cpy_version): # pragma:
"https://github.com/adafruit/circuitpython/releases/latest"
)
global CPY_VERSION
if device_path is None:
if device_path is None or not ctx.obj["backend"].is_device_present():
click.secho("Could not find a connected CircuitPython device.", fg="red")
sys.exit(1)
else:
Expand Down
42 changes: 28 additions & 14 deletions circup/backends.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@
import sys
import tempfile
from urllib.parse import urlparse

import click
import findimports
import requests
from requests.auth import HTTPBasicAuth

Expand Down Expand Up @@ -133,11 +131,11 @@ def install_module(
else:
click.echo("Unknown module named, '{}'.".format(name))

def libraries_from_imports(self, code_py, mod_names):
"""
To be overridden by subclass
"""
raise NotImplementedError
# def libraries_from_imports(self, code_py, mod_names):
# """
# To be overridden by subclass
# """
# raise NotImplementedError

def uninstall(self, device_path, module_path):
"""
Expand Down Expand Up @@ -399,14 +397,16 @@ def _install_module_py(self, metadata):

source_path = metadata["path"] # Path to Python source version.
if os.path.isdir(source_path):
target = os.path.basename(os.path.dirname(source_path))
self.install_dir_http(source_path)

else:
target = os.path.basename(source_path)
self.install_file_http(source_path)

def get_auto_file_path(self, auto_file_path):
"""
Make a local temp copy of the --auto file from the device.
Returns the path to the local copy.
"""
url = auto_file_path
auth = HTTPBasicAuth("", self.password)
r = requests.get(url, auth=auth)
Expand Down Expand Up @@ -454,6 +454,16 @@ def get_file_path(self, filename):
"""
return os.path.join(self.device_location, "fs", filename)

def is_device_present(self):
"""
returns True if the device is currently connected
"""
try:
_ = self.get_device_versions()
return True
except requests.exceptions.ConnectionError:
return False


class DiskBackend(Backend):
"""
Expand All @@ -463,7 +473,8 @@ class DiskBackend(Backend):
def __init__(self, device_location, logger, version_info=None):
if device_location is None:
raise ValueError(
"Auto locating USB Disk based device failed. Please specify --path argument or ensure your device "
"Auto locating USB Disk based device failed. "
"Please specify --path argument or ensure your device "
"is connected and mounted under the name CIRCUITPY."
)
super().__init__(logger)
Expand Down Expand Up @@ -510,8 +521,8 @@ def get_circuitpython_version(self):
sys.exit(1)

return circuit_python, board_id
else:
return self.version_info

return self.version_info

def _get_modules(self, device_lib_path):
"""
Expand Down Expand Up @@ -573,6 +584,9 @@ def _install_module_py(self, metadata):
shutil.copyfile(source_path, target_path)

def get_auto_file_path(self, auto_file_path):
"""
Returns the path on the device to the file to be read for --auto.
"""
return auto_file_path

def uninstall(self, device_path, module_path):
Expand Down Expand Up @@ -615,12 +629,12 @@ def _update_file(self, module):

def get_file_path(self, filename):
"""
retuns the full path on the device to a given file name.
returns the full path on the device to a given file name.
"""
return os.path.join(self.device_location, filename)

def is_device_present(self):
"""
To be overriden by subclass
returns True if the device is currently connected
"""
return os.path.exists(self.device_location)
1 change: 1 addition & 0 deletions circup/shared.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ def extract_metadata(path, logger):
:param str path: The path to the file containing the metadata.
:return: The dunder based metadata found in the file, as a dictionary.
"""
# pylint: disable=too-many-locals
result = {}
logger.info("%s", path)
if path.endswith(".py"):
Expand Down

0 comments on commit 31270a0

Please sign in to comment.