from pathspec.patterns.gitwildmatch import GitWildMatchPatternError
from _black_version import version as __version__
-from black.cache import Cache, get_cache_info, read_cache, write_cache
+from black.cache import Cache
from black.comments import normalize_fmt_off
from black.const import (
DEFAULT_EXCLUDES,
)
from black.linegen import LN, LineGenerator, transform_line
from black.lines import EmptyLineTracker, LinesBlock
-from black.mode import (
- FUTURE_FLAG_TO_FEATURE,
- VERSION_TO_FEATURES,
- Feature,
- Mode,
- TargetVersion,
- supports_feature,
-)
+from black.mode import FUTURE_FLAG_TO_FEATURE, VERSION_TO_FEATURES, Feature
+from black.mode import Mode as Mode # re-exported
+from black.mode import TargetVersion, supports_feature
from black.nodes import (
STARS,
is_number_token,
content=code, fast=fast, write_back=write_back, mode=mode, report=report
)
else:
+ assert root is not None # root is only None if code is not None
try:
sources = get_sources(
- ctx=ctx,
+ root=root,
src=src,
quiet=quiet,
verbose=verbose,
def get_sources(
*,
- ctx: click.Context,
+ root: Path,
src: Tuple[str, ...],
quiet: bool,
verbose: bool,
) -> Set[Path]:
"""Compute the set of files to be formatted."""
sources: Set[Path] = set()
- root = ctx.obj["root"]
using_default_exclude = exclude is None
exclude = re_compile_maybe_verbose(DEFAULT_EXCLUDES) if exclude is None else exclude
if is_stdin or p.is_file():
normalized_path: Optional[str] = normalize_path_maybe_ignore(
- p, ctx.obj["root"], report
+ p, root, report
)
if normalized_path is None:
if verbose:
p = Path(f"{STDIN_PLACEHOLDER}{str(p)}")
if p.suffix == ".ipynb" and not jupyter_dependencies_are_installed(
- verbose=verbose, quiet=quiet
+ warn=verbose or not quiet
):
continue
sources.add(p)
elif p.is_dir():
- p = root / normalize_path_maybe_ignore(p, ctx.obj["root"], report)
+ p_relative = normalize_path_maybe_ignore(p, root, report)
+ assert p_relative is not None
+ p = root / p_relative
if verbose:
out(f'Found input source directory: "{p}"', fg="blue")
sources.update(
gen_python_files(
p.iterdir(),
- ctx.obj["root"],
+ root,
include,
exclude,
extend_exclude,
if format_stdin_to_stdout(fast=fast, write_back=write_back, mode=mode):
changed = Changed.YES
else:
- cache: Cache = {}
+ cache = Cache.read(mode)
if write_back not in (WriteBack.DIFF, WriteBack.COLOR_DIFF):
- cache = read_cache(mode)
- res_src = src.resolve()
- res_src_s = str(res_src)
- if res_src_s in cache and cache[res_src_s] == get_cache_info(res_src):
+ if not cache.is_changed(src):
changed = Changed.CACHED
if changed is not Changed.CACHED and format_file_in_place(
src, fast=fast, write_back=write_back, mode=mode
if (write_back is WriteBack.YES and changed is not Changed.CACHED) or (
write_back is WriteBack.CHECK and changed is Changed.NO
):
- write_cache(cache, [src], mode)
+ cache.write([src])
report.done(src, changed)
except Exception as exc:
if report.verbose:
yield
-def patch_click() -> None:
- """Make Click not crash on Python 3.6 with LANG=C.
-
- On certain misconfigured environments, Python 3 selects the ASCII encoding as the
- default which restricts paths that it can access during the lifetime of the
- application. Click refuses to work in this scenario by raising a RuntimeError.
-
- In case of Black the likelihood that non-ASCII characters are going to be used in
- file paths is minimal since it's Python source code. Moreover, this crash was
- spurious on Python 3.7 thanks to PEP 538 and PEP 540.
- """
- modules: List[Any] = []
- try:
- from click import core
- except ImportError:
- pass
- else:
- modules.append(core)
- try:
- # Removed in Click 8.1.0 and newer; we keep this around for users who have
- # older versions installed.
- from click import _unicodefun # type: ignore
- except ImportError:
- pass
- else:
- modules.append(_unicodefun)
-
- for module in modules:
- if hasattr(module, "_verify_python3_env"):
- module._verify_python3_env = lambda: None
- if hasattr(module, "_verify_python_env"):
- module._verify_python_env = lambda: None
-
-
def patched_main() -> None:
# PyInstaller patches multiprocessing to need freeze_support() even in non-Windows
# environments so just assume we always need to call it if frozen.
freeze_support()
- patch_click()
main()