Skip to content

Commit

Permalink
Resolve #1684: Add support for --extend-skip and --extend-skip-glob
Browse files Browse the repository at this point in the history
  • Loading branch information
timothycrosley committed Mar 14, 2021
1 parent 31e4fde commit d09acd0
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 6 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ Find out more about isort's release policy [here](https://pycqa.github.io/isort/
- Implemented #1669: Parallel (`-j`) now defaults to number of CPU cores if no value is provided.
- Implemented #1668: Added a safeguard against accidental usage against /.
- Implemented #1638 / #1644: Provide a flag `--overwrite-in-place` to ensure same file handle is used after sorting.
- Implemented #1684: Added support for extending skips with `--extend-skip` and `--extend-skip-glob`.
- Documented #1685: Skip doesn't support plain directory names, but skip_glob does.

### 5.7.0 December 30th 2020
Expand Down
20 changes: 18 additions & 2 deletions isort/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -311,17 +311,33 @@ def _build_arg_parser() -> argparse.ArgumentParser:
target_group.add_argument(
"-s",
"--skip",
help="Files that sort imports should skip over. If you want to skip multiple "
help="Files that isort should skip over. If you want to skip multiple "
"files you should specify twice: --skip file1 --skip file2. Values can be "
"file names, directory names or file paths. To skip all files in a nested path "
"use --skip-glob.",
dest="skip",
action="append",
)
target_group.add_argument(
"--extend-skip",
help="Extends --skip to add additional files that isort should skip over. "
"If you want to skip multiple "
"files you should specify twice: --skip file1 --skip file2. Values can be "
"file names, directory names or file paths. To skip all files in a nested path "
"use --skip-glob.",
dest="extend_skip",
action="append",
)
target_group.add_argument(
"--sg",
"--skip-glob",
help="Files that sort imports should skip over.",
help="Files that isort should skip over.",
dest="skip_glob",
action="append",
)
target_group.add_argument(
"--extend-skip-glob",
help="Additional files that isort should skip over (extending --skip-glob).",
dest="skip_glob",
action="append",
)
Expand Down
28 changes: 25 additions & 3 deletions isort/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,9 @@ class _Config:
py_version: str = "3"
force_to_top: FrozenSet[str] = frozenset()
skip: FrozenSet[str] = DEFAULT_SKIP
extend_skip: FrozenSet[str] = frozenset()
skip_glob: FrozenSet[str] = frozenset()
extend_skip_glob: FrozenSet[str] = frozenset()
skip_gitignore: bool = False
line_length: int = 79
wrap_length: int = 0
Expand Down Expand Up @@ -267,13 +269,17 @@ def __init__(
):
self._known_patterns: Optional[List[Tuple[Pattern[str], str]]] = None
self._section_comments: Optional[Tuple[str, ...]] = None
self._skips: Optional[FrozenSet[str]] = None
self._skip_globs: Optional[FrozenSet[str]] = None

if config:
config_vars = vars(config).copy()
config_vars.update(config_overrides)
config_vars["py_version"] = config_vars["py_version"].replace("py", "")
config_vars.pop("_known_patterns")
config_vars.pop("_section_comments")
config_vars.pop("_skips")
config_vars.pop("_skip_globs")
super().__init__(**config_vars) # type: ignore
return

Expand Down Expand Up @@ -521,19 +527,19 @@ def is_skipped(self, file_path: Path) -> bool:
if normalized_path[1:2] == ":":
normalized_path = normalized_path[2:]

for skip_path in self.skip:
for skip_path in self.skips:
if posixpath.abspath(normalized_path) == posixpath.abspath(
skip_path.replace("\\", "/")
):
return True

position = os.path.split(file_name)
while position[1]:
if position[1] in self.skip:
if position[1] in self.skips:
return True
position = os.path.split(position[0])

for glob in self.skip_glob:
for glob in self.skip_globs:
if fnmatch.fnmatch(file_name, glob) or fnmatch.fnmatch("/" + file_name, glob):
return True

Expand Down Expand Up @@ -574,6 +580,22 @@ def section_comments(self) -> Tuple[str, ...]:
self._section_comments = tuple(f"# {heading}" for heading in self.import_headings.values())
return self._section_comments

@property
def skips(self) -> FrozenSet[str]:
if self._skips is not None:
return self._skips

self._skips = self.skip.union(self.extend_skip)
return self._skips

@property
def skip_globs(self) -> FrozenSet[str]:
if self._skip_globs is not None:
return self._skip_globs

self._skip_globs = self.skip_glob.union(self.extend_skip_glob)
return self._skip_globs

def _parse_known_pattern(self, pattern: str) -> List[str]:
"""Expand pattern if identified as a directory and return found sub packages"""
if pattern.endswith(os.path.sep):
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@ def test_main(capsys, tmpdir):
import b
"""
)
main.main([str(tmpdir), "--skip", "skip.py", "--check"])
main.main([str(tmpdir), "--extend-skip", "skip.py", "--check"])

# without filter options passed in should successfully sort files
main.main([str(python_file), str(should_skip), "--verbose", "--atomic"])
Expand Down

0 comments on commit d09acd0

Please sign in to comment.