Skip to content

Commit 38c50fd

Browse files
author
atollk
committed
Fixed tox
1 parent 16ea5fe commit 38c50fd

File tree

7 files changed

+90
-23
lines changed

7 files changed

+90
-23
lines changed

fs/_ftp_parse.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ def parse(lines):
7878

7979
def parse_line(line):
8080
for line_re, decode_callable in get_decoders():
81-
match = line_re._match(line)
81+
match = line_re.match(line)
8282
if match is not None:
8383
return decode_callable(line, match)
8484
return None

fs/base.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -539,22 +539,22 @@ def filterdir(
539539
def match_dir(patterns, info):
540540
# type: (Optional[Iterable[Text]], Info) -> bool
541541
"""Pattern match info.name."""
542-
return info.is_file or self._match(patterns, info.name)
542+
return info.is_file or self.match(patterns, info.name)
543543

544544
def match_file(patterns, info):
545545
# type: (Optional[Iterable[Text]], Info) -> bool
546546
"""Pattern match info.name."""
547-
return info.is_dir or self._match(patterns, info.name)
547+
return info.is_dir or self.match(patterns, info.name)
548548

549549
def exclude_dir(patterns, info):
550550
# type: (Optional[Iterable[Text]], Info) -> bool
551551
"""Pattern match info.name."""
552-
return info.is_file or not self._match(patterns, info.name)
552+
return info.is_file or not self.match(patterns, info.name)
553553

554554
def exclude_file(patterns, info):
555555
# type: (Optional[Iterable[Text]], Info) -> bool
556556
"""Pattern match info.name."""
557-
return info.is_dir or not self._match(patterns, info.name)
557+
return info.is_dir or not self.match(patterns, info.name)
558558

559559
if files:
560560
filters.append(partial(match_file, files))
@@ -1581,7 +1581,9 @@ def match(self, patterns, name, accept_prefix=False):
15811581
case_sensitive = not typing.cast(
15821582
bool, self.getmeta().get("case_insensitive", False)
15831583
)
1584-
matcher = wildcard.get_matcher(patterns, case_sensitive, accept_prefix=accept_prefix)
1584+
matcher = wildcard.get_matcher(
1585+
patterns, case_sensitive, accept_prefix=accept_prefix
1586+
)
15851587
return matcher(name)
15861588

15871589
def tree(self, **kwargs):

fs/glob.py

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,50 @@
2727
) # type: LRUCache[Tuple[Text, bool], Tuple[int, bool, Pattern]]
2828

2929

30+
def _translate(pattern, case_sensitive=True):
31+
# type: (Text, bool) -> Text
32+
"""Translate a wildcard pattern to a regular expression.
33+
There is no way to quote meta-characters.
34+
Arguments:
35+
pattern (str): A wildcard pattern.
36+
case_sensitive (bool): Set to `False` to use a case
37+
insensitive regex (default `True`).
38+
Returns:
39+
str: A regex equivalent to the given pattern.
40+
"""
41+
if not case_sensitive:
42+
pattern = pattern.lower()
43+
i, n = 0, len(pattern)
44+
res = ""
45+
while i < n:
46+
c = pattern[i]
47+
i = i + 1
48+
if c == "*":
49+
res = res + "[^/]*"
50+
elif c == "?":
51+
res = res + "."
52+
elif c == "[":
53+
j = i
54+
if j < n and pattern[j] == "!":
55+
j = j + 1
56+
if j < n and pattern[j] == "]":
57+
j = j + 1
58+
while j < n and pattern[j] != "]":
59+
j = j + 1
60+
if j >= n:
61+
res = res + "\\["
62+
else:
63+
stuff = pattern[i:j].replace("\\", "\\\\")
64+
i = j + 1
65+
if stuff[0] == "!":
66+
stuff = "^" + stuff[1:]
67+
elif stuff[0] == "^":
68+
stuff = "\\" + stuff
69+
res = "%s[%s]" % (res, stuff)
70+
else:
71+
res = res + re.escape(c)
72+
return res
73+
3074
def _translate_glob(pattern, case_sensitive=True):
3175
levels = 0
3276
recursive = False
@@ -37,7 +81,7 @@ def _translate_glob(pattern, case_sensitive=True):
3781
recursive = True
3882
else:
3983
re_patterns.append(
40-
"/" + wildcard._translate(component, case_sensitive=case_sensitive)
84+
"/" + _translate(component, case_sensitive=case_sensitive)
4185
)
4286
levels += 1
4387
re_glob = "(?ms)^" + "".join(re_patterns) + ("/$" if pattern.endswith("/") else "$")

fs/test.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1783,9 +1783,9 @@ def test_movedir(self):
17831783
self.fs.movedir("foo2/foofoo.txt", "foo2/baz/egg")
17841784

