From b35d02812c7db9bc3b6f8931d0712a70ef80bbb2 Mon Sep 17 00:00:00 2001 From: Jos Verlinde Date: Tue, 2 Jul 2024 21:09:31 +0200 Subject: [PATCH] stubber: use list_mcu logic from mpflash Signed-off-by: Jos Verlinde --- .vscode/settings.json | 2 +- src/stubber/bulk/mcu_stubber.py | 84 ++++++++++---------- src/stubber/codemod/enrich.py | 2 +- src/stubber/codemod/merge_docstub.py | 2 +- src/stubber/commands/build_cmd.py | 2 +- src/stubber/commands/cli.py | 16 ++-- src/stubber/commands/clone_cmd.py | 2 +- src/stubber/commands/config_cmd.py | 2 +- src/stubber/commands/enrich_folder_cmd.py | 2 +- src/stubber/commands/get_core_cmd.py | 2 +- src/stubber/commands/get_docstubs_cmd.py | 7 +- src/stubber/commands/get_frozen_cmd.py | 7 +- src/stubber/commands/get_mcu_cmd.py | 63 ++++++++++++--- src/stubber/commands/merge_cmd.py | 2 +- src/stubber/commands/publish_cmd.py | 2 +- src/stubber/commands/stub_cmd.py | 2 +- src/stubber/commands/variants_cmd.py | 2 +- src/stubber/downloader.py | 3 +- src/stubber/freeze/common.py | 10 ++- src/stubber/freeze/freeze_folder.py | 4 +- src/stubber/freeze/freeze_manifest_2.py | 25 ++++-- src/stubber/freeze/get_frozen.py | 10 ++- src/stubber/get_cpython.py | 19 ++++- src/stubber/minify.py | 10 ++- src/stubber/publish/candidates.py | 33 ++++++-- src/stubber/publish/merge_docstubs.py | 6 +- src/stubber/publish/missing_class_methods.py | 6 +- src/stubber/publish/package.py | 10 ++- src/stubber/publish/pathnames.py | 2 +- src/stubber/publish/publish.py | 2 +- src/stubber/publish/pypi.py | 8 +- src/stubber/publish/stubpackage.py | 47 ++++++++--- src/stubber/rst/classsort.py | 3 +- src/stubber/rst/reader.py | 48 ++++++++--- src/stubber/rst/report_return.py | 16 +++- src/stubber/rst/rst_utils.py | 3 +- src/stubber/stubs_from_docs.py | 2 +- src/stubber/update_fallback.py | 2 +- src/stubber/update_module_list.py | 2 +- src/stubber/utils/config.py | 26 ++++-- src/stubber/utils/post.py | 3 +- src/stubber/utils/repos.py | 12 ++- src/stubber/utils/stubmaker.py | 2 +- src/stubber/utils/typed_config_toml.py | 7 +- src/stubber/variants.py | 2 +- 45 files changed, 355 insertions(+), 169 deletions(-) diff --git a/.vscode/settings.json b/.vscode/settings.json index add51d503..2a009ed68 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,6 +1,6 @@ { "python.analysis.autoSearchPaths": true, - "python.analysis.diagnosticMode": "workspace", + "python.analysis.diagnosticMode": "openFilesOnly", // "python.analysis.logLevel": "Trace", ////////////////////////////////////////////////////////////// "python.testing.pytestEnabled": true, diff --git a/src/stubber/bulk/mcu_stubber.py b/src/stubber/bulk/mcu_stubber.py index 0ccac34c1..23d2bbcbc 100644 --- a/src/stubber/bulk/mcu_stubber.py +++ b/src/stubber/bulk/mcu_stubber.py @@ -11,12 +11,15 @@ from tempfile import mkdtemp from typing import List, Optional, Tuple -from loguru import logger as log + from rich.console import Console from rich.table import Table from tenacity import retry, stop_after_attempt, wait_fixed from mpflash.mpremoteboard import ERROR, OK, MPRemoteBoard +from mpflash.connected import list_mcus +from mpflash.list import show_mcus +from mpflash.logger import log from stubber import utils from stubber.publish.merge_docstubs import merge_all_docstubs from stubber.publish.pathnames import board_folder_name @@ -145,7 +148,9 @@ def run_createstubs(dest: Path, mcu: MPRemoteBoard, variant: Variant = Variant.d mcu.run_command("reset", timeout=5) time.sleep(2) - log.info(f"Running createstubs {variant.value} on {mcu.serialport} {mcu.description} using temp path: {dest}") + log.info( + f"Running createstubs {variant.value} on {mcu.serialport} {mcu.description} using temp path: {dest}" + ) cmd = build_cmd(dest, variant) log.info(f"Running : mpremote {' '.join(cmd)}") mcu.run_command.retry.wait = wait_fixed(15) @@ -155,7 +160,13 @@ def run_createstubs(dest: Path, mcu: MPRemoteBoard, variant: Variant = Variant.d timeout = 90 if mcu.port == "esp8266" else 6 * 60 # type: ignore rc, out = mcu.run_command(cmd, timeout=timeout) # check last line for exception or error and raise that if found - if rc != OK and out and ":" in out[-1] and not out[-1].startswith("INFO") and not out[-1].startswith("WARN"): + if ( + rc != OK + and out + and ":" in out[-1] + and not out[-1].startswith("INFO") + and not out[-1].startswith("WARN") + ): log.warning(f"createstubs: {out[-1]}") raise RuntimeError(out[-1]) from eval(out[-1].split(":")[0]) @@ -250,7 +261,10 @@ def copy_boardname_to_board(mcu: MPRemoteBoard): None """ if mcu.board: - cmd = ["exec", f"with open('lib/boardname.py', 'w') as f: f.write('BOARDNAME=\"{mcu.board}\"')"] + cmd = [ + "exec", + f"with open('lib/boardname.py', 'w') as f: f.write('BOARDNAME=\"{mcu.board}\"')", + ] log.info(f"Writing BOARDNAME='{mcu.board}' to boardname.py") else: cmd = ["rm", "boardname.py"] @@ -285,31 +299,12 @@ def copy_scripts_to_board(mcu: MPRemoteBoard, variant: Variant, form: Form): def get_stubfolder(out: List[str]): return ( - lines[-1].split("/remote/")[-1].strip() if (lines := [l for l in out if l.startswith("INFO : Path: ")]) else "" + lines[-1].split("/remote/")[-1].strip() + if (lines := [l for l in out if l.startswith("INFO : Path: ")]) + else "" ) -def scan_boards(optimistic: bool = False) -> List[MPRemoteBoard]: - """ - This function scans for boards and returns a list of MPRemoteBoard objects. - :return: list of MPRemoteBoard objects - """ - boards = [] - for mpr_port in MPRemoteBoard.connected_boards(): - board = MPRemoteBoard(mpr_port) - log.debug(f"Attempt to connect to: {board.serialport}") - try: - board.get_mcu_info() - log.success(f"Detected board {board.description} {board.version}") - boards.append(board) - except Exception: - log.error(f"Failed to get mcu_info for {board.serialport}") - if optimistic: - boards.append(board) - continue - return boards - - def set_loglevel(verbose: int) -> str: """Set log level based on verbose level Get the level from the verbose setting (0=INFO, 1=DEBUG, 2=TRACE) @@ -324,7 +319,9 @@ def set_loglevel(verbose: int) -> str: else: format_str = "{time:YYYY-MM-DD HH:mm:ss.SSS}|{level: <8}|{name}:{function}:{line} - {message}" - log.add(sys.stderr, level=level, backtrace=True, diagnose=True, colorize=True, format=format_str) + log.add( + sys.stderr, level=level, backtrace=True, diagnose=True, colorize=True, format=format_str + ) # log.info(f"micropython-stubber {__version__}") return level @@ -347,7 +344,14 @@ def copy_to_repo(source: Path, fw: dict) -> Optional[Path]: return None -def stub_connected_mcus(variant: str, format: str, debug: bool) -> int: +def stub_connected_mcus( + variant: str, + format: str, + debug: bool, + serial: List[str], + ignore: List[str], + bluetooth: bool, +) -> int: """ Runs the stubber to generate stubs for connected MicroPython boards. @@ -372,24 +376,16 @@ def stub_connected_mcus(variant: str, format: str, debug: bool) -> int: all_built = [] # scan boards and just work with the ones that respond with understandable data - connected_boards = scan_boards(True) - if not connected_boards: + connected_mcus = list_mcus(ignore=ignore, include=serial, bluetooth=bluetooth) + + if not connected_mcus: log.error("No micropython boards were found") return ERROR - table = Table(show_header=True, header_style="bold magenta") - table.add_column("Serial Port") - table.add_column("Port") - table.add_column("Description") - table.add_column("Version") - - for b in connected_boards: - table.add_row(b.serialport, b.port, b.description, b.version) - console = Console() - console.print(table) + show_mcus(connected_mcus, refresh=False) # scan boards and generate stubs - for board in connected_boards: + for board in connected_mcus: log.info( f"Connecting using {board.serialport} to {board.port} {board.board} {board.version}: {board.description}" ) @@ -428,14 +424,16 @@ def stub_connected_mcus(variant: str, format: str, debug: bool) -> int: all_built.extend(built) if all_built: - print_result_table(all_built, console) + print_result_table(all_built) log.success("Done") return OK log.error(f"Failed to generate stubs for {board.serialport}") return ERROR -def print_result_table(all_built: List, console: Console): +def print_result_table(all_built: List, console: Optional[Console] = None): + if not console: + console = Console() # create a rich table of the results and print it' table = Table(title="Results") diff --git a/src/stubber/codemod/enrich.py b/src/stubber/codemod/enrich.py index 5cad93d18..c644a24c2 100644 --- a/src/stubber/codemod/enrich.py +++ b/src/stubber/codemod/enrich.py @@ -8,7 +8,7 @@ from libcst.codemod import CodemodContext, diff_code, exec_transform_with_prettyprint from libcst.tool import _default_config # type: ignore -from loguru import logger as log +from mpflash.logger import log import stubber.codemod.merge_docstub as merge_docstub from stubber.utils.post import run_black diff --git a/src/stubber/codemod/merge_docstub.py b/src/stubber/codemod/merge_docstub.py index 86d179bd3..c1dc7b34a 100644 --- a/src/stubber/codemod/merge_docstub.py +++ b/src/stubber/codemod/merge_docstub.py @@ -13,7 +13,7 @@ from libcst.codemod import CodemodContext, VisitorBasedCodemodCommand from libcst.codemod.visitors import AddImportsVisitor, GatherImportsVisitor, ImportItem from libcst.helpers.module import insert_header_comments -from loguru import logger as log +from mpflash.logger import log from stubber.cst_transformer import ( MODULE_KEY, diff --git a/src/stubber/commands/build_cmd.py b/src/stubber/commands/build_cmd.py index 2dda6b9c2..5813c513f 100644 --- a/src/stubber/commands/build_cmd.py +++ b/src/stubber/commands/build_cmd.py @@ -3,7 +3,7 @@ from typing import List, Union import rich_click as click -from loguru import logger as log +from mpflash.logger import log from tabulate import tabulate from stubber.commands.cli import stubber_cli diff --git a/src/stubber/commands/cli.py b/src/stubber/commands/cli.py index a33576538..027fd904b 100644 --- a/src/stubber/commands/cli.py +++ b/src/stubber/commands/cli.py @@ -4,12 +4,13 @@ import sys +from mpflash.vendor.click_aliases import ClickAliasedGroup import rich_click as click -from loguru import logger as log +from mpflash.logger import log, set_loglevel as mpf_set_loglevel from stubber import __version__ -@click.group(chain=True) +@click.group(chain=True, cls=ClickAliasedGroup) @click.version_option(package_name="micropython-stubber", prog_name="micropython-stubber✏️ ") @click.option( "-V", @@ -41,15 +42,8 @@ def set_loglevel(verbose: int) -> str: Add the handler to the logger, with the level and format string. Return the level """ - log.remove() level = {0: "INFO", 1: "DEBUG", 2: "TRACE"}.get(verbose, "TRACE") - if level == "INFO": - format_str = "{time:HH:mm:ss} | {level: <8} | {module: <18} - {message}" - else: - format_str = "{time:YYYY-MM-DD HH:mm:ss.SSS} | {level: <8} | {name}:{function}:{line} - {message}" - - log.add( - sys.stderr, level=level, backtrace=True, diagnose=True, colorize=True, format=format_str - ) + # reuse mpflash logger + mpf_set_loglevel(level) log.info(f"micropython-stubber {__version__}") return level diff --git a/src/stubber/commands/clone_cmd.py b/src/stubber/commands/clone_cmd.py index 5d21d3b5c..6f13ba9c8 100644 --- a/src/stubber/commands/clone_cmd.py +++ b/src/stubber/commands/clone_cmd.py @@ -9,7 +9,7 @@ import rich_click as click import mpflash.basicgit as git -from loguru import logger as log +from mpflash.logger import log from stubber.utils.config import CONFIG from .cli import stubber_cli diff --git a/src/stubber/commands/config_cmd.py b/src/stubber/commands/config_cmd.py index 7cd413248..853ad542f 100644 --- a/src/stubber/commands/config_cmd.py +++ b/src/stubber/commands/config_cmd.py @@ -2,7 +2,7 @@ # pragma: no cover -from loguru import logger as log +from mpflash.logger import log from stubber.utils.config import CONFIG diff --git a/src/stubber/commands/enrich_folder_cmd.py b/src/stubber/commands/enrich_folder_cmd.py index bbe8553c4..e012b4e60 100644 --- a/src/stubber/commands/enrich_folder_cmd.py +++ b/src/stubber/commands/enrich_folder_cmd.py @@ -6,7 +6,7 @@ from typing import Union import rich_click as click -from loguru import logger as log +from mpflash.logger import log from stubber.codemod.enrich import enrich_folder from stubber.utils.config import CONFIG diff --git a/src/stubber/commands/get_core_cmd.py b/src/stubber/commands/get_core_cmd.py index f2ad4025f..264dd6e4a 100644 --- a/src/stubber/commands/get_core_cmd.py +++ b/src/stubber/commands/get_core_cmd.py @@ -8,7 +8,7 @@ from typing import List import rich_click as click -from loguru import logger as log +from mpflash.logger import log import stubber.get_cpython as get_cpython import stubber.utils as utils diff --git a/src/stubber/commands/get_docstubs_cmd.py b/src/stubber/commands/get_docstubs_cmd.py index e85e1fb8e..acbf9fd13 100644 --- a/src/stubber/commands/get_docstubs_cmd.py +++ b/src/stubber/commands/get_docstubs_cmd.py @@ -7,7 +7,7 @@ from typing import Optional import rich_click as click -from loguru import logger as log +from mpflash.logger import log import mpflash.basicgit as git import stubber.utils as utils @@ -22,7 +22,10 @@ ######################################################################################### -@stubber_cli.command(name="get-docstubs") +@stubber_cli.command( + name="get-docstubs", + aliases=["get-doc-stubs", "docstubs"], +) @click.option( "--path", "-p", diff --git a/src/stubber/commands/get_frozen_cmd.py b/src/stubber/commands/get_frozen_cmd.py index f672eb1ad..5cd4a18ef 100644 --- a/src/stubber/commands/get_frozen_cmd.py +++ b/src/stubber/commands/get_frozen_cmd.py @@ -7,7 +7,7 @@ from typing import List, Optional import rich_click as click -from loguru import logger as log +from mpflash.logger import log import stubber.utils as utils from stubber.codemod.enrich import enrich_folder @@ -20,7 +20,10 @@ ########################################################################################## -@stubber_cli.command(name="get-frozen") +@stubber_cli.command( + name="get-frozen", + aliases=["get-frozen-stubs", "frozen"], +) @click.option( "--stub-folder", "-stubs", diff --git a/src/stubber/commands/get_mcu_cmd.py b/src/stubber/commands/get_mcu_cmd.py index 8ad9714de..31ffa1ce7 100644 --- a/src/stubber/commands/get_mcu_cmd.py +++ b/src/stubber/commands/get_mcu_cmd.py @@ -5,8 +5,9 @@ ########################################################################################## +from typing import List import rich_click as click -from loguru import logger as log +from mpflash.logger import log from stubber.bulk.mcu_stubber import stub_connected_mcus from stubber.utils.config import CONFIG @@ -18,7 +19,10 @@ ######################################################################################### -@stubber_cli.command(name="get-mcu-stubs") +@stubber_cli.command( + name="get-mcu-stubs", + aliases=["get-mcu-stubs", "mcu-stubs", "mcu"], +) @click.option( "--variant", # "-v", @@ -35,22 +39,50 @@ show_default=True, help="Python source or pre-compiled.", ) -@click.option("--debug/--no-debug", default=False, show_default=True, help="Debug mode.") @click.option( - "--reset/--no-reset", - default=False, + "--serial", + "--serial-port", + "-s", + "serial", + default=["*"], + multiple=True, show_default=True, - help="Reset the board before running createstubs.", + help="Which serial port(s) to list. ", + metavar="SERIALPORT", ) @click.option( - "--github/--local", - default=True, + "--ignore", + "-i", + is_eager=True, + help="Serial port(s) to ignore. Defaults to MPFLASH_IGNORE.", + multiple=True, + default=[], + envvar="MPFLASH_IGNORE", show_default=True, - help="where to install the board files from. local is intended for development.", + metavar="SERIALPORT", ) -def cli_create_mcu_stubs(variant: str, format: str, debug: bool, reset: bool, github: bool) -> int: +@click.option( + "--bluetooth/--no-bluetooth", + "-b/-nb", + is_flag=True, + default=False, + show_default=True, + help="""Include bluetooth ports in the list""", +) +@click.option("--debug/--no-debug", default=False, show_default=True, help="Debug mode.") +def cli_create_mcu_stubs( + variant: str, + format: str, + debug: bool, + serial: List[str], + ignore: List[str], + bluetooth: bool, +) -> int: """Run createstubs on one or more MCUs, and add the stubs to the micropython-stub repo.""" # check if all repos have been cloned + serial = list(serial) + ignore = list(ignore) + for repo in CONFIG.repos: if not repo.exists(): log.error( @@ -58,4 +90,13 @@ def cli_create_mcu_stubs(variant: str, format: str, debug: bool, reset: bool, gi ) exit(1) - exit(stub_connected_mcus(variant=variant, format=format, debug=debug)) + exit( + stub_connected_mcus( + variant=variant, + format=format, + debug=debug, + serial=serial, + ignore=ignore, + bluetooth=bluetooth, + ) + ) diff --git a/src/stubber/commands/merge_cmd.py b/src/stubber/commands/merge_cmd.py index a0d4263b2..5ea886107 100644 --- a/src/stubber/commands/merge_cmd.py +++ b/src/stubber/commands/merge_cmd.py @@ -5,7 +5,7 @@ from typing import List, Union import rich_click as click -from loguru import logger as log +from mpflash.logger import log from stubber.publish.merge_docstubs import merge_all_docstubs from stubber.publish.package import GENERIC_L diff --git a/src/stubber/commands/publish_cmd.py b/src/stubber/commands/publish_cmd.py index bacfc7d89..f8404d069 100644 --- a/src/stubber/commands/publish_cmd.py +++ b/src/stubber/commands/publish_cmd.py @@ -5,7 +5,7 @@ from typing import List, Union import rich_click as click -from loguru import logger as log +from mpflash.logger import log from tabulate import tabulate from stubber.commands.cli import stubber_cli diff --git a/src/stubber/commands/stub_cmd.py b/src/stubber/commands/stub_cmd.py index 399546198..1832e9f55 100644 --- a/src/stubber/commands/stub_cmd.py +++ b/src/stubber/commands/stub_cmd.py @@ -4,7 +4,7 @@ # stub ########################################################################################## -from loguru import logger as log +from mpflash.logger import log from pathlib import Path from typing import Union diff --git a/src/stubber/commands/variants_cmd.py b/src/stubber/commands/variants_cmd.py index 2f1ecc053..05a01243d 100644 --- a/src/stubber/commands/variants_cmd.py +++ b/src/stubber/commands/variants_cmd.py @@ -3,7 +3,7 @@ from pathlib import Path import rich_click as click -from loguru import logger as log +from mpflash.logger import log from stubber.utils.config import CONFIG from stubber.variants import create_variants import stubber diff --git a/src/stubber/downloader.py b/src/stubber/downloader.py index aa5460532..8e16692e1 100644 --- a/src/stubber/downloader.py +++ b/src/stubber/downloader.py @@ -1,11 +1,12 @@ """Download files from a public github repo""" + # Copyright (c) 2020 Jos Verlinde # MIT license # pylint: disable= invalid-name import os import requests -from loguru import logger as log +from mpflash.logger import log # # log = logging.getLogger(__name__) # log.setLevel(level=logging.INFO) diff --git a/src/stubber/freeze/common.py b/src/stubber/freeze/common.py index d1e2f27df..fe3d4d4e8 100644 --- a/src/stubber/freeze/common.py +++ b/src/stubber/freeze/common.py @@ -5,7 +5,7 @@ from pathlib import Path from typing import Tuple -from loguru import logger as log +from mpflash.logger import log from stubber.publish.defaults import GENERIC_U @@ -17,7 +17,9 @@ def get_portboard(manifest_path: Path): raises an ValueError if neither a port or board can be found """ # https://regex101.com/r/tv7JX4/1 - re_pb = r".*micropython[\/\\]ports[\/\\](?P[\w_-]*)([\/\\]boards[\/\\](?P\w*))?[\/\\]" + re_pb = ( + r".*micropython[\/\\]ports[\/\\](?P[\w_-]*)([\/\\]boards[\/\\](?P\w*))?[\/\\]" + ) mpy_port = mpy_board = "" if matches := re.search(re_pb, manifest_path.absolute().as_posix()): # port and board @@ -50,7 +52,9 @@ def apply_frozen_module_fixes(freeze_path: Path, mpy_path: Path): apply common fixes to the fozen modules to improve stub generation """ # NOTE: FIX 1 add __init__.py to umqtt - if (freeze_path / "umqtt/robust.py").exists(): # and not (freeze_path / "umqtt" / "__init__.py").exists(): + if ( + freeze_path / "umqtt/robust.py" + ).exists(): # and not (freeze_path / "umqtt" / "__init__.py").exists(): log.debug("add missing : umqtt/__init__.py") with open(freeze_path / "umqtt" / "__init__.py", "a") as f: f.write("") diff --git a/src/stubber/freeze/freeze_folder.py b/src/stubber/freeze/freeze_folder.py index cca404cd3..d3327bcf5 100644 --- a/src/stubber/freeze/freeze_folder.py +++ b/src/stubber/freeze/freeze_folder.py @@ -6,7 +6,7 @@ import warnings from pathlib import Path # start moving from os & glob to pathlib -from loguru import logger as log +from mpflash.logger import log from stubber.utils.repos import match_lib_with_mpy from .. import utils @@ -25,7 +25,7 @@ def freeze_folders(stub_folder: str, mpy_folder: str, lib_folder: str, version: - 'ports//modules/*.py' - 'ports//boards//modules/*.py' """ - match_lib_with_mpy(version_tag=version, mpy_path=Path(mpy_folder),lib_path=Path(lib_folder)) + match_lib_with_mpy(version_tag=version, mpy_path=Path(mpy_folder), lib_path=Path(lib_folder)) targets = [] scripts = glob.glob(mpy_folder + "/ports/**/modules/*.py", recursive=True) diff --git a/src/stubber/freeze/freeze_manifest_2.py b/src/stubber/freeze/freeze_manifest_2.py index 7b45bbac5..3b64fc231 100644 --- a/src/stubber/freeze/freeze_manifest_2.py +++ b/src/stubber/freeze/freeze_manifest_2.py @@ -9,7 +9,7 @@ from pathlib import Path from typing import List, Optional -from loguru import logger as log +from mpflash.logger import log from stubber import utils from stubber.tools.manifestfile import MODE_FREEZE, ManifestFile, ManifestFileError, ManifestOutput @@ -51,7 +51,9 @@ def make_path_vars( } -def freeze_one_manifest_2(manifest: Path, frozen_stub_path: Path, mpy_path: Path, mpy_lib_path: Path, version: str): +def freeze_one_manifest_2( + manifest: Path, frozen_stub_path: Path, mpy_path: Path, mpy_lib_path: Path, version: str +): # apparently there can be multiple manifest files to a board ? # save cwd for 'misbehaving' older esp8266 manifest files cwd = Path.cwd() @@ -61,7 +63,9 @@ def freeze_one_manifest_2(manifest: Path, frozen_stub_path: Path, mpy_path: Path log.info("port-board: {}".format((port + "-" + board).rstrip("-"))) - path_vars = make_path_vars(port=port, board=board, mpy_path=mpy_path, mpy_lib_path=mpy_lib_path) + path_vars = make_path_vars( + port=port, board=board, mpy_path=mpy_path, mpy_lib_path=mpy_lib_path + ) upy_manifest = ManifestFile(MODE_FREEZE, path_vars) try: # assume manifestneeds to be run from the port's folder @@ -75,11 +79,18 @@ def freeze_one_manifest_2(manifest: Path, frozen_stub_path: Path, mpy_path: Path # restore working directory os.chdir(cwd) # save the frozen files to the stubs - copy_frozen_to_stubs(frozen_stub_path, port, board, upy_manifest.files(), version, mpy_path=mpy_path) + copy_frozen_to_stubs( + frozen_stub_path, port, board, upy_manifest.files(), version, mpy_path=mpy_path + ) def copy_frozen_to_stubs( - stub_path: Path, port: str, board: str, files: List[ManifestOutput], version: str, mpy_path: Path + stub_path: Path, + port: str, + board: str, + files: List[ManifestOutput], + version: str, + mpy_path: Path, ): """ copy the frozen files from the manifest to the stubs folder @@ -110,4 +121,6 @@ def copy_frozen_to_stubs( # make a module manifest FAMILY = "micropython" - utils.make_manifest(freeze_path, FAMILY, port=port, board=board, version=version, stubtype="frozen") + utils.make_manifest( + freeze_path, FAMILY, port=port, board=board, version=version, stubtype="frozen" + ) diff --git a/src/stubber/freeze/get_frozen.py b/src/stubber/freeze/get_frozen.py index 7ce1ecddd..c1de4c53b 100644 --- a/src/stubber/freeze/get_frozen.py +++ b/src/stubber/freeze/get_frozen.py @@ -22,7 +22,7 @@ from pathlib import Path from typing import List, Optional -from loguru import logger as log +from mpflash.logger import log from packaging.version import Version from mpflash.versions import SET_PREVIEW, V_PREVIEW @@ -83,7 +83,9 @@ def freeze_any( frozen_stub_path = get_fsp(version, stub_folder) log.debug("MicroPython v1.11, older or other") # others - modules = freeze_folders(frozen_stub_path.as_posix(), mpy_path.as_posix(), mpy_lib_path.as_posix(), version) + modules = freeze_folders( + frozen_stub_path.as_posix(), mpy_path.as_posix(), mpy_lib_path.as_posix(), version + ) count = len(modules) else: # get the current checked out version @@ -120,7 +122,9 @@ def freeze_any( def get_fsp(version: str, stub_folder: Optional[Path] = None) -> Path: if not stub_folder: - frozen_stub_path = CONFIG.stub_path / f"{FAMILY}-{utils.clean_version(version, flat=True)}-frozen" + frozen_stub_path = ( + CONFIG.stub_path / f"{FAMILY}-{utils.clean_version(version, flat=True)}-frozen" + ) frozen_stub_path = frozen_stub_path.absolute() else: frozen_stub_path: Path = Path(stub_folder).absolute() diff --git a/src/stubber/get_cpython.py b/src/stubber/get_cpython.py index aa1274b00..c2df4b327 100644 --- a/src/stubber/get_cpython.py +++ b/src/stubber/get_cpython.py @@ -2,6 +2,7 @@ Download or update the MicroPython compatibility modules from pycopy and stores them in the all_stubs folder The all_stubs folder should be mapped/symlinked to the micropython_stubs/stubs repo/folder """ + # pragma: no cover import json import os @@ -12,7 +13,7 @@ from pathlib import Path from typing import Optional, Union -from loguru import logger as log +from mpflash.logger import log from . import __version__ @@ -22,7 +23,9 @@ # # log = logging.getLogger(__name__) -def get_core(requirements: str, stub_path: Optional[Union[str, Path]] = None, family: str = "core"): +def get_core( + requirements: str, stub_path: Optional[Union[str, Path]] = None, family: str = "core" +): "Download MicroPython compatibility modules" if not stub_path: stub_path = CONFIG.stub_path / "cpython-core" # pragma: no cover @@ -71,7 +74,9 @@ def get_core(requirements: str, stub_path: Optional[Union[str, Path]] = None, fa except OSError as err: # pragma: no cover log.error( - "An error occurred while trying to run pip to download the MicroPython compatibility modules from PyPi: {}".format(err) + "An error occurred while trying to run pip to download the MicroPython compatibility modules from PyPi: {}".format( + err + ) ) # copy *.py files in build folder to stub_path @@ -86,7 +91,13 @@ def get_core(requirements: str, stub_path: Optional[Union[str, Path]] = None, fa log.exception(err) # build modules.json - mod_manifest = utils.manifest(family="cpython-core", port=family, version=__version__, stubtype="core", platform="cpython") + mod_manifest = utils.manifest( + family="cpython-core", + port=family, + version=__version__, + stubtype="core", + platform="cpython", + ) mod_manifest["modules"] += modlist if mod_manifest and len(modlist): diff --git a/src/stubber/minify.py b/src/stubber/minify.py index a5609ca47..3b4e0d4e8 100644 --- a/src/stubber/minify.py +++ b/src/stubber/minify.py @@ -16,7 +16,7 @@ except ImportError: python_minifier = None -from loguru import logger as log +from mpflash.logger import log from mpflash.versions import SET_PREVIEW, V_PREVIEW @@ -205,7 +205,9 @@ def minify_script(source_script: StubSource, keep_report: bool = True, diff: boo elif isinstance(source_script, str): # type: ignore source_content = source_script else: - raise TypeError(f"source_script must be str, Path, or file-like object, not {type(source_script)}") + raise TypeError( + f"source_script must be str, Path, or file-like object, not {type(source_script)}" + ) if not source_content: raise ValueError("No source content") @@ -400,7 +402,9 @@ def pipx_mpy_cross(version: str, source_file, _target): # Add params cmd += ["-O2", str(source_file), "-o", str(_target), "-s", "createstubs.py"] log.trace(" ".join(cmd)) - result = subprocess.run(cmd, capture_output=True, text=True, encoding="utf-8") # Specify the encoding + result = subprocess.run( + cmd, capture_output=True, text=True, encoding="utf-8" + ) # Specify the encoding return result diff --git a/src/stubber/publish/candidates.py b/src/stubber/publish/candidates.py index 8a8b72830..51bd97be6 100644 --- a/src/stubber/publish/candidates.py +++ b/src/stubber/publish/candidates.py @@ -14,11 +14,17 @@ from pathlib import Path from typing import Any, Dict, Generator, List, Optional, Union -from loguru import logger as log +from mpflash.logger import log from packaging.version import parse import mpflash.basicgit as git -from mpflash.versions import OLDEST_VERSION, SET_PREVIEW, V_PREVIEW, clean_version, micropython_versions +from mpflash.versions import ( + OLDEST_VERSION, + SET_PREVIEW, + V_PREVIEW, + clean_version, + micropython_versions, +) from stubber import utils from stubber.publish.defaults import GENERIC, GENERIC_L, GENERIC_U @@ -108,7 +114,9 @@ def frozen_candidates( auto_port = is_auto(ports) auto_board = is_auto(boards) if is_auto(versions): - versions = list(version_candidates(suffix="frozen", prefix=family, path=path)) + [V_PREVIEW] + versions = list(version_candidates(suffix="frozen", prefix=family, path=path)) + [ + V_PREVIEW + ] else: versions = [versions] if isinstance(versions, str) else versions @@ -126,7 +134,9 @@ def frozen_candidates( # lookup the (frozen) micropython ports ports = list_frozen_ports(family, version, path=path) else: - raise NotImplementedError(f"auto ports not implemented for family {family}") # pragma: no cover + raise NotImplementedError( + f"auto ports not implemented for family {family}" + ) # pragma: no cover # elif family == "pycom": # ports = ["esp32"] # elif family == "lobo": @@ -159,7 +169,9 @@ def frozen_candidates( else: # raise NotImplementedError(f"auto boards not implemented for family {family}") # pragma: no cover - raise NotImplementedError(f"auto boards not implemented for family {family}") # pragma: no cover + raise NotImplementedError( + f"auto boards not implemented for family {family}" + ) # pragma: no cover # elif family == "pycom": # boards = ["wipy", "lopy", "gpy", "fipy"] # --------------------------------------------------------------------------- @@ -215,7 +227,9 @@ def board_candidates( else: r = git.checkout_tag(repo=mpy_path, tag=version) if not r: - log.warning(f"Incorrect version: {version} or did you forget to run `stubber clone` to get the micropython repo?") + log.warning( + f"Incorrect version: {version} or did you forget to run `stubber clone` to get the micropython repo?" + ) return [] ports = list_micropython_ports(family=family, mpy_path=mpy_path) for port in ports: @@ -252,5 +266,10 @@ def filter_list( worklist = [i for i in worklist if i["port"].lower() in ports_] if boards and not is_auto(boards): boards_ = [i.lower() for i in boards] - worklist = [i for i in worklist if i["board"].lower() in boards_ or i["board"].lower().replace("generic_", "") in boards_] + worklist = [ + i + for i in worklist + if i["board"].lower() in boards_ + or i["board"].lower().replace("generic_", "") in boards_ + ] return worklist diff --git a/src/stubber/publish/merge_docstubs.py b/src/stubber/publish/merge_docstubs.py index 988b956dd..fa9d7c562 100644 --- a/src/stubber/publish/merge_docstubs.py +++ b/src/stubber/publish/merge_docstubs.py @@ -6,7 +6,7 @@ from pathlib import Path from typing import List, Optional, Union -from loguru import logger as log +from mpflash.logger import log from stubber.codemod.enrich import enrich_folder from stubber.publish.candidates import board_candidates, filter_list @@ -49,7 +49,9 @@ def merge_all_docstubs( for candidate in candidates: # use the default board for the port if candidate["board"] in GENERIC: - candidate["board"] = default_board(port=candidate["port"], version=candidate["version"]) + candidate["board"] = default_board( + port=candidate["port"], version=candidate["version"] + ) # check if we have MCU stubs of this version and port doc_path = CONFIG.stub_path / f"{get_base(candidate)}-docstubs" # src and dest paths diff --git a/src/stubber/publish/missing_class_methods.py b/src/stubber/publish/missing_class_methods.py index c862ada81..fbec271e2 100644 --- a/src/stubber/publish/missing_class_methods.py +++ b/src/stubber/publish/missing_class_methods.py @@ -6,7 +6,7 @@ from pathlib import Path import libcst as cst -from loguru import logger as log +from mpflash.logger import log from mpflash.versions import clean_version from stubber.codemod.add_method import CallAdder, CallFinder @@ -23,7 +23,9 @@ def add_machine_pin_call(merged_path: Path, version: str): # and to avoid having to parse the file twice # first find the __call__ method in the default stubs - mod_path = CONFIG.stub_path / f"micropython-{clean_version(version, flat=True)}-docstubs/machine.pyi" + mod_path = ( + CONFIG.stub_path / f"micropython-{clean_version(version, flat=True)}-docstubs/machine.pyi" + ) if not mod_path.exists(): log.error(f"no docstubs found for {version}") return False diff --git a/src/stubber/publish/package.py b/src/stubber/publish/package.py index 5ae62fcf4..c3ffb55d1 100644 --- a/src/stubber/publish/package.py +++ b/src/stubber/publish/package.py @@ -7,7 +7,7 @@ from pathlib import Path from typing import Dict, Union -from loguru import logger as log +from mpflash.logger import log from packaging.version import parse from pysondb import PysonDB @@ -62,7 +62,9 @@ def get_package( ) -def get_package_info(db: PysonDB, pub_path: Path, *, pkg_name: str, mpy_version: str) -> Union[Dict, None]: +def get_package_info( + db: PysonDB, pub_path: Path, *, pkg_name: str, mpy_version: str +) -> Union[Dict, None]: """ get a package's record from the json db if it can be found matches om the package name and version @@ -70,7 +72,9 @@ def get_package_info(db: PysonDB, pub_path: Path, *, pkg_name: str, mpy_version: mpy_version: micropython/firmware version (1.18) """ # find in the database - recs = db.get_by_query(query=lambda x: x["mpy_version"] == mpy_version and x["name"] == pkg_name) + recs = db.get_by_query( + query=lambda x: x["mpy_version"] == mpy_version and x["name"] == pkg_name + ) # dict to list recs = [{"id": key, "data": recs[key]} for key in recs] # sort diff --git a/src/stubber/publish/pathnames.py b/src/stubber/publish/pathnames.py index afc61d41a..bf9cf25c2 100644 --- a/src/stubber/publish/pathnames.py +++ b/src/stubber/publish/pathnames.py @@ -6,7 +6,7 @@ from pathlib import Path from typing import Dict, Optional -from loguru import logger as log +from mpflash.logger import log from mpflash.versions import V_PREVIEW, clean_version from stubber.publish.defaults import default_board diff --git a/src/stubber/publish/publish.py b/src/stubber/publish/publish.py index 8a5fb60ff..d4771e2bc 100644 --- a/src/stubber/publish/publish.py +++ b/src/stubber/publish/publish.py @@ -6,7 +6,7 @@ from typing import Any, Dict, List, Optional, Union -from loguru import logger as log +from mpflash.logger import log from mpflash.versions import V_PREVIEW from stubber.publish.candidates import board_candidates, filter_list diff --git a/src/stubber/publish/pypi.py b/src/stubber/publish/pypi.py index 27f85ca95..274437749 100644 --- a/src/stubber/publish/pypi.py +++ b/src/stubber/publish/pypi.py @@ -7,7 +7,7 @@ from packaging.version import Version, parse from pypi_simple import PyPISimple, NoSuchProjectError -from loguru import logger as log +from mpflash.logger import log def get_pypi_versions(package_name: str, base: Optional[Version] = None, production: bool = True): @@ -28,7 +28,11 @@ def get_pypi_versions(package_name: str, base: Optional[Version] = None, product if not package_info: return [] - versions = [parse(pkg.version) for pkg in package_info.packages if pkg.package_type == "wheel" and pkg.version] + versions = [ + parse(pkg.version) + for pkg in package_info.packages + if pkg.package_type == "wheel" and pkg.version + ] # print(versions) if base: diff --git a/src/stubber/publish/stubpackage.py b/src/stubber/publish/stubpackage.py index 4231aa3bf..ce1a0130a 100644 --- a/src/stubber/publish/stubpackage.py +++ b/src/stubber/publish/stubpackage.py @@ -20,7 +20,7 @@ from typing import NewType import tomli_w -from loguru import logger as log +from mpflash.logger import log from packaging.version import Version, parse from pysondb import PysonDB @@ -125,9 +125,15 @@ def _get_next_preview_package_version(self, production: bool = False) -> str: parts = describe.split("-", 3) ver = parts[0] if len(parts) > 1: - rc = parts[1] if parts[1].isdigit() else parts[2] if len(parts) > 2 and parts[2].isdigit() else 1 + rc = ( + parts[1] + if parts[1].isdigit() + else parts[2] if len(parts) > 2 and parts[2].isdigit() else 1 + ) rc = int(rc) - base = bump_version(Version(ver), minor_bump=True) if parts[1] != V_PREVIEW else Version(ver) + base = ( + bump_version(Version(ver), minor_bump=True) if parts[1] != V_PREVIEW else Version(ver) + ) return str(bump_version(base, rc=rc)) # raise ValueError("cannot determine next version number micropython") @@ -304,7 +310,9 @@ def copy_stubs(self) -> None: # Check if all stub source folders exist for stub_type, src_path in self.stub_sources: if not (CONFIG.stub_path / src_path).exists(): - raise FileNotFoundError(f"Could not find stub source folder {CONFIG.stub_path / src_path}") + raise FileNotFoundError( + f"Could not find stub source folder {CONFIG.stub_path / src_path}" + ) # 1 - Copy the stubs to the package, directly in the package folder (no folders) # for stub_type, fw_path in [s for s in self.stub_sources]: @@ -315,7 +323,9 @@ def copy_stubs(self) -> None: self.copy_folder(stub_type, src_path) except OSError as e: if stub_type != StubSource.FROZEN: - raise FileNotFoundError(f"Could not find stub source folder {src_path}") from e + raise FileNotFoundError( + f"Could not find stub source folder {src_path}" + ) from e else: log.debug(f"Error copying stubs from : {CONFIG.stub_path / src_path}, {e}") finally: @@ -707,7 +717,8 @@ def update_pyproject_stubs(self) -> int: _pyproject = self.pyproject assert _pyproject is not None, "No pyproject.toml file found" _pyproject["tool"]["poetry"]["packages"] = [ - {"include": p.relative_to(self.package_path).as_posix()} for p in sorted((self.package_path).rglob("*.pyi")) + {"include": p.relative_to(self.package_path).as_posix()} + for p in sorted((self.package_path).rglob("*.pyi")) ] # write out the pyproject.toml file self.pyproject = _pyproject @@ -851,7 +862,9 @@ def update_distribution(self, production: bool) -> bool: # check if the sources exist ok = self.are_package_sources_available() if not ok: - log.debug(f"{self.package_name}: skipping as one or more source stub folders are missing") + log.debug( + f"{self.package_name}: skipping as one or more source stub folders are missing" + ) self.status["error"] = "Skipped, stub folder(s) missing" shutil.rmtree(self.package_path.as_posix()) self._publish = False # type: ignore @@ -873,7 +886,9 @@ def build_distribution( self, production: bool, # PyPI or Test-PyPi - USED TO FIND THE NEXT VERSION NUMBER force=False, # BUILD even if no changes - ) -> bool: # sourcery skip: default-mutable-arg, extract-duplicate-method, require-parameter-annotation + ) -> ( + bool + ): # sourcery skip: default-mutable-arg, extract-duplicate-method, require-parameter-annotation """ Build a package look up the previous package version in the dabase @@ -899,14 +914,18 @@ def build_distribution( if force: log.info(f"Force build: {self.package_name} {self.pkg_version} ") else: - log.info(f"Found changes to package sources: {self.package_name} {self.pkg_version} ") + log.info( + f"Found changes to package sources: {self.package_name} {self.pkg_version} " + ) log.trace(f"Old hash {self.hash} != New hash {self.calculate_hash()}") # Build the distribution files old_ver = self.pkg_version self.pkg_version = self.next_package_version(production) self.status["version"] = self.pkg_version # to get the next version - log.debug(f"{self.package_name}: bump version for {old_ver} to {self.pkg_version } {'production' if production else 'test'}") + log.debug( + f"{self.package_name}: bump version for {old_ver} to {self.pkg_version } {'production' if production else 'test'}" + ) self.write_package_json() log.trace(f"New hash: {self.package_name} {self.pkg_version} {self.hash}") if self.poetry_build(): @@ -959,7 +978,9 @@ def publish_distribution_ifchanged( # Publish the package to PyPi, Test-PyPi or Github if self.is_changed(): if self.mpy_version in SET_PREVIEW and production and not force: - log.warning("version: `latest` package will only be available on Github, and not published to PyPi.") + log.warning( + "version: `latest` package will only be available on Github, and not published to PyPi." + ) self.status["result"] = "Published to GitHub" else: return self.publish_distribution(dry_run, production, db) @@ -988,7 +1009,9 @@ def publish_distribution(self, dry_run, production, db): if not dry_run: pub_ok = self.poetry_publish(production=production) else: - log.warning(f"{self.package_name}: Dry run, not publishing to {'' if production else 'Test-'}PyPi") + log.warning( + f"{self.package_name}: Dry run, not publishing to {'' if production else 'Test-'}PyPi" + ) pub_ok = True if not pub_ok: log.warning(f"{self.package_name}: Publish failed for {self.pkg_version}") diff --git a/src/stubber/rst/classsort.py b/src/stubber/rst/classsort.py index 22dfe9897..2b4f23c83 100644 --- a/src/stubber/rst/classsort.py +++ b/src/stubber/rst/classsort.py @@ -4,10 +4,11 @@ ref : https://stackoverflow.com/questions/34964878/python-generate-a-dictionarytree-from-a-list-of-tuples/35049729#35049729 with modification """ + import re from typing import List -from loguru import logger as log +from mpflash.logger import log __all__ = ["sort_classes"] RE_CLASS = re.compile(r"class\s+(?P\w+)(\((?P\w*)\))?") diff --git a/src/stubber/rst/reader.py b/src/stubber/rst/reader.py index 6a012718b..602f06e62 100644 --- a/src/stubber/rst/reader.py +++ b/src/stubber/rst/reader.py @@ -67,7 +67,7 @@ from pathlib import Path from typing import List, Optional, Tuple -from loguru import logger as log +from mpflash.logger import log from mpflash.versions import V_PREVIEW from stubber.rst import ( @@ -149,7 +149,11 @@ def extend_and_balance_line(self) -> str: """ append = 0 newline = self.rst_text[self.line_no] - while not self.is_balanced(newline) and self.line_no >= 0 and (self.line_no + append + 1) <= self.max_line: + while ( + not self.is_balanced(newline) + and self.line_no >= 0 + and (self.line_no + append + 1) <= self.max_line + ): append += 1 # concat the lines newline += self.rst_text[self.line_no + append] @@ -220,7 +224,9 @@ def at_heading(self, large=False) -> bool: "stop at heading" u_line = self.rst_text[min(self.line_no + 1, self.max_line - 1)].rstrip() # Heading ---, ==, ~~~ - underlined = u_line.startswith("---") or u_line.startswith("===") or u_line.startswith("~~~") + underlined = ( + u_line.startswith("---") or u_line.startswith("===") or u_line.startswith("~~~") + ) if underlined and self.line_no > 0: # check if previous line is a heading line = self.rst_text[self.line_no].strip() @@ -367,9 +373,15 @@ class RSTParser(RSTReader): target = ".py" # py/pyi # TODO: Move to lookup.py PARAM_RE_FIXES = [ - Fix(r"\[angle, time=0\]", "[angle], time=0", is_re=True), # fix: method:: Servo.angle([angle, time=0]) - Fix(r"\[speed, time=0\]", "[speed], time=0", is_re=True), # fix: .. method:: Servo.speed([speed, time=0]) - Fix(r"\[service_id, key=None, \*, \.\.\.\]", "[service_id], [key], *, ...", is_re=True), # fix: network - AbstractNIC.connect + Fix( + r"\[angle, time=0\]", "[angle], time=0", is_re=True + ), # fix: method:: Servo.angle([angle, time=0]) + Fix( + r"\[speed, time=0\]", "[speed], time=0", is_re=True + ), # fix: .. method:: Servo.speed([speed, time=0]) + Fix( + r"\[service_id, key=None, \*, \.\.\.\]", "[service_id], [key], *, ...", is_re=True + ), # fix: network - AbstractNIC.connect ] def __init__(self, v_tag: str) -> None: @@ -433,7 +445,9 @@ def fix_parameters(self, params: str, name: str = "") -> str: def apply_fix(fix: Fix, params: str, name: str = ""): if fix.name and fix.name != name: return params - return re.sub(fix.from_, fix.to, params) if fix.is_re else params.replace(fix.from_, fix.to) + return ( + re.sub(fix.from_, fix.to, params) if fix.is_re else params.replace(fix.from_, fix.to) + ) def create_update_class(self, name: str, params: str, docstr: List[str]): # a bit of a hack: assume no classes in classes or functions in function @@ -494,8 +508,12 @@ def parse_module(self): if "nightly" in self.source_tag: version = V_PREVIEW else: - version = self.source_tag.replace("_", ".") # TODO Use clean_version(self.source_tag) - docstr[0] = f"{docstr[0]}.\n\nMicroPython module: https://docs.micropython.org/en/{version}/library/{module_name}.html" + version = self.source_tag.replace( + "_", "." + ) # TODO Use clean_version(self.source_tag) + docstr[0] = ( + f"{docstr[0]}.\n\nMicroPython module: https://docs.micropython.org/en/{version}/library/{module_name}.html" + ) self.output_dict.name = module_name self.output_dict.add_comment(f"# source version: {self.source_tag}") @@ -527,7 +545,9 @@ def parse_function(self): for this_function in function_names: # Parse return type from docstring - ret_type = return_type_from_context(docstring=docstr, signature=this_function, module=self.current_module) + ret_type = return_type_from_context( + docstring=docstr, signature=this_function, module=self.current_module + ) # defaults name = params = "" @@ -633,7 +653,9 @@ def parse_method(self): params = self.fix_parameters(params, f"{class_name}.{name}") # parse return type from docstring - ret_type = return_type_from_context(docstring=docstr, signature=f"{class_name}.{name}", module=self.current_module) + ret_type = return_type_from_context( + docstring=docstr, signature=f"{class_name}.{name}", module=self.current_module + ) # methods have 4 flavours # - __init__ (self, ) -> None: # - classmethod (cls, ) -> : @@ -753,7 +775,9 @@ def parse_data(self): # deal with documentation wildcards for name in names: - r_type = return_type_from_context(docstring=docstr, signature=name, module=self.current_module, literal=True) + r_type = return_type_from_context( + docstring=docstr, signature=name, module=self.current_module, literal=True + ) if r_type in ["None"]: # None does not make sense r_type = "Incomplete" # Default to Incomplete/ Unknown / int name = self.strip_prefixes(name) diff --git a/src/stubber/rst/report_return.py b/src/stubber/rst/report_return.py index 95042297b..61eb2834d 100644 --- a/src/stubber/rst/report_return.py +++ b/src/stubber/rst/report_return.py @@ -15,7 +15,7 @@ from typing import List, Tuple import stubber.utils as utils -from loguru import logger as log +from mpflash.logger import log def process(folder: Path, pattern: str): @@ -34,7 +34,9 @@ def process(folder: Path, pattern: str): signature = str(item[3]).split("::")[-1].strip() docstring = item[4] - r = utils._type_from_context(docstring=docstring, signature=signature, module=module_name) + r = utils._type_from_context( + docstring=docstring, signature=signature, module=module_name + ) report.append( { "signature": signature, @@ -49,11 +51,17 @@ def process(folder: Path, pattern: str): } ) # isGood = r["confidence"] >= 0.5 and r["confidence"] <= 0.8 and item[2] != "" - isBad = float(r["confidence"]) <= 0.5 and float(r["confidence"]) <= 0.8 and item[2] != "" + isBad = ( + float(r["confidence"]) <= 0.5 + and float(r["confidence"]) <= 0.8 + and item[2] != "" + ) if isBad: context = item[3] + ".".join((item[0], item[1], item[2])) try: - log.debug(f"{context:40} {r['type']:<15} - {r['confidence']} {r['match'].groups('return')}") + log.debug( + f"{context:40} {r['type']:<15} - {r['confidence']} {r['match'].groups('return')}" + ) except Exception: log.debug(f"{context:40} {r['type']:<15} - {r['confidence']} ") diff --git a/src/stubber/rst/rst_utils.py b/src/stubber/rst/rst_utils.py index 0f8a44342..3ac1b75e3 100644 --- a/src/stubber/rst/rst_utils.py +++ b/src/stubber/rst/rst_utils.py @@ -32,13 +32,14 @@ https://docs.microsoft.com/en-us/azure/machine-learning/quickstart-create-resources - """ + # ref: https://regex101.com/codegen?language=python # https://regex101.com/r/Ni8g2z/2 import re from typing import Dict, List, Optional, Union -from loguru import logger as log +from mpflash.logger import log from .lookup import LOOKUP_LIST, NONE_VERBS, TYPING_IMPORT diff --git a/src/stubber/stubs_from_docs.py b/src/stubber/stubs_from_docs.py index c4fd12fec..0624facae 100644 --- a/src/stubber/stubs_from_docs.py +++ b/src/stubber/stubs_from_docs.py @@ -7,7 +7,7 @@ from pathlib import Path from typing import List, Optional -from loguru import logger as log +from mpflash.logger import log from stubber import utils from stubber.rst import DOCSTUB_SKIP, U_MODULES diff --git a/src/stubber/update_fallback.py b/src/stubber/update_fallback.py index 6e6136c3b..0944047a7 100644 --- a/src/stubber/update_fallback.py +++ b/src/stubber/update_fallback.py @@ -7,7 +7,7 @@ # from pathlib import Path # from typing import List, Optional, Tuple -# from loguru import logger as log +# from mpflash.logger import log # from mpflash.versions import V_PREVIEW diff --git a/src/stubber/update_module_list.py b/src/stubber/update_module_list.py index 063ed0627..c7cef5c0d 100644 --- a/src/stubber/update_module_list.py +++ b/src/stubber/update_module_list.py @@ -15,7 +15,7 @@ from pathlib import Path from typing import List, Optional, Set -from loguru import logger as log +from mpflash.logger import log def read_modules(path: Optional[Path] = None) -> Set[str]: diff --git a/src/stubber/utils/config.py b/src/stubber/utils/config.py index 02715926e..1286aa3b9 100644 --- a/src/stubber/utils/config.py +++ b/src/stubber/utils/config.py @@ -3,7 +3,7 @@ from pathlib import Path from typing import List -from loguru import logger as log +from mpflash.logger import log from typedconfig.config import Config, key, section from typedconfig.source import EnvironmentConfigSource @@ -17,7 +17,9 @@ class StubberConfig(Config): "stubber configuration class" # relative to stubs folder - fallback_path: Path = key(key_name="fallback-path", cast=Path, required=False, default=Path("typings/fallback")) + fallback_path: Path = key( + key_name="fallback-path", cast=Path, required=False, default=Path("typings/fallback") + ) "a Path to the fallback stubs directory" # ------------------------------------------------------------------------------------------ @@ -25,16 +27,24 @@ class StubberConfig(Config): repo_path: Path = key(key_name="repo-path", cast=Path, required=False, default=Path("./repos")) "a Path to the repo directory" - mpy_path: Path = key(key_name="mpy-path", cast=Path, required=False, default=Path("micropython")) + mpy_path: Path = key( + key_name="mpy-path", cast=Path, required=False, default=Path("micropython") + ) "a Path to the micropython folder in the repos directory" - mpy_lib_path: Path = key(key_name="mpy-lib-path", cast=Path, required=False, default=Path("micropython-lib")) + mpy_lib_path: Path = key( + key_name="mpy-lib-path", cast=Path, required=False, default=Path("micropython-lib") + ) "a Path to the micropython-lib folder in the repos directory" - mpy_stubs_path: Path = key(key_name="mpy-stubs-path", cast=Path, required=False, default=Path("micropython-stubs")) + mpy_stubs_path: Path = key( + key_name="mpy-stubs-path", cast=Path, required=False, default=Path("micropython-stubs") + ) "a Path to the micropython-stubs folder in the repos directory (or current directory)" - stable_version: str = key(key_name="stable-version", cast=str, required=False, default="1.20.0") + stable_version: str = key( + key_name="stable-version", cast=str, required=False, default="1.20.0" + ) "last published stable" @@ -113,7 +123,9 @@ def readconfig(filename: str = "pyproject.toml", prefix: str = "tool.", must_exi # add provider sources to the config config.add_source(EnvironmentConfigSource()) if use_toml: - config.add_source(TomlConfigSource(filename, prefix=prefix, must_exist=must_exist)) # ,"tools.micropython-stubber")) + config.add_source( + TomlConfigSource(filename, prefix=prefix, must_exist=must_exist) + ) # ,"tools.micropython-stubber")) config.read() return config diff --git a/src/stubber/utils/post.py b/src/stubber/utils/post.py index 29e007996..5ad24fd3a 100644 --- a/src/stubber/utils/post.py +++ b/src/stubber/utils/post.py @@ -1,11 +1,12 @@ """Pre/Post Processing for createstubs.py""" + import subprocess import sys from pathlib import Path from typing import List import autoflake -from loguru import logger as log +from mpflash.logger import log from .stubmaker import generate_pyi_files diff --git a/src/stubber/utils/repos.py b/src/stubber/utils/repos.py index a82a6536b..8a0995be4 100644 --- a/src/stubber/utils/repos.py +++ b/src/stubber/utils/repos.py @@ -8,7 +8,7 @@ from pathlib import Path from typing import Tuple -from loguru import logger as log +from mpflash.logger import log from packaging.version import Version import mpflash.basicgit as git @@ -63,7 +63,11 @@ def read_micropython_lib_commits(filename: str = "data/micropython_tags.csv"): reader = csv.DictReader(ntf.file, skipinitialspace=True) # dialect="excel", rows = list(reader) # create a dict version --> commit_hash - version_commit = {row["version"].split("/")[-1]: row["lib_commit_hash"] for row in rows if row["version"].startswith("refs/tags/")} + version_commit = { + row["version"].split("/")[-1]: row["lib_commit_hash"] + for row in rows + if row["version"].startswith("refs/tags/") + } # add default version_commit = defaultdict(lambda: "master", version_commit) return version_commit @@ -91,7 +95,9 @@ def match_lib_with_mpy(version_tag: str, mpy_path: Path, lib_path: Path) -> bool return False return git.sync_submodules(mpy_path) else: - log.info(f"Matching repo's: Micropython {version_tag} needs micropython-lib:{micropython_lib_commits[version_tag]}") + log.info( + f"Matching repo's: Micropython {version_tag} needs micropython-lib:{micropython_lib_commits[version_tag]}" + ) return git.checkout_commit(micropython_lib_commits[version_tag], lib_path) diff --git a/src/stubber/utils/stubmaker.py b/src/stubber/utils/stubmaker.py index 3e302b20f..25b35648a 100644 --- a/src/stubber/utils/stubmaker.py +++ b/src/stubber/utils/stubmaker.py @@ -5,7 +5,7 @@ from pathlib import Path import mypy.stubgen as stubgen -from loguru import logger as log +from mpflash.logger import log from mypy.errors import CompileError # default stubgen options diff --git a/src/stubber/utils/typed_config_toml.py b/src/stubber/utils/typed_config_toml.py index 7078d68b0..f7a0e62e3 100644 --- a/src/stubber/utils/typed_config_toml.py +++ b/src/stubber/utils/typed_config_toml.py @@ -4,6 +4,7 @@ Extend typed-config to read configuration from .toml files """ + # TODO : extend support for . notation in section names from pathlib import Path @@ -15,7 +16,7 @@ import tomli as tomllib # type: ignore assert tomllib -from loguru import logger as log +from mpflash.logger import log from typedconfig.source import ConfigSource @@ -69,7 +70,9 @@ def __init__(self, filename: str, prefix: Optional[str] = None, must_exist: bool # do not assume/require that all values are strings # assert isinstance(v_v, str) # Convert all keys to lowercase - self.data = {k.lower(): {v_k.lower(): v_v for v_k, v_v in v.items()} for k, v in self.data.items()} + self.data = { + k.lower(): {v_k.lower(): v_v for v_k, v_v in v.items()} for k, v in self.data.items() + } def get_config_value(self, section_name: str, key_name: str) -> Optional[str]: # Extract info from data which we read in during __init__ diff --git a/src/stubber/variants.py b/src/stubber/variants.py index 36dbdc933..33097755f 100644 --- a/src/stubber/variants.py +++ b/src/stubber/variants.py @@ -10,7 +10,7 @@ import libcst as cst import libcst.codemod as codemod -from loguru import logger as log +from mpflash.logger import log from stubber.codemod.board import CreateStubsCodemod, CreateStubsVariant from stubber.codemod.modify_list import ListChangeSet # type: ignore