Skip to content
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

Support glob-style operations (rebase) #171

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 21 additions & 1 deletion sphinx_autobuild/filter.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import fnmatch
import os
import re
from glob import glob


class IgnoreFilter:
Expand All @@ -19,9 +20,28 @@ def __repr__(self):

def __call__(self, path):
"""Determine if 'path' should be ignored."""
# Return the full path so we make sure we handle relative paths OK
path_expanded = os.path.abspath(path)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should prefer pathlib for path operations

# Any regular pattern matches.
for pattern in self.regular_patterns:
if path.startswith((pattern + os.sep, pattern + "/")):
# Expand the pattern into a list of files that match a glob
matched_files = set(map(os.path.abspath, glob(pattern, recursive=True)))

if path_expanded in matched_files:
return True

# If the parent of this path matches any of the glob matches, ignore it
if any(path_expanded.startswith(imatch) for imatch in matched_files):
return True

# These two checks are for preserving old behavior.
# They might not be necessary but leaving here just in case.
# Neither depends on the files actually being on disk.

if path.strip(os.path.sep).startswith((
pattern.strip(os.path.sep) + os.path.sep,
pattern + "/",
)):
return True
if fnmatch.fnmatch(path, pattern):
return True
Comment on lines 46 to 47
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a way to merge fnmatch and glob?

Expand Down
24 changes: 24 additions & 0 deletions tests/test_ignore.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from glob import glob

from sphinx_autobuild.filter import IgnoreFilter


Expand Down Expand Up @@ -72,3 +74,25 @@ def test_multiple_both():
assert ignored("foo/random.txt")
assert ignored("foo/module.pyc")
assert ignored("bar/__pycache__/file.pyc")


def test_glob_expression():
ignored = IgnoreFilter(
[
# Glob for folder
"**/do_ignore",
# Glob for files
"**/*doignore*.*",
],
[],
)
# Root folder of our glob test files. Assume tests are run from project root.
for ifile in glob("tests/test_ignore_glob/**/*"):
# Convert to be relative to the tests directory since that mimics
# the user's behavior.
if "do_ignore" in ifile or "doignore" in ifile:
print(f"Should ignore: {ifile})")
assert ignored(ifile)
else:
print(f"Should NOT ignore: {ifile})")
assert not ignored(ifile)
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Loading