Skip to content

Commit

Permalink
Tidied up cli (#9)
Browse files Browse the repository at this point in the history
  • Loading branch information
abrahammurciano authored Aug 15, 2022
1 parent d5de9d6 commit 83cc20f
Show file tree
Hide file tree
Showing 11 changed files with 156 additions and 159 deletions.
102 changes: 2 additions & 100 deletions condax/cli/__init__.py
Original file line number Diff line number Diff line change
@@ -1,72 +1,9 @@
import logging
from statistics import median
import rainbowlog
from pathlib import Path
from typing import Callable, Optional

import click

import condax.config as config
from condax import __version__


option_config = click.option(
"--config",
"config_file",
type=click.Path(exists=True, path_type=Path),
help=f"Custom path to a condax config file in YAML. Default: {config.DEFAULT_CONFIG}",
callback=lambda _, __, f: (f and config.set_via_file(f)) or f,
)

option_channels = click.option(
"--channel",
"-c",
"channels",
multiple=True,
help=f"""Use the channels specified to install. If not specified condax will
default to using {config.DEFAULT_CHANNELS}, or 'channels' in the config file.""",
callback=lambda _, __, c: (c and config.set_via_value(channels=c)) or c,
)

option_envname = click.option(
"--name",
"-n",
"envname",
required=True,
prompt="Specify the environment (Run `condax list --short` to see available ones)",
type=str,
help=f"""Specify existing environment to inject into.""",
callback=lambda _, __, n: n.strip(),
)

option_is_forcing = click.option(
"-f",
"--force",
"is_forcing",
help="""Modify existing environment and files in CONDAX_BIN_DIR.""",
is_flag=True,
default=False,
)


def options_logging(f: Callable) -> Callable:
option_verbose = click.option(
"-v",
"--verbose",
count=True,
help="Raise verbosity level.",
callback=lambda _, __, v: _LoggerSetup.set_verbose(v),
)
option_quiet = click.option(
"-q",
"--quiet",
count=True,
help="Decrease verbosity level.",
callback=lambda _, __, q: _LoggerSetup.set_quiet(q),
)
return option_verbose(option_quiet(f))


@click.group(
help=f"""Install and execute applications packaged by conda.
Expand All @@ -80,41 +17,6 @@ def options_logging(f: Callable) -> Callable:
__version__,
message="%(prog)s %(version)s",
)
@option_config
@options_logging
@click.help_option("-h", "--help")
def cli(**_):
"""Main entry point for condax."""
pass


class _LoggerSetup:
handler = logging.StreamHandler()
formatter = rainbowlog.Formatter(logging.Formatter())
logger = logging.getLogger((__package__ or __name__).split(".", 1)[0])
verbose = 0
quiet = 0

@classmethod
def setup(cls) -> int:
"""Setup the logger.
Returns:
int: The log level.
"""
cls.handler.setFormatter(cls.formatter)
cls.logger.addHandler(cls.handler)
level = logging.INFO - 10 * (int(median((-1, 3, cls.verbose - cls.quiet))))
cls.logger.setLevel(level)
return level

@classmethod
def set_verbose(cls, v: int) -> int:
"""Set the verbose level and return the new log level."""
cls.verbose += v
return cls.setup()

@classmethod
def set_quiet(cls, q: int):
"""Set the quiet level and return the new log level."""
cls.quiet += q
return cls.setup()
return
8 changes: 2 additions & 6 deletions condax/cli/ensure_path.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,15 @@
from pathlib import Path
from typing import Optional

import condax.config as config
import condax.paths as paths
from condax import __version__

from . import cli, option_config, options_logging
from . import cli, options


@cli.command(
help="""
Ensure the condax links directory is on $PATH.
"""
)
@option_config
@options_logging
@options.common
def ensure_path(**_):
paths.add_path_to_environment(config.C.bin_dir())
16 changes: 8 additions & 8 deletions condax/cli/export.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import condax.core as core
from condax import __version__

from . import cli, option_is_forcing, options_logging
from . import cli, options


@cli.command(
Expand All @@ -17,9 +17,9 @@
default="condax_exported",
help="Set directory to export to.",
)
@options_logging
def export(dir: str, verbose: int, **_):
core.export_all_environments(dir, conda_stdout=verbose <= logging.INFO)
@options.common
def export(dir: str, log_level: int, **_):
core.export_all_environments(dir, conda_stdout=log_level <= logging.INFO)


@cli.command(
Expand All @@ -28,14 +28,14 @@ def export(dir: str, verbose: int, **_):
[experimental] Import condax environments.
""",
)
@option_is_forcing
@options_logging
@options.is_forcing
@options.common
@click.argument(
"directory",
required=True,
type=click.Path(exists=True, dir_okay=True, file_okay=False),
)
def run_import(directory: str, is_forcing: bool, verbose: int, **_):
def run_import(directory: str, is_forcing: bool, log_level: int, **_):
core.import_environments(
directory, is_forcing, conda_stdout=verbose <= logging.INFO
directory, is_forcing, conda_stdout=log_level <= logging.INFO
)
27 changes: 13 additions & 14 deletions condax/cli/inject.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,43 +2,42 @@
from typing import List
import click

import condax.config as config
import condax.core as core
from condax import __version__

from . import cli, option_channels, option_envname, option_is_forcing, options_logging
from . import cli, options


@cli.command(
help="""
Inject a package to existing environment created by condax.
"""
)
@option_channels
@option_envname
@option_is_forcing
@options.channels
@options.envname
@options.is_forcing
@click.option(
"--include-apps",
help="""Make apps from the injected package available.""",
is_flag=True,
default=False,
)
@options_logging
@click.argument("packages", nargs=-1, required=True)
@options.common
@options.packages
def inject(
packages: List[str],
envname: str,
is_forcing: bool,
include_apps: bool,
verbose: int,
log_level: int,
**_,
):
core.inject_package_to(
envname,
packages,
is_forcing=is_forcing,
include_apps=include_apps,
conda_stdout=verbose <= logging.INFO,
conda_stdout=log_level <= logging.INFO,
)


Expand All @@ -47,8 +46,8 @@ def inject(
Uninject a package from an existing environment.
"""
)
@option_envname
@options_logging
@click.argument("packages", nargs=-1, required=True)
def uninject(packages: List[str], envname: str, verbose: int, **_):
core.uninject_package_from(envname, packages, verbose <= logging.INFO)
@options.envname
@options.common
@options.packages
def uninject(packages: List[str], envname: str, log_level: int, **_):
core.uninject_package_from(envname, packages, log_level <= logging.INFO)
22 changes: 7 additions & 15 deletions condax/cli/install.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,7 @@
import condax.core as core
from condax import __version__

from . import (
cli,
option_config,
option_channels,
option_is_forcing,
option_channels,
options_logging,
)
from . import cli, options


@cli.command(
Expand All @@ -25,18 +18,17 @@
provided by it to `{config.DEFAULT_BIN_DIR}`.
"""
)
@option_channels
@option_config
@option_is_forcing
@options_logging
@click.argument("packages", nargs=-1)
@options.channels
@options.is_forcing
@options.common
@options.packages
def install(
packages: List[str],
is_forcing: bool,
verbose: int,
log_level: int,
**_,
):
for pkg in packages:
core.install_package(
pkg, is_forcing=is_forcing, conda_stdout=verbose <= logging.INFO
pkg, is_forcing=is_forcing, conda_stdout=log_level <= logging.INFO
)
4 changes: 2 additions & 2 deletions condax/cli/list.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import condax.core as core
from condax import __version__

from . import cli, options_logging
from . import cli, options


@cli.command(
Expand All @@ -27,6 +27,6 @@
default=False,
help="Show packages injected into the main app's environment.",
)
@options_logging
@options.common
def run_list(short: bool, include_injected: bool, **_):
core.list_all_packages(short=short, include_injected=include_injected)
Loading

0 comments on commit 83cc20f

Please sign in to comment.