Skip to content

cli

cli

Module contains implementation of CSSFinder command line interface.

Ctx dataclass

Command line context wrapper class.

Source code in cssfinder/cli.py
@dataclass
class Ctx:
    """Command line context wrapper class."""

    is_debug: bool = False
    is_rich: bool = True
    project_path: Path | None = None

main

main(
    ctx: click.Context,
    verbose: int,
    seed: Optional[int],
    numpy_thread_count: int,
    *,
    debug: bool,
    is_rich: bool,
    perf_log: bool
) -> None

CSSFinder is a script for finding closest separable states.

Source code in cssfinder/cli.py
@click.group(invoke_without_command=True, no_args_is_help=True)
@click.pass_context
@click.version_option(cssfinder.__version__, "-V", "--version", prog_name="cssfinder")
@click.option(
    "-v",
    "--verbose",
    default=0,
    count=True,
    help="Control verbosity of logging, by default+ critical only, use "
    "-v, -vv, -vvv to gradually increase it.",
)
@click.option(
    "--numpy-thread-count",
    type=int,
    default=1,
    required=False,
    help="NumPy thread count override. Use '-1' to disable override and use defaults.",
)
@click.option(
    "--seed",
    type=int,
    default=None,
    required=False,
    help="NumPy random generator seed override.",
)
@click.option("--debug", is_flag=True, default=False)
@click.option("--rich", "--no-rich", "is_rich", is_flag=True, default=True)
@click.option("--perf-log", is_flag=True, default=False)
def main(
    ctx: click.Context,
    verbose: int,
    seed: Optional[int],
    numpy_thread_count: int,
    *,
    debug: bool,
    is_rich: bool,
    perf_log: bool,
) -> None:
    """CSSFinder is a script for finding closest separable states."""
    import os
    from pprint import pformat

    import pendulum
    import rich
    from threadpoolctl import threadpool_info

    if numpy_thread_count != -1:
        numpy_thread_count_str = str(numpy_thread_count)

        os.environ["OMP_NUM_THREADS"] = numpy_thread_count_str
        os.environ["OPENBLAS_NUM_THREADS"] = numpy_thread_count_str
        os.environ["MKL_NUM_THREADS"] = numpy_thread_count_str
        os.environ["VECLIB_MAXIMUM_THREADS"] = numpy_thread_count_str
        os.environ["NUMEXPR_NUM_THREADS"] = numpy_thread_count_str

    import numpy as np

    from cssfinder.log import configure_logger, enable_performance_logging

    configure_logger(verbosity=verbose, logger_name="cssfinder", use_rich=is_rich)
    ctx.obj = Ctx(is_debug=debug, is_rich=is_rich)

    if seed is not None:
        logging.debug("NumPy random number generator seed set to %d", seed)
        np.random.seed(seed)  # noqa: NPY002

    logging.debug("\n%s", pformat(threadpool_info(), indent=4))

    logging.getLogger("numba").setLevel(logging.ERROR)
    logging.info("CSSFinder started at %s", pendulum.now().isoformat(sep=" "))

    if perf_log:
        enable_performance_logging()

    if verbose >= VERBOSITY_INFO:
        rich.print(
            f"""{'[blue]' if is_rich else ''}
  ██████╗███████╗███████╗███████╗██╗███╗   ██╗██████╗ ███████╗██████╗
 ██╔════╝██╔════╝██╔════╝██╔════╝██║████╗  ██║██╔══██╗██╔════╝██╔══██╗
 ██║     ███████╗███████╗█████╗  ██║██╔██╗ ██║██║  ██║█████╗  ██████╔╝
 ██║     ╚════██║╚════██║██╔══╝  ██║██║╚██╗██║██║  ██║██╔══╝  ██╔══██╗
 ╚██████╗███████║███████║██║     ██║██║ ╚████║██████╔╝███████╗██║  ██║
  ╚═════╝╚══════╝╚══════╝╚═╝     ╚═╝╚═╝  ╚═══╝╚═════╝ ╚══════╝╚═╝  ╚═╝
""",
        )

tree

tree(ctx: click.Context) -> None

Show the command tree of your CLI.

Source code in cssfinder/cli.py
@main.command(name="show-command-tree")
@click.pass_context
def tree(ctx: click.Context) -> None:
    """Show the command tree of your CLI."""
    root_cmd = _build_command_tree(ctx.find_root().command)
    _print_tree(root_cmd)

validate_mutually_exclusive

validate_mutually_exclusive(
    this: str, other: str
) -> Callable[
    [click.Context, dict[str, str], str], Optional[str]
]

Return callback checking for mutually exclusive options.

Source code in cssfinder/cli.py
def validate_mutually_exclusive(
    this: str,
    other: str,
) -> Callable[[click.Context, dict[str, str], str], Optional[str]]:
    """Return callback checking for mutually exclusive options."""

    def _(
        ctx: click.Context,
        param: dict[str, str],  # noqa: ARG001
        value: Optional[str],
    ) -> Optional[str]:
        if value is not None and ctx.params.get(other) is not None:
            msg = f"{this!r} and {other!r} options are mutually exclusive."
            raise click.BadParameter(msg)

        return value

    return _