Compute the set of files to be formatted.
(
*,
root: Path,
src: tuple[str, ...],
quiet: bool,
verbose: bool,
include: Pattern[str],
exclude: Pattern[str] | None,
extend_exclude: Pattern[str] | None,
force_exclude: Pattern[str] | None,
report: "Report",
stdin_filename: str | None,
)
| 761 | |
| 762 | |
| 763 | def get_sources( |
| 764 | *, |
| 765 | root: Path, |
| 766 | src: tuple[str, ...], |
| 767 | quiet: bool, |
| 768 | verbose: bool, |
| 769 | include: Pattern[str], |
| 770 | exclude: Pattern[str] | None, |
| 771 | extend_exclude: Pattern[str] | None, |
| 772 | force_exclude: Pattern[str] | None, |
| 773 | report: "Report", |
| 774 | stdin_filename: str | None, |
| 775 | ) -> set[Path]: |
| 776 | """Compute the set of files to be formatted.""" |
| 777 | sources: set[Path] = set() |
| 778 | |
| 779 | assert root.is_absolute(), f"INTERNAL ERROR: `root` must be absolute but is {root}" |
| 780 | using_default_exclude = exclude is None |
| 781 | exclude = re_compile_maybe_verbose(DEFAULT_EXCLUDES) if exclude is None else exclude |
| 782 | gitignore: dict[Path, GitIgnoreSpec] | None = None |
| 783 | root_gitignore = get_gitignore(root) |
| 784 | |
| 785 | for s in src: |
| 786 | if s == "-" and stdin_filename: |
| 787 | path = Path(stdin_filename) |
| 788 | if path_is_excluded(stdin_filename, force_exclude): |
| 789 | report.path_ignored( |
| 790 | path, |
| 791 | "--stdin-filename matches the --force-exclude regular expression", |
| 792 | ) |
| 793 | continue |
| 794 | is_stdin = True |
| 795 | else: |
| 796 | path = Path(s) |
| 797 | is_stdin = False |
| 798 | |
| 799 | # Compare the logic here to the logic in `gen_python_files`. |
| 800 | if is_stdin or path.is_file(): |
| 801 | if resolves_outside_root_or_cannot_stat(path, root, report): |
| 802 | if verbose: |
| 803 | out(f'Skipping invalid source: "{path}"', fg="red") |
| 804 | continue |
| 805 | |
| 806 | root_relative_path = best_effort_relative_path(path, root).as_posix() |
| 807 | root_relative_path = "/" + root_relative_path |
| 808 | |
| 809 | # Hard-exclude any files that matches the `--force-exclude` regex. |
| 810 | if path_is_excluded(root_relative_path, force_exclude): |
| 811 | report.path_ignored( |
| 812 | path, "matches the --force-exclude regular expression" |
| 813 | ) |
| 814 | continue |
| 815 | |
| 816 | if is_stdin: |
| 817 | path = Path(f"{STDIN_PLACEHOLDER}{path}") |
| 818 | |
| 819 | if path.suffix == ".ipynb" and not jupyter_dependencies_are_installed( |
| 820 | warn=verbose or not quiet |
no test coverage detected