From 8e14fa691158a4c537cd89d39db1b92cd208bc42 Mon Sep 17 00:00:00 2001 From: Braden Mars Date: Mon, 27 Mar 2023 04:28:11 -0500 Subject: [PATCH] feat(cli): remove old cli module. Signed-off-by: Braden Mars --- micropy/cli.py | 318 ------------------------------------------------- 1 file changed, 318 deletions(-) delete mode 100644 micropy/cli.py diff --git a/micropy/cli.py b/micropy/cli.py deleted file mode 100644 index 94ee1460..00000000 --- a/micropy/cli.py +++ /dev/null @@ -1,318 +0,0 @@ -#!/usr/bin/env python - -"""MicropyCli Console Entrypoint.""" -from __future__ import annotations - -import sys -from pathlib import Path -from typing import TYPE_CHECKING - -import click -import micropy.exceptions as exc -import questionary as prompt -from micropy import __version__ as micropy_version -from micropy import main, utils -from micropy.logger import Log -from micropy.project import Project, modules -from micropy.stubs import source as stubs_source -from questionary import Choice - -if TYPE_CHECKING: - pass - -pass_mpy = click.make_pass_decorator(main.MicroPy, ensure=True) - - -@click.group(invoke_without_command=True) -@click.version_option(version=micropy_version) -@click.option( - "--skip-checks", - "-s", - is_flag=True, - default=False, - help="Skip Project Checks. Defaults to False.", -) -@pass_mpy -@click.pass_context -def cli(ctx, mpy, skip_checks=False): - """CLI Application for creating/managing Micropython Projects.""" - if ctx.invoked_subcommand is None: - if not mpy.project.exists: - return click.echo(ctx.get_help()) - latest = utils.is_update_available() - if latest: - log = Log.get_logger("MicroPy") - log.title("Update Available!") - log.info(f"Version $B[v{latest}] is now available") - log.info("You can update via: $[pip install --upgrade micropy-cli]\n") - mpy.RUN_CHECKS = not skip_checks - - -@cli.group(short_help="Manage Micropy Stubs") -def stubs(): - """Manage Micropy Stubs. - - \b - Stub files are what enable linting, - Intellisense, Autocompletion, and more. - - \b - To achieve the best results, you can install - stubs specific to your device/firmware using: - - micropy stubs add - - For more info, please check micropy stubs add --help - - """ - - -@cli.command(short_help="Create new Micropython Project") -@click.argument("path", required=False, default=None) -@click.option( - "--name", "-n", required=False, default=None, help="Project Name. Defaults to Path name." -) -@click.option( - "--template", - "-t", - type=click.Choice(modules.TemplatesModule.TEMPLATES.keys()), - multiple=True, - required=False, - help=("Templates to generate for project." " Multiple options can be passed."), -) -@pass_mpy -def init(mpy, path, name=None, template=None): - """Create new Micropython Project. - - \b When creating a new project, all files will be placed under the - generated folder. - - """ - mpy.log.title("Creating New Project") - if not path: - path = Path.cwd() - default_name = path.name - prompt_name = prompt.text("Project Name", default=default_name).ask() - name = prompt_name.strip() - if not template: - templates = modules.TemplatesModule.TEMPLATES.items() - templ_choices = [Choice(str(val[1]), value=t) for t, val in templates] - template = prompt.checkbox("Choose any Templates to Generate", choices=templ_choices).ask() - stubs = [Choice(str(s), value=s) for s in mpy.stubs] - if not stubs: - mpy.log.error("You don't have any stubs!") - mpy.log.title("To add stubs to micropy, use $[micropy stubs add ]") - sys.exit(1) - stub_choices = prompt.checkbox("Which stubs would you like to use?", choices=stubs).ask() - project = Project(path, name=name) - project.add(modules.StubsModule, mpy.stubs, stubs=stub_choices) - project.add(modules.PackagesModule, "requirements.txt") - project.add(modules.DevPackagesModule, "dev-requirements.txt") - project.add(modules.TemplatesModule, templates=template, run_checks=mpy.RUN_CHECKS) - proj_path = project.create() - try: - rel_path = f"./{proj_path.relative_to(Path.cwd())}" - except ValueError: - rel_path = proj_path - mpy.log.title(f"Created $w[{project.name}] at $w[{rel_path}]") - - -@cli.command(short_help="Install Project Requirements") -@click.argument("packages", nargs=-1) -@click.option( - "-d", "--dev", is_flag=True, default=False, help=("Add dependency to dev requirements") -) -@click.option( - "-p", - "--path", - type=click.Path(exists=True, path_type=str), - help=("Add dependency from local path. Can be a file or directory."), -) -@pass_mpy -def install(mpy, packages, dev=False, path=None): - """Install Packages as Project Requirements. - - \b - Install a project dependency while enabling - intellisense, autocompletion, and linting for it. - - \b - If no packages are passed and a requirements.txt file is found, - then micropy will install all packages listed in it. - - \b - If the --dev flag is passed, then the packages are only - added to micropy.json. They are not stubbed. - - \b - To add a dependency from a path, use the --path option - and provide a name for your package: - \b - $ micropy install --path ./src/lib/mypackage MyCustomPackage - - - \b - You can import installed packages just as you would - on your actual device: - \b - >>> # main.py - >>> import - - """ - project = mpy.project - if not project.exists: - mpy.log.error("You are not currently in an active project!") - sys.exit(1) - if path: - pkg_name = next(iter(packages), None) - mpy.log.title("Installing Local Package") - pkg_path = "-e " + path - project.add_package(pkg_path, dev=dev, name=pkg_name) - sys.exit(0) - if not packages: - mpy.log.title("Installing all Requirements") - try: - project.add_from_file(dev=dev) - except Exception as e: - mpy.error("Failed to load requirements!", exception=e) - raise click.Abort() from e - else: - mpy.log.success("\nRequirements Installed!") - sys.exit(0) - mpy.log.title("Installing Packages") - for pkg in packages: - try: - project.add_package(pkg, dev=dev) - except exc.RequirementException as e: - pkg_name = str(e.package) - mpy.log.error( - (f"Failed to install {pkg_name}!" " Is it available on PyPi?"), exception=e - ) - raise click.Abort() from e - - -@stubs.command(short_help="Add Stubs from package or path") -@click.argument("stub_name", required=True) -@click.option("-f", "--force", is_flag=True, default=False, help="Overwrite Stub if it exists.") -@pass_mpy -def add(mpy: main.MicroPy, stub_name, force=False): - """Add Stubs from package or path. - - \b - In general, stub package names follow this schema: - -- - - \b - For example: - esp32-micropython-1.11.0 - - \b - You can search premade stub packages using: - micropy stubs search - - Checkout the docs on Github for more info. - - """ - proj = mpy.project - mpy.log.title(f"Adding $[{stub_name}] to stubs") - locator = stubs_source.StubSource( - [stubs_source.RepoStubLocator(mpy.repo), stubs_source.StubInfoSpecLocator()] - ) - with locator.ready(stub_name) as stub: - stub_name = stub - try: - stub = mpy.stubs.add(stub_name, force=force) - except exc.StubNotFound: - mpy.log.error(f"$[{stub_name}] could not be found!") - sys.exit(1) - except exc.StubError: - mpy.log.error(f"$[{stub_name}] is not a valid stub!") - sys.exit(1) - else: - mpy.log.success(f"{stub.name} added!") - if proj.exists: - mpy.log.title(f"Adding $[{stub.name}] to $[{proj.name}]") - proj.add_stub(stub) - - -@stubs.command() -@click.argument("query", required=True) -@click.option( - "-s", "--show-outdated", is_flag=True, help="Include outdated versions in search results." -) -@pass_mpy -def search(mpy: main.MicroPy, query: str, show_outdated: bool = False): - """Search available Stubs.""" - mpy.log.title("Searching Stub Repositories...") - installed_stubs = map(str, mpy.stubs._loaded | mpy.stubs._firmware) - results = [ - (r, r.name in installed_stubs) - for r in mpy.repo.search(query, include_versions=show_outdated) - ] - results = sorted(results, key=lambda pkg: pkg[0].name) - if not any(results): - mpy.log.warn(f"No results found for: $[{query}].") - sys.exit(0) - mpy.log.title(f"Results for $[{query}]:") - max_name = max(len(n[0].repo_name) for n in results) - for pkg, installed in results: - pad = max_name - len(pkg.repo_name) + 2 - pad = pad if (pad % 2 == 0) else pad + 1 - spacer = "{:>{pad}}".format("::", pad=pad) - repo_logger = Log.add_logger(f"{pkg.repo_name} {spacer}", "bright_white") - name = "{:>{pad}}".format(f"{pkg.name} ($w[{pkg.version}])", pad=pad) - name = f"{name} $B[(Installed)]" if installed else name - repo_logger.info(name) - - -@stubs.command() -@pass_mpy -def list(mpy): - """List installed stubs.""" - - def print_stubs(stub_list): - for firm, stubs in stub_list: - if stubs: - title = str(firm).capitalize() - mpy.log.title(f"$[{title}]:") - for stub in stubs: - mpy.log.info(str(stub)) - - mpy.log.title("Installed Stubs:") - mpy.log.info(f"Total: {len(mpy.stubs)}") - print_stubs(mpy.stubs.iter_by_firmware()) - mpy.verbose = False - proj = mpy.project - if proj.exists: - mpy.log.title(f"Stubs used in {proj.name}:") - mpy.log.info(f"Total: {len(proj.stubs)}") - stubs = mpy.stubs.iter_by_firmware(stubs=proj.stubs) - print_stubs(stubs) - - -@stubs.command(short_help="Create Stubs from Pyboard") -@click.argument("port", required=True) -@click.option("-v", "--verbose", is_flag=True, default=False, help="Enable verbose output") -@click.option( - "-b", - "--backend", - type=click.Choice(["upydevice", "rshell"]), - default="upydevice", - help="PyDevice backend.", - show_default=True, -) -@pass_mpy -def create(mpy, port, verbose=False, backend="upydevice"): - """Create stubs from a pyboard at - - \b - MicropyCli uses Josverl's micropython-stubber for stub creation. - For more information, please visit the repository - at: https://github.com/Josverl/micropython-stubber - - """ - return mpy.create_stubs(port, verbose=verbose, backend=backend) - - -if __name__ == "__main__": - cli()