Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
"""
Some tests that need a very specific file set to test against for corner cases.
See `_TestGlob` class for more information in regards to test case format.
"""
cases = [
# Test very specific, special cases.
[('a[/]b',), [('a[', ']b',)]],
[('@(a/b)',), []],
[('@(a[/]b)',), []],
[('test[',), [('test[',)]],
[(r'a\/b',), [('a', 'b')] if not util.is_case_sensitive() else []],
[(r'a[\/]b',), [('a[', ']b')] if not util.is_case_sensitive() else []],
Options(skip=util.is_case_sensitive()),
[('a[\\',), [('a[', '')]],
[('@(a[\\',), [('@(a[', '')]],
Options(skip=False)
]
@classmethod
def setup_fs(cls):
"""Setup file system."""
cls.mktemp('test[')
cls.mktemp('a', 'b')
cls.mktemp('a[', ']b')
cls.mktemp('@(a', 'b)')
cls.mktemp('@(a[', ']b)')
cls.can_symlink = can_symlink()
self.assertEqual(p1, [r'^(?s:test\ m)$'])
self.assertEqual(p2, [])
else:
self.assertEqual(p1, [r'(?s)^(?:test\ m)$'])
self.assertEqual(p2, [])
p1, p2 = fnmatch.translate(r'test[\\]test', flags=flags | fnmatch.R)
if util.PY36:
self.assertEqual(p1, [r'^(?s:test[\\]test)$'])
self.assertEqual(p2, [])
else:
self.assertEqual(p1, [r'(?s)^(?:test[\\]test)$'])
self.assertEqual(p2, [])
p1, p2 = fnmatch.translate('test[\\', flags=flags)
if util.PY36:
self.assertEqual(p1, [r'^(?s:test\[\\)$'])
self.assertEqual(p2, [])
else:
self.assertEqual(p1, [r'(?s)^(?:test\[\\)$'])
self.assertEqual(p2, [])
p1, p2 = fnmatch.translate(r'test\44test', flags=flags | fnmatch.R)
if util.PY36:
self.assertEqual(p1, [r'^(?s:test\$test)$'])
self.assertEqual(p2, [])
else:
self.assertEqual(p1, [r'(?s)^(?:test\$test)$'])
self.assertEqual(p2, [])
p1, p2 = fnmatch.translate(r'test\44', flags=flags | fnmatch.R)
if util.PY36:
@pytest.mark.skipif(not util.PY36, reason="path-like interface not supported on Python < 3.6")
def test_cwd_root_dir_pathlike_bytes(self):
"""Test root level glob when we switch directory via `root_dir` with a path-like object."""
self.assert_equal(glob.glob(b'EF', root_dir=pathlib.Path(self.tempdir)), [b'EF'])
['?*****?c', ['abc'], 0, ['abc']],
['?***?****c', ['abc'], 0, ['abc']],
['?***?****?', ['abc'], 0, ['abc']],
['?***?****', ['abc'], 0, ['abc']],
['*******c', ['abc'], 0, ['abc']],
['*******?', ['abc'], 0, ['abc']],
['a*cd**?**??k', ['abcdecdhjk'], 0, ['abcdecdhjk']],
['a**?**cd**?**??k', ['abcdecdhjk'], 0, ['abcdecdhjk']],
['a**?**cd**?**??k***', ['abcdecdhjk'], 0, ['abcdecdhjk']],
['a**?**cd**?**??***k', ['abcdecdhjk'], 0, ['abcdecdhjk']],
['a**?**cd**?**??***k**', ['abcdecdhjk'], 0, ['abcdecdhjk']],
['a****c**?**??*****', ['abcdecdhjk'], 0, ['abcdecdhjk']],
['[-abc]', ['-'], 0, ['-']],
['[abc-]', ['-'], 0, ['-']],
['\\', ['\\'], 0, ['\\']],
['[\\\\]', (['\\'] if util.is_case_sensitive() else []), 0, ['\\']],
['[\\\\]', ['\\'], glob.U, ['\\']],
['[\\\\]', [], glob.W, ['\\']],
['[[]', ['['], 0, ['[']],
['[', ['['], 0, ['[']],
['[*', ['[abc'], 0, ['[abc']],
# a right bracket shall lose its special meaning and\
# represent itself in a bracket expression if it occurs\
# first in the list. -- POSIX.2 2.8.3.2
['[]]', [']'], 0, [']']],
['[]-]', [']'], 0, [']']],
['[a-\\z]', ['p'], 0, ['p']],
['??**********?****?', [], 0, ['abc']],
['??**********?****c', [], 0, ['abc']],
['?************c****?****', [], 0, ['abc']],
['*c*?**', [], 0, ['abc']],
def escape_drive(drive, case):
"""Escape drive."""
if case and not util.PY36:
escaped = []
upper = drive.upper()
lower = drive.lower()
for index, u in enumerate(upper):
l = lower[index]
if u != l:
escaped.append('[{}{}]'.format(re.escape(l), re.escape(u)))
else:
escaped.append(re.escape(l))
return ''.join(escaped)
else:
return '(?i:{})'.format(re.escape(drive)) if case else re.escape(drive)
from . import _wcparse
from . import util
__all__ = (
"CASE", "IGNORECASE", "RAWCHARS", "DOTGLOB", "DOTMATCH",
"EXTGLOB", "EXTMATCH", "GLOBSTAR", "NEGATE", "MINUSNEGATE", "BRACE", "NOUNIQUE",
"REALPATH", "FOLLOW", "MATCHBASE", "MARK", "NEGATEALL", "NODIR", "FORCEWIN", "FORCEUNIX", "GLOBTILDE",
"NODOTDIR", "SCANDOTDIR",
"C", "I", "R", "D", "E", "G", "N", "M", "B", "P", "L", "S", "X", 'K', "O", "A", "W", "U", "T", "Q", "Z", "SD",
"iglob", "glob", "globmatch", "globfilter", "escape", "raw_escape"
)
# We don't use `util.platform` only because we mock it in tests,
# and `scandir` will not work with bytes on the wrong system.
WIN = sys.platform.startswith('win')
NO_SCANDIR_WORKAROUND = util.PY36
C = CASE = _wcparse.CASE
I = IGNORECASE = _wcparse.IGNORECASE
R = RAWCHARS = _wcparse.RAWCHARS
D = DOTGLOB = DOTMATCH = _wcparse.DOTMATCH
E = EXTGLOB = EXTMATCH = _wcparse.EXTMATCH
G = GLOBSTAR = _wcparse.GLOBSTAR
N = NEGATE = _wcparse.NEGATE
M = MINUSNEGATE = _wcparse.MINUSNEGATE
B = BRACE = _wcparse.BRACE
P = REALPATH = _wcparse.REALPATH
L = FOLLOW = _wcparse.FOLLOW
S = SPLIT = _wcparse.SPLIT
X = MATCHBASE = _wcparse.MATCHBASE
O = NODIR = _wcparse.NODIR
A = NEGATEALL = _wcparse.NEGATEALL
def expand(self, pattern):
"""Expand."""
self.depth = 0
self.expanding = False
self.total = 0
if pattern:
i = iter(util.StringIter(pattern))
for x in self.get_literals(next(i), i):
if not self.total:
break
self.total -= 1
yield x
else:
yield ""
def _valid_folder(self, base, name):
"""Return whether a folder can be searched."""
valid = True
fullpath = os.path.join(base, name)
if (
not self.recursive or
(
self.folder_exclude_check is not None and
not self.compare_directory(fullpath[self._base_len:] if self.dir_pathname else name)
)
):
valid = False
if valid and (not self.show_hidden and util.is_hidden(fullpath)):
valid = False
return self.on_validate_directory(base, name) if valid else valid