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

Add Exclusion Filters #705

Merged
merged 23 commits into from
Dec 29, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
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
Prev Previous commit
Next Next commit
Fix directories tests on Windows
Regexes did not match properly because the separator for Windows is '\\'
  • Loading branch information
glubsy committed Dec 29, 2020
commit 4b4cc04e879c650e956b16f985447ce77c9cd40f
31 changes: 23 additions & 8 deletions core/exclude.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,18 @@
import logging
import functools
from hscommon.util import FileOrPath
from hscommon.plat import ISWINDOWS
import time

default_regexes = [r"^thumbs\.db$", # Obsolete after WindowsXP
r"^desktop\.ini$", # Windows metadata
r"^\.DS_Store$", # MacOS metadata
r"^\.Trash\-.*", # Linux trash directories
r"^\$Recycle\.Bin$", # Windows
r"^\..*", # Hidden files
r"^\..*", # Hidden files on Unix-like
]
# These are too broad
forbidden_regexes = [r".*", r"\/.*", r".*\/.*", r".*\..*"]
forbidden_regexes = [r".*", r"\/.*", r".*\/.*", r".*\\\\.*", r".*\..*"]


def timer(func):
Expand Down Expand Up @@ -168,10 +169,16 @@ def error(self, regex):

def build_compiled_caches(self, union=False):
if not union:
self._cached_compiled_files =\
[x for x in self._excluded_compiled if sep not in x.pattern]
self._cached_compiled_paths =\
[x for x in self._excluded_compiled if sep in x.pattern]
if not ISWINDOWS:
self._cached_compiled_files =\
[x for x in self._excluded_compiled if not has_sep(x.pattern)]
self._cached_compiled_paths =\
[x for x in self._excluded_compiled if has_sep(x.pattern)]
else:
self._cached_compiled_files =\
[x for x in self._excluded_compiled if not has_sep(x.pattern)]
self._cached_compiled_paths =\
[x for x in self._excluded_compiled if has_sep(x.pattern)]
return
marked_count = [x for marked, x in self if marked]
# If there is no item, the compiled Pattern will be '' and match everything!
Expand All @@ -184,13 +191,13 @@ def build_compiled_caches(self, union=False):
# the same regardless of whether the client asked for union or not
self._cached_compiled_union_all =\
(re.compile('|'.join(marked_count)),)
files_marked = [x for x in marked_count if sep not in x]
files_marked = [x for x in marked_count if not has_sep(x)]
if not files_marked:
self._cached_compiled_union_files = tuple()
else:
self._cached_compiled_union_files =\
(re.compile('|'.join(files_marked)),)
paths_marked = [x for x in marked_count if sep in x]
paths_marked = [x for x in marked_count if has_sep(x)]
if not paths_marked:
self._cached_compiled_union_paths = tuple()
else:
Expand Down Expand Up @@ -488,3 +495,11 @@ def ordered_keys(_dict):
list_of_items.sort(key=lambda x: x[1].get("index"))
for item in list_of_items:
yield item[0]


if ISWINDOWS:
def has_sep(x):
return '\\' + sep in x
else:
def has_sep(x):
return sep in x
8 changes: 7 additions & 1 deletion core/tests/directories_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from pytest import raises
from hscommon.path import Path
from hscommon.testutil import eq_
from hscommon.plat import ISWINDOWS

from ..fs import File
from ..directories import (
Expand Down Expand Up @@ -428,7 +429,10 @@ def test_exclude_refined(self, tmpdir):
assert "unwanted_subdirfile.gif" not in files
assert "unwanted_subdarfile.png" not in files

regex3 = r".*Recycle\.Bin\/.*unwanted.*subdirfile.*"
if ISWINDOWS:
regex3 = r".*Recycle\.Bin\\.*unwanted.*subdirfile.*"
else:
regex3 = r".*Recycle\.Bin\/.*unwanted.*subdirfile.*"
self.d._exclude_list.rename(regex2, regex3)
assert self.d._exclude_list.error(regex3) is None
# print(f"get_folders(): {[x for x in self.d.get_folders()]}")
Expand Down Expand Up @@ -516,6 +520,8 @@ def test_get_state_returns_excluded_for_hidden_directories_and_files(self, tmpdi
self.d.set_state(p1["foobar"][".hidden_dir"], DirectoryState.Normal)
# The files should still be filtered
files = self.get_files_and_expect_num_result(1)
eq_(len(self.d._exclude_list.compiled_paths), 0)
eq_(len(self.d._exclude_list.compiled_files), 1)
assert ".hidden_file.txt" not in files
assert ".hidden_subfile.png" not in files
assert "foobar.jpg" in files
Expand Down
2 changes: 1 addition & 1 deletion core/tests/exclude_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ def test_same_number_of_expressions(self):
assert expr.pattern in exprs

def test_compiled_files(self):
# test is separator is indeed checked properly to yield the output
# is path separator checked properly to yield the output
regex1 = r"test/one/sub"
self.e_separate.add(regex1)
self.e_separate.mark(regex1)
Expand Down