-
Notifications
You must be signed in to change notification settings - Fork 2.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added --include and --exclude cli options #281
Merged
+123
−22
Merged
Changes from 1 commit
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
b185331
Added --include and --exclude cli options
autophagy 9fa5d9c
Addressing review comments
autophagy 88c1c50
Addressing review comments
autophagy 57e86ab
Add tests for invalid and empty include/excludes
autophagy 7d008a4
Update help for windows systems
autophagy File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -46,6 +46,10 @@ | |
|
||
__version__ = "18.5b1" | ||
DEFAULT_LINE_LENGTH = 88 | ||
DEFAULT_EXCLUDES = ( | ||
r"build/|buck-out/|dist/|_build/|\.git/|\.hg/|\.mypy_cache/|\.tox/|\.venv/" | ||
) | ||
DEFAULT_INCLUDES = r"\.pyi?$" | ||
CACHE_DIR = Path(user_cache_dir("black", version=__version__)) | ||
|
||
|
||
|
@@ -189,6 +193,26 @@ class FileMode(Flag): | |
is_flag=True, | ||
help="Don't normalize string quotes or prefixes.", | ||
) | ||
@click.option( | ||
"--include", | ||
type=str, | ||
default=DEFAULT_INCLUDES, | ||
help=( | ||
"A regular expression that matches files and directories that should be " | ||
"included on recursive searches." | ||
), | ||
show_default=True, | ||
) | ||
@click.option( | ||
"--exclude", | ||
type=str, | ||
default=DEFAULT_EXCLUDES, | ||
help=( | ||
"A regular expression that matches files and directories that should be " | ||
"excluded on recursive searches." | ||
), | ||
show_default=True, | ||
) | ||
@click.version_option(version=__version__) | ||
@click.argument( | ||
"src", | ||
|
@@ -208,14 +232,28 @@ def main( | |
py36: bool, | ||
skip_string_normalization: bool, | ||
quiet: bool, | ||
include: str, | ||
exclude: str, | ||
src: List[str], | ||
) -> None: | ||
"""The uncompromising code formatter.""" | ||
sources: List[Path] = [] | ||
for s in src: | ||
p = Path(s) | ||
if p.is_dir(): | ||
sources.extend(gen_python_files_in_dir(p)) | ||
try: | ||
include_regex = re.compile(include) | ||
except Exception as exc: | ||
raise SyntaxError( | ||
"Invalid regular expression for include given: {}".format(include) | ||
) | ||
try: | ||
exclude_regex = re.compile(exclude) | ||
except Exception as esc: | ||
raise SyntaxError( | ||
"Invalid regular expression for exclude given: {}".format(exclude) | ||
) | ||
sources.extend(gen_python_files_in_dir(p, include_regex, exclude_regex)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sorry, just noticed this now.
|
||
elif p.is_file(): | ||
# if a file was explicitly given, we don't care about its extension | ||
sources.append(p) | ||
|
@@ -2750,32 +2788,24 @@ def get_future_imports(node: Node) -> Set[str]: | |
return imports | ||
|
||
|
||
PYTHON_EXTENSIONS = {".py", ".pyi"} | ||
BLACKLISTED_DIRECTORIES = { | ||
"build", | ||
"buck-out", | ||
"dist", | ||
"_build", | ||
".git", | ||
".hg", | ||
".mypy_cache", | ||
".tox", | ||
".venv", | ||
} | ||
|
||
|
||
def gen_python_files_in_dir(path: Path) -> Iterator[Path]: | ||
"""Generate all files under `path` which aren't under BLACKLISTED_DIRECTORIES | ||
and have one of the PYTHON_EXTENSIONS. | ||
def gen_python_files_in_dir( | ||
path: Path, include: Pattern[str], exclude: Pattern[str] | ||
) -> Iterator[Path]: | ||
"""Generate all files under `path` whose paths are not excluded by the | ||
`exclude` regex, but are included by the `include` regex. | ||
""" | ||
for child in path.iterdir(): | ||
if child.is_dir(): | ||
if child.name in BLACKLISTED_DIRECTORIES: | ||
if exclude.search(str(child) + "/"): | ||
continue | ||
|
||
yield from gen_python_files_in_dir(child) | ||
yield from gen_python_files_in_dir(child, include, exclude) | ||
|
||
elif child.is_file() and child.suffix in PYTHON_EXTENSIONS: | ||
elif ( | ||
child.is_file() | ||
and include.search(str(child)) | ||
and not exclude.search(str(child)) | ||
): | ||
yield child | ||
|
||
|
||
|
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There's a problem to solve here. In the current model, "not_a_build/" is also matching. It would be more robust to prepend all those directory names with slashes. However, if the user specified a relative directory, we might not have a leading slash always. If they said
black .
, it's fine. If they saidblack *
, it's not.So up to you how we solve this but it will have to be addressed before we land this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good point, I think prepending the directories with slashes would be the best route. I'll play around with it and see if I can come up with an elegant way of doing this.