Skip to content

Commit 04f702e

Browse files
committed
Adding forgotten file.
1 parent d1967e4 commit 04f702e

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

solution-downloader/name_filter.py

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import re
2+
3+
4+
def _force_regex_list(regs):
5+
'''
6+
Convert a string or list of strings into a list of regexes.
7+
'''
8+
if type(regs) != list:
9+
regs = [regs]
10+
11+
return list(map(lambda r: re.compile(r), regs))
12+
13+
14+
def _match_regs(regs, name):
15+
'''
16+
Return true if the name fully matches one of the regexes on the list.
17+
'''
18+
for reg in regs:
19+
if reg.fullmatch(name) is not None:
20+
return True
21+
return False
22+
23+
24+
class NameFilter:
25+
'''
26+
Holds the name filter configuration and performs the filtering.
27+
'''
28+
def __init__(self, config) -> None:
29+
self._include = _force_regex_list(config.get('include_files', '.*') or '.*')
30+
self._exclude = _force_regex_list(config.get('exclude_files', []) or [])
31+
32+
def _is_included(self, name):
33+
return _match_regs(self._include, name)
34+
35+
def _is_excluded(self, name):
36+
return _match_regs(self._exclude, name)
37+
38+
def valid_name(self, name):
39+
return self._is_included(name) and not self._is_excluded(name)

0 commit comments

Comments
 (0)