Skip to content

Commit ced0e07

Browse files
committed
Refactor get_ignore() into an Ignore callable
1 parent bda901a commit ced0e07

File tree

3 files changed

+33
-28
lines changed

3 files changed

+33
-28
lines changed

sphinx_autobuild/__main__.py

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
from sphinx_autobuild import __version__
1313
from sphinx_autobuild.build import SPHINX_BUILD_OPTIONS, Builder
14-
from sphinx_autobuild.ignore import get_ignore
14+
from sphinx_autobuild.ignore import Ignore
1515
from sphinx_autobuild.utils import find_free_port
1616

1717

@@ -36,16 +36,15 @@ def _get_build_args(args):
3636
return build_args, pre_build_commands
3737

3838

39-
def _get_ignore_handler(args):
40-
regular = [os.path.realpath(path) for path in args.ignore[:]]
41-
regular.append(os.path.realpath(args.outdir)) # output directory
42-
if args.w: # Logfile
43-
regular.append(os.path.realpath(args.w[0]))
44-
if args.d: # Doctrees
45-
regular.append(os.path.realpath(args.d[0]))
39+
def _get_ignore_handler(ignore, regex_based, out_dir, doctree_dir, warnings_file):
40+
regular = list(map(os.path.realpath, ignore))
41+
regular.append(os.path.realpath(out_dir)) # output directory
42+
if doctree_dir: # Doctrees
43+
regular.append(os.path.realpath(doctree_dir))
44+
if warnings_file: # Logfile
45+
regular.append(os.path.realpath(warnings_file))
4646

47-
regex_based = args.re_ignore
48-
return get_ignore(regular, regex_based)
47+
return Ignore(regular, regex_based)
4948

5049

5150
def get_parser():
@@ -184,7 +183,8 @@ def main():
184183
pre_build_commands=pre_build_commands,
185184
)
186185

187-
ignore_handler = _get_ignore_handler(args)
186+
ignore_handler = _get_ignore_handler(args.ignore, args.re_ignore, outdir,
187+
args.w, args.d)
188188
server.watch(srcdir, builder, ignore=ignore_handler)
189189
for dirpath in args.additional_watched_dirs:
190190
dirpath = os.path.realpath(dirpath)

sphinx_autobuild/ignore.py

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,32 @@
11
"""Logic for ignoring paths."""
2+
23
import fnmatch
34
import os
45
import re
56

67

7-
def get_ignore(regular_patterns, regex_based):
8-
"""Prepare the function that determines whether a path should be ignored."""
9-
regex_based_patterns = list(map(re.compile, regex_based))
8+
class Ignore:
9+
def __init__(self, regular, regex_based):
10+
"""Prepare the function that determines whether a path should be ignored."""
11+
self.regular_patterns = regular
12+
self.regex_based_patterns = list(map(re.compile, regex_based))
13+
14+
def __repr__(self):
15+
return (f"Ignore(regular={self.regular_patterns!r}, "
16+
f"regex_based={self.regex_based_patterns!r})")
1017

11-
def ignore(path):
12-
"""Determine if path should be ignored."""
18+
def __call__(self, path):
19+
"""Determine if 'path' should be ignored."""
1320
# Any regular pattern matches.
14-
for pattern in regular_patterns:
15-
if path.startswith((pattern + os.sep, pattern + '/')):
21+
for pattern in self.regular_patterns:
22+
if path.startswith((pattern + os.sep, pattern + "/")):
1623
return True
1724
if fnmatch.fnmatch(path, pattern):
1825
return True
1926

2027
# Any regular expression matches.
21-
for regex in regex_based_patterns:
28+
for regex in self.regex_based_patterns:
2229
if regex.search(path):
2330
return True
2431

2532
return False
26-
27-
return ignore

tests/test_ignore.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
from sphinx_autobuild.ignore import get_ignore
1+
from sphinx_autobuild.ignore import Ignore
22

33

44
def test_empty():
5-
ignored = get_ignore([], [])
5+
ignored = Ignore([], [])
66

77
assert not ignored("amazing-file.txt")
88
assert not ignored("module.pyc")
@@ -14,7 +14,7 @@ def test_empty():
1414

1515

1616
def test_single_regex():
17-
ignored = get_ignore([], [r"\.pyc$"])
17+
ignored = Ignore([], [r"\.pyc$"])
1818

1919
assert not ignored("amazing-file.txt")
2020
assert ignored("module.pyc")
@@ -26,7 +26,7 @@ def test_single_regex():
2626

2727

2828
def test_multiple_regex():
29-
ignored = get_ignore([], [r"\.md", r"one\.rst"])
29+
ignored = Ignore([], [r"\.md", r"one\.rst"])
3030

3131
assert not ignored("amazing-file.txt")
3232
assert not ignored("module.pyc")
@@ -38,7 +38,7 @@ def test_multiple_regex():
3838

3939

4040
def test_single_regular():
41-
ignored = get_ignore(["*.pyc"], [])
41+
ignored = Ignore(["*.pyc"], [])
4242

4343
assert not ignored("amazing-file.txt")
4444
assert ignored("module.pyc")
@@ -50,7 +50,7 @@ def test_single_regular():
5050

5151

5252
def test_multiple_regular():
53-
ignored = get_ignore(["bar", "foo"], [])
53+
ignored = Ignore(["bar", "foo"], [])
5454

5555
assert not ignored("amazing-file.txt")
5656
assert not ignored("module.pyc")
@@ -62,7 +62,7 @@ def test_multiple_regular():
6262

6363

6464
def test_multiple_both():
65-
ignored = get_ignore(["bar", "foo"], [r"\.txt", r"one\.*"])
65+
ignored = Ignore(["bar", "foo"], [r"\.txt", r"one\.*"])
6666

6767
assert ignored("amazing-file.txt")
6868
assert not ignored("module.pyc")

0 commit comments

Comments
 (0)