Repository: psf/black
File: src/black_primer/cli.py (Python)

Code before:
    param: Optional[Union[click.core.Option, click.core.Parameter]],
    debug: Union[bool, int, str],
) -> Union[bool, int, str]:
    """Turn on debugging if asked otherwise INFO default"""
    log_level = logging.DEBUG if debug else logging.INFO
    logging.basicConfig(
        format="[%(asctime)s] %(levelname)s: %(message)s (%(filename)s:%(lineno)d)",
        level=log_level,
    )
    return debug


def _projects_callback(
    ctx: click.core.Context,
    param: Optional[Union[click.core.Option, click.core.Parameter]],
    projects: str,
) -> List[str]:
    requested_projects = set(projects.split(","))

    if str(DEFAULT_CONFIG) == ctx.params["config"]:
        available_projects = set(DEFAULT_PROJECTS)
    else:
        available_projects = set(
            json.load(open(ctx.params["config"]))["projects"].keys()
        )

    unavailable = requested_projects - available_projects
    if unavailable:
        LOG.error(f"Projects not found: {unavailable}. Available: {available_projects}")

    return sorted(requested_projects & available_projects)


async def async_main(
    config: str,
    debug: bool,
    keep: bool,
    long_checkouts: bool,
    no_diff: bool,
    projects: List[str],
    rebase: bool,
    workdir: str,
    workers: int,
) -> int:
    work_path = Path(workdir)
    if not work_path.exists():
        LOG.debug(f"Creating {work_path}")
        work_path.mkdir()

    if not which("black"):
        LOG.error("Can not find 'black' executable in PATH. No point in running")

Review comment (suggestion):
Might want to strip the names in case they are separated by ` ,` (space and comma).

(And you can resolve the conservation, you have applied in my previous review)

Code after:
) -> Union[bool, int, str]:
    """Turn on debugging if asked otherwise INFO default"""
    log_level = logging.DEBUG if debug else logging.INFO
    logging.basicConfig(
        format="[%(asctime)s] %(levelname)s: %(message)s (%(filename)s:%(lineno)d)",
        level=log_level,
    )
    return debug


def load_projects(config_path: Path) -> List[str]:
    with open(config_path) as config:
        return sorted(json.load(config)["projects"].keys())


# Unfortunately does import time file IO - but appears to be the only
# way to get `black-primer --help` to show projects list
DEFAULT_PROJECTS = load_projects(DEFAULT_CONFIG)


def _projects_callback(
    ctx: click.core.Context,
    param: Optional[Union[click.core.Option, click.core.Parameter]],
    projects: str,
) -> List[str]:
    requested_projects = set(projects.split(","))
    available_projects = set(
        DEFAULT_PROJECTS
        if str(DEFAULT_CONFIG) == ctx.params["config"]
        else load_projects(ctx.params["config"])
    )

    unavailable = requested_projects - available_projects
    if unavailable:
        LOG.error(f"Projects not found: {unavailable}. Available: {available_projects}")

    return sorted(requested_projects & available_projects)


async def async_main(
    config: str,
    debug: bool,
    keep: bool,
    long_checkouts: bool,
    no_diff: bool,
    projects: List[str],
    rebase: bool,
    workdir: str,
    workers: int,
) -> int:
    work_path = Path(workdir)