Skip to content

Commit b5ac959

Browse files
authored
ignore python files by default (#228)
* fix excludes for files * fix `INCLUDE_EXT` for files * refactor `get_paths` and `iterate_dir` to use the same exclude logic with `should_parse_path` Co-authored-by: detachhead <detachhead@users.noreply.github.com>
1 parent a1652d1 commit b5ac959

File tree

1 file changed

+18
-11
lines changed

1 file changed

+18
-11
lines changed

robotidy/files.py

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -84,12 +84,21 @@ def get_gitignore(root: Path) -> PathSpec:
8484
return PathSpec.from_lines("gitwildmatch", lines)
8585

8686

87-
def path_is_excluded(
88-
normalized_path: str,
89-
pattern: Optional[Pattern[str]],
87+
def should_parse_path(
88+
path: Path, exclude: Optional[Pattern[str]], extend_exclude: Optional[Pattern[str]], gitignore: Optional[PathSpec]
9089
) -> bool:
91-
match = pattern.search(normalized_path) if pattern else None
92-
return bool(match and match.group(0))
90+
normalized_path = str(path)
91+
for pattern in (exclude, extend_exclude):
92+
match = pattern.search(normalized_path) if pattern else None
93+
if bool(match and match.group(0)):
94+
return False
95+
if gitignore is not None and gitignore.match_file(path):
96+
return False
97+
if path.is_file():
98+
return path.suffix in INCLUDE_EXT
99+
if exclude and exclude.match(path.name):
100+
return False
101+
return True
93102

94103

95104
def get_paths(src: Tuple[str, ...], exclude: Optional[Pattern], extend_exclude: Optional[Pattern]):
@@ -101,6 +110,8 @@ def get_paths(src: Tuple[str, ...], exclude: Optional[Pattern], extend_exclude:
101110
sources.add("-")
102111
continue
103112
path = Path(s).resolve()
113+
if not should_parse_path(path, exclude, extend_exclude, gitignore):
114+
continue
104115
if path.is_file():
105116
sources.add(path)
106117
elif path.is_dir():
@@ -118,18 +129,14 @@ def iterate_dir(
118129
gitignore: Optional[PathSpec],
119130
) -> Iterator[Path]:
120131
for path in paths:
121-
if gitignore is not None and gitignore.match_file(path):
122-
continue
123-
if path_is_excluded(str(path), exclude) or path_is_excluded(str(path), extend_exclude):
132+
if not should_parse_path(path, exclude, extend_exclude, gitignore):
124133
continue
125-
if path.is_dir() and exclude and not exclude.match(path.name):
134+
if path.is_dir():
126135
yield from iterate_dir(
127136
path.iterdir(),
128137
exclude,
129138
extend_exclude,
130139
gitignore + get_gitignore(path) if gitignore is not None else None,
131140
)
132141
elif path.is_file():
133-
if path.suffix not in INCLUDE_EXT:
134-
continue
135142
yield path

0 commit comments

Comments
 (0)