예제 #1
0
def main(
    ctx: click.Context,
    code: Optional[str],
    line_length: int,
    target_version: List[TargetVersion],
    check: bool,
    diff: bool,
    fast: bool,
    pyi: bool,
    py36: bool,
    skip_string_normalization: bool,
    single_quotes: bool,
    quiet: bool,
    verbose: bool,
    include: str,
    exclude: str,
    src: Tuple[str],
    config: Optional[str],
) -> None:
    """The uncompromising code formatter."""
    write_back = WriteBack.from_configuration(check=check, diff=diff)
    if target_version:
        if py36:
            err('Cannot use both --target-version and --py36')
            ctx.exit(2)
        else:
            versions = set(target_version)
    elif py36:
        err('--py36 is deprecated and will be removed in a future version. '
            'Use --target-version py36 instead.')
        versions = PY36_VERSIONS
    else:
        # We'll autodetect later.
        versions = set()
    mode = FileMode(
        target_versions=versions,
        line_length=line_length,
        is_pyi=pyi,
        string_normalization=not skip_string_normalization,
    )

    if single_quotes:
        black.normalize_string_quotes = patched_normalize_string_quotes

    if config and verbose:
        out(f'Using configuration from {config}.', bold=False, fg='blue')
    if code is not None:
        print(format_str(code, mode=mode))
        ctx.exit(0)
    try:
        include_regex = re_compile_maybe_verbose(include)
    except re.error:
        err(f'Invalid regular expression for include given: {include!r}')
        ctx.exit(2)
    try:
        exclude_regex = re_compile_maybe_verbose(exclude)
    except re.error:
        err(f'Invalid regular expression for exclude given: {exclude!r}')
        ctx.exit(2)
    report = Report(check=check, quiet=quiet, verbose=verbose)
    root = find_project_root(src)
    sources: Set[Path] = set()
    path_empty(src=src, quiet=quiet, verbose=verbose, ctx=ctx, msg=None)
    for s in src:
        p = Path(s)
        if p.is_dir():
            sources.update(
                gen_python_files_in_dir(
                    p,
                    root,
                    include_regex,
                    exclude_regex,
                    report,
                    get_gitignore(root),
                ))
        elif p.is_file() or s == '-':
            # if a file was explicitly given, we don't care about its extension
            sources.add(p)
        else:
            err(f'invalid path: {s}')
    if len(sources) == 0:
        if verbose or not quiet:
            out('No Python files are present to be formatted. Nothing to do 😴')
        ctx.exit(0)

    reformat_many(
        sources=sources,
        fast=fast,
        write_back=write_back,
        mode=mode,
        report=report,
    )

    if verbose or not quiet:
        out('Oh no! 💥 💔 💥' if report.return_code else 'All done! ✨ 🍰 ✨')
        click.secho(str(report), err=True)
    ctx.exit(report.return_code)
예제 #2
0
def cli(
    ctx: click.Context,
    line_length: int,
    check: bool,
    include: Pattern[str],
    exclude: Pattern[str],
    extend_exclude: Optional[Pattern[str]],
    force_exclude: Optional[Pattern[str]],
    stdin_filename: Optional[str],
    quiet: bool,
    verbose: bool,
    clear_output: bool,
    src: Tuple[str, ...],
    config: Optional[str],
) -> None:
    """
    The uncompromising code formatter, for Jupyter notebooks.
    """
    write_back = black.WriteBack.from_configuration(check=check, diff=False)
    mode = black.Mode(
        target_versions=TARGET_VERSIONS,
        line_length=line_length,
        is_pyi=False,
        string_normalization=True,
    )

    if config and verbose:
        black.out(f"Using configuration from {config}.", bold=False, fg="blue")

    report = black.Report(check=check, quiet=quiet, verbose=verbose)

    sources = black.get_sources(
        ctx=ctx,
        src=src,
        quiet=quiet,
        verbose=verbose,
        include=include,
        exclude=exclude,
        force_exclude=force_exclude,
        report=report,
        extend_exclude=extend_exclude,
        stdin_filename=stdin_filename,
    )

    black.path_empty(
        sources,
        "No Jupyter notebooks are present to be formatted. Nothing to do 😴",
        quiet,
        verbose,
        ctx,
    )

    for source in sources:
        reformat_one(
            src=source,
            write_back=write_back,
            mode=mode,
            clear_output=clear_output,
            report=report,
            quiet=quiet,
            verbose=verbose,
        )

    if verbose or not quiet:
        black.out("All done! ✨ 🍰 ✨")
        click.secho(str(report), err=True)
    ctx.exit(report.return_code)