code format + copyright for logging.py

This commit is contained in:
foamyguy 2024-04-02 20:02:38 -05:00
parent 3a93a1e610
commit b11f97bfc7
7 changed files with 75 additions and 43 deletions

View file

@ -11,11 +11,9 @@ from circup.backends import WebBackend, DiskBackend
from circup.logging import logger
# Useful constants.
__version__ = "0.0.0-auto.0"
__repo__ = "https://github.com/adafruit/circup.git"

View file

@ -10,8 +10,13 @@ import sys
import click
import requests
from circup.shared import DATA_DIR, PLATFORMS, REQUESTS_TIMEOUT, tags_data_load, \
get_latest_release_from_url
from circup.shared import (
DATA_DIR,
PLATFORMS,
REQUESTS_TIMEOUT,
tags_data_load,
get_latest_release_from_url,
)
from circup.logging import logger
@ -106,7 +111,9 @@ class Bundle:
:return: The most recent tag value for the project.
"""
if self._latest is None:
self._latest = get_latest_release_from_url(self.url + "/releases/latest", logger)
self._latest = get_latest_release_from_url(
self.url + "/releases/latest", logger
)
return self._latest
def validate(self):
@ -146,4 +153,4 @@ class Bundle:
"current": self._current,
"latest": self._latest,
}
)
)

View file

@ -17,8 +17,17 @@ import toml
import re
import findimports
from circup.shared import PLATFORMS, REQUESTS_TIMEOUT, _get_modules_file, BUNDLE_CONFIG_OVERWRITE, BUNDLE_CONFIG_FILE, \
BUNDLE_CONFIG_LOCAL, BUNDLE_DATA, NOT_MCU_LIBRARIES, tags_data_load
from circup.shared import (
PLATFORMS,
REQUESTS_TIMEOUT,
_get_modules_file,
BUNDLE_CONFIG_OVERWRITE,
BUNDLE_CONFIG_FILE,
BUNDLE_CONFIG_LOCAL,
BUNDLE_DATA,
NOT_MCU_LIBRARIES,
tags_data_load,
)
from circup.logging import logger
from circup.module import Module
from circup.bundle import Bundle
@ -258,7 +267,7 @@ def get_bundle(bundle, tag):
total_size = int(r.headers.get("Content-Length"))
temp_zip = bundle.zip.format(platform=platform)
with click.progressbar(
r.iter_content(1024), label="Extracting:", length=total_size
r.iter_content(1024), label="Extracting:", length=total_size
) as pbar, open(temp_zip, "wb") as zip_fp:
for chunk in pbar:
zip_fp.write(chunk)

View file

@ -7,7 +7,7 @@
The following functions have IO side effects (for instance they emit to
stdout). Ergo, these are not checked with unit tests. Most of the
functionality they provide is provided by the functions from util_functions.py,
and the respective Backends which *are* tested. Most of the logic of the following
and the respective Backends which *are* tested. Most of the logic of the following
functions is to prepare things for presentation to / interaction with the user.
"""
import os
@ -25,11 +25,20 @@ from circup.backends import WebBackend, DiskBackend
from circup.logging import logger, log_formatter, LOGFILE
from circup.shared import BOARDLESS_COMMANDS, get_latest_release_from_url
from circup.bundle import Bundle
from circup.command_utils import get_device_path, get_circup_version, find_modules, \
get_bundles_list, completion_for_install, get_bundle_versions, libraries_from_requirements, \
libraries_from_code_py, get_dependencies, get_bundles_local_dict, save_local_bundles, get_bundles_dict
from circup.command_utils import (
get_device_path,
get_circup_version,
find_modules,
get_bundles_list,
completion_for_install,
get_bundle_versions,
libraries_from_requirements,
libraries_from_code_py,
get_dependencies,
get_bundles_local_dict,
save_local_bundles,
get_bundles_dict,
)
@click.group()
@ -57,13 +66,13 @@ from circup.command_utils import get_device_path, get_circup_version, find_modul
"--board-id",
default=None,
help="Manual Board ID of the CircuitPython device. If provided in combination "
"with --cpy-version, it overrides the detected board ID.",
"with --cpy-version, it overrides the detected board ID.",
)
@click.option(
"--cpy-version",
default=None,
help="Manual CircuitPython version. If provided in combination "
"with --board-id, it overrides the detected CPy version.",
"with --board-id, it overrides the detected CPy version.",
)
@click.version_option(
prog_name="CircUp",
@ -71,7 +80,7 @@ from circup.command_utils import get_device_path, get_circup_version, find_modul
)
@click.pass_context
def main( # pylint: disable=too-many-locals
ctx, verbose, path, host, password, timeout, board_id, cpy_version
ctx, verbose, path, host, password, timeout, board_id, cpy_version
): # pragma: no cover
"""
A tool to manage and update libraries on a CircuitPython device.
@ -133,8 +142,7 @@ def main( # pylint: disable=too-many-locals
ctx.obj["DEVICE_PATH"] = device_path
latest_version = get_latest_release_from_url(
"https://github.com/adafruit/circuitpython/releases/latest",
logger
"https://github.com/adafruit/circuitpython/releases/latest", logger
)
global CPY_VERSION
if device_path is None or not ctx.obj["backend"].is_device_present():
@ -191,7 +199,7 @@ def freeze(ctx, requirement): # pragma: no cover
for i, module in enumerate(output):
output[i] += "\n"
with open(
cwd + "/" + "requirements.txt", "w", newline="\n", encoding="utf-8"
cwd + "/" + "requirements.txt", "w", newline="\n", encoding="utf-8"
) as file:
file.truncate(0)
file.writelines(output)
@ -255,7 +263,7 @@ def list_cli(ctx): # pragma: no cover
"--requirement",
type=click.Path(exists=True, dir_okay=False),
help="specify a text file to install all modules listed in the text file."
" Typically requirements.txt.",
" Typically requirements.txt.",
)
@click.option(
"--auto", "-a", is_flag=True, help="Install the modules imported in code.py."
@ -264,7 +272,7 @@ def list_cli(ctx): # pragma: no cover
"--auto-file",
default=None,
help="Specify the name of a file on the board to read for auto install."
" Also accepts an absolute path or a local ./ path.",
" Also accepts an absolute path or a local ./ path.",
)
@click.pass_context
def install(ctx, modules, pyext, requirement, auto, auto_file): # pragma: no cover
@ -379,8 +387,8 @@ def uninstall(ctx, module): # pragma: no cover
@main.command(
short_help=(
"Update modules on the device. "
"Use --all to automatically update all modules without Major Version warnings."
"Update modules on the device. "
"Use --all to automatically update all modules without Major Version warnings."
)
)
@click.option(
@ -420,7 +428,7 @@ def update(ctx, update_all): # pragma: no cover
)
)
if isinstance(module.bundle_version, str) and not VersionInfo.is_valid(
module.bundle_version
module.bundle_version
):
click.secho(
f"WARNING: Library {module.name} repo has incorrect __version__"

View file

@ -1,3 +1,9 @@
# SPDX-FileCopyrightText: 2019 Nicholas Tollervey, 2024 Tim Cocks, written for Adafruit Industries
#
# SPDX-License-Identifier: MIT
"""
Logging utilities and configuration used by circup
"""
import os
import logging
from logging.handlers import RotatingFileHandler
@ -24,4 +30,4 @@ log_formatter = logging.Formatter(
"%(asctime)s %(levelname)s: %(message)s", datefmt="%m/%d/%Y %H:%M:%S"
)
logfile_handler.setFormatter(log_formatter)
logger.addHandler(logfile_handler)
logger.addHandler(logfile_handler)

View file

@ -12,6 +12,7 @@ from circup.shared import BAD_FILE_FORMAT
from circup.backends import WebBackend
from circup.logging import logger
class Module:
"""
Represents a CircuitPython module.
@ -20,15 +21,15 @@ class Module:
# pylint: disable=too-many-arguments
def __init__(
self,
name,
backend,
repo,
device_version,
bundle_version,
mpy,
bundle,
compatibility,
self,
name,
backend,
repo,
device_version,
bundle_version,
mpy,
bundle,
compatibility,
):
"""
The ``self.file`` and ``self.name`` attributes are constructed from
@ -56,9 +57,9 @@ class Module:
url = urlparse(self.path, allow_fragments=False)
if (
url.path.endswith("/")
if isinstance(backend, WebBackend)
else self.path.endswith(os.sep)
url.path.endswith("/")
if isinstance(backend, WebBackend)
else self.path.endswith(os.sep)
):
self.file = None
self.name = self.path.split(
@ -80,7 +81,7 @@ class Module:
self.bundle_path = None
if self.mpy:
# Byte compiled, now check CircuitPython version.
major_version = self.backend.get_circuitpython_version()[0].split(".")[0]
bundle_platform = "{}mpy".format(major_version)
else:
@ -158,8 +159,8 @@ class Module:
"""
try:
if (
VersionInfo.parse(self.device_version).major
== VersionInfo.parse(self.bundle_version).major
VersionInfo.parse(self.device_version).major
== VersionInfo.parse(self.bundle_version).major
):
return False
except (TypeError, ValueError) as ex:

View file

@ -51,6 +51,7 @@ NOT_MCU_LIBRARIES = [
#: Commands that do not require an attached board
BOARDLESS_COMMANDS = ["show", "bundle-add", "bundle-remove", "bundle-show"]
def _get_modules_file(path, logger):
"""
Get a dictionary containing metadata about all the Python modules found in
@ -175,6 +176,7 @@ def extract_metadata(path, logger):
result["__version__"] = BAD_FILE_FORMAT
return result
def tags_data_load(logger):
"""
Load the list of the version tags of the bundles on disk.
@ -197,6 +199,7 @@ def tags_data_load(logger):
tags_data = {}
return tags_data
def get_latest_release_from_url(url, logger):
"""
Find the tag name of the latest release by using HTTP HEAD and decoding the redirect.
@ -212,4 +215,4 @@ def get_latest_release_from_url(url, logger):
responseurl = response.headers["Location"]
tag = responseurl.rsplit("/", 1)[-1]
logger.info("Tag: '%s'", tag)
return tag
return tag