17851785
def test_match(self):
1786-
self.assertTrue(self.fs._match(["*.py"], "foo.py"))
1786+
self.assertTrue(self.fs.match(["*.py"], "foo.py"))
17871787
self.assertEqual(
1788-
self.fs._match(["*.py"], "FOO.PY"),
1788+
self.fs.match(["*.py"], "FOO.PY"),
17891789
self.fs.getmeta().get("case_insensitive", False),
17901790
)
17911791

fs/walk.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,8 @@ def check_scan_dir(self, fs, path, info):
276276
def _check_file(self, fs, dir_path, info):
277277
# type: (FS, Text, Info) -> bool
278278
"""Check if a filename should be included."""
279-
# Weird check required for backwards compatibility, when _check_file did not exist.
279+
# Weird check required for backwards compatibility,
280+
# when _check_file did not exist.
280281
if Walker._check_file == type(self)._check_file:
281282
if self.exclude is not None and fs.match(self.exclude, info.name):
282283
return False

fs/wildcard.py

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
"""Match wildcard filenames.
22
3-
Patterns are derived from Unix shell style:
3+
Patterns are derived from Unix shell style:
44
5-
* matches everything except for a path seperator
6-
** matches everything
7-
? matches any single character
8-
[seq] matches any character in seq
9-
[!seq] matches any char not in seq
5+
* matches everything except for a path seperator
6+
** matches everything
7+
? matches any single character
8+
[seq] matches any character in seq
9+
[!seq] matches any char not in seq
1010
"""
1111

1212
from __future__ import unicode_literals, print_function
@@ -19,7 +19,9 @@
1919
from typing import Callable, Iterable, Text, Tuple
2020

2121

22-
_PATTERN_CACHE = LRUCache(1000) # type: LRUCache[Tuple[Text, bool, bool], PatternMatcher]
22+
_PATTERN_CACHE = LRUCache(
23+
1000
24+
) # type: LRUCache[Tuple[Text, bool, bool], _PatternMatcher]
2325

2426

2527
def match(pattern, name, accept_prefix=False):
@@ -38,7 +40,7 @@ def match(pattern, name, accept_prefix=False):
3840
try:
3941
pat = _PATTERN_CACHE[args]
4042
except KeyError:
41-
pat = PatternMatcher(*args)
43+
pat = _PatternMatcher(*args)
4244
_PATTERN_CACHE[args] = pat
4345
return pat(name)
4446

@@ -59,7 +61,7 @@ def imatch(pattern, name, accept_prefix=False):
5961
try:
6062
pat = _PATTERN_CACHE[args]
6163
except KeyError:
62-
pat = PatternMatcher(*args)
64+
pat = _PatternMatcher(*args)
6365
_PATTERN_CACHE[args] = pat
6466
return pat(name)
6567

@@ -138,7 +140,7 @@ def get_matcher(patterns, case_sensitive, accept_prefix=False):
138140
return lambda name: imatch_any(patterns, name, accept_prefix)
139141

140142

141-
class PatternMatcher:
143+
class _PatternMatcher:
142144
def __init__(self, pattern, case_sensitive, accept_prefixes):
143145
self.case_sensitive = case_sensitive
144146
self.accept_prefixes = accept_prefixes
@@ -151,16 +153,24 @@ def __init__(self, pattern, case_sensitive, accept_prefixes):
151153
i = pattern.find("]", i) + 1
152154
self.tokens.append(_PatternMatcherToken(pattern[start:i]))
153155
elif pattern[i] == "]":
154-
raise ValueError(pattern + " is not a valid wildcard pattern. (unmatched ])")
156+
raise ValueError(
157+
pattern + " is not a valid wildcard pattern. (unmatched ])"
158+
)
155159
elif pattern[i] == "?":
156160
self.tokens.append(_PatternMatcherToken("?"))
157161
i += 1
158162
elif pattern[i] == "*":
159163
asterik_len = 1
160-
while (asterik_len + i) < len(pattern) and pattern[asterik_len + i] == "*":
164+
while (asterik_len + i) < len(pattern) and pattern[
165+
asterik_len + i
166+
] == "*":
161167
asterik_len += 1
162168
if asterik_len > 2:
163-
raise ValueError(pattern + " is not a valid wildcard pattern. (sequence of three or more *)")
169+
raise ValueError(
170+
pattern
171+
+ " is not a valid wildcard pattern. "
172+
+ "(sequence of three or more *)"
173+
)
164174
self.tokens.append(_PatternMatcherToken("*" * asterik_len))
165175
i += asterik_len
166176
else:

tests/test_walk.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,16 @@ def test_walk_files_filter(self):
314314

315315
self.assertEqual(files, [])
316316

317+
def test_walk_files_filter_glob(self):
318+
files = list(self.fs.walk.files(filter_glob=["/foo2/**"]))
319+
self.assertEqual(
320+
files,
321+
[
322+
"/foo2/top3.bin",
323+
"/foo2/bar2/bar3/test.txt",
324+
],
325+
)
326+
317327
def test_walk_files_exclude(self):
318328
# Test exclude argument works
319329
files = list(self.fs.walk.files(exclude=["*.txt"]))

0 commit comments

Comments
 (0)