Skip to content

Commit 7eddf84

Browse files
committed
replace class Filter with function get_filter
1 parent f2dda4e commit 7eddf84

File tree

1 file changed

+26
-30
lines changed

1 file changed

+26
-30
lines changed

inoreader/filter.py

Lines changed: 26 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,22 @@
11
import re
22

33

4-
class Filter(object):
5-
def __init__(self, *args, **kwargs):
6-
pass
7-
8-
@classmethod
9-
def from_config(cls, config):
10-
"""
11-
config: {
12-
'type': 'include_any',
13-
'rules': [],
14-
}
15-
"""
16-
cls_map = {
17-
sub_cls.name: sub_cls
18-
for sub_cls in cls.__subclasses__()
19-
}
20-
21-
sub_cls = config['type']
22-
rules = config['rules']
23-
return cls_map[sub_cls](rules)
4+
_FILTERS = {}
245

25-
def validate(self, text):
26-
raise NotImplementedError
276

7+
def register_filter(name, override=False):
8+
def wrap(cls):
9+
global _FILTERS
10+
if name not in _FILTERS or override:
11+
_FILTERS[name] = cls
12+
13+
return cls
2814

29-
class IncludeAnyFilter(Filter):
15+
return wrap
3016

31-
name = 'include_any'
17+
18+
@register_filter('include_any')
19+
class IncludeAnyFilter(object):
3220

3321
def __init__(self, rules):
3422
self.rules = [re.compile(regexp) for regexp in rules]
@@ -41,9 +29,8 @@ def validate(self, text):
4129
return False
4230

4331

44-
class IncludeAllFilter(Filter):
45-
46-
name = 'include_all'
32+
@register_filter('include_all')
33+
class IncludeAllFilter(object):
4734

4835
def __init__(self, rules):
4936
self.rules = [re.compile(regexp) for regexp in rules]
@@ -56,9 +43,8 @@ def validate(self, text):
5643
return True
5744

5845

59-
class ExcludeFilter(Filter):
60-
61-
name = 'exclude'
46+
@register_filter('exclude')
47+
class ExcludeFilter(object):
6248

6349
def __init__(self, rules):
6450
self.rules = [re.compile(regexp) for regexp in rules]
@@ -69,3 +55,13 @@ def validate(self, text):
6955
return False
7056

7157
return True
58+
59+
60+
def get_filter(config):
61+
filter_type = config['type']
62+
if filter_type not in _FILTERS:
63+
raise ValueError("unsupported filter type: {}".format(filter_type))
64+
65+
filter_cls = _FILTERS[filter_type]
66+
params = {k: v for k, v in config.items() if k != 'type'}
67+
return filter_cls(**params)

0 commit comments

Comments
 (0)