-
Notifications
You must be signed in to change notification settings - Fork 2
/
filter_manager.py
56 lines (45 loc) · 1.68 KB
/
filter_manager.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# -*- coding: UTF-8 -*-
# Copyright 2017 Red Hat, Inc.
# Part of clufter project
# Licensed under GPLv2+ (a copy included | http://gnu.org/licenses/gpl-2.0.txt)
"""Filter manager"""
__author__ = "Jan Pokorný <jpokorny @at@ Red Hat .dot. com>"
from logging import getLogger
from .error import ClufterError
from .filter import filters
from .format_manager import FormatManager
from .plugin_registry import PluginManager
from .utils_2to3 import iter_values
log = getLogger(__name__)
class FilterManagerError(ClufterError):
pass
class FilterManager(PluginManager):
"""Class responsible to manage filters and filtering itself"""
_default_registry = filters
@classmethod
def _init_plugins(cls, filters, fmt_mgr=None, **kwargs):
log.debug("Filters before resolving: {0}".format(filters))
if fmt_mgr is None:
fmts = set()
for flt in iter_values(filters):
# XXX composite format
for attr in ('in_format', 'out_format'):
fmts.add(getattr(flt, attr))
fmt_mgr = FormatManager.init_lookup(fmts, **kwargs)
return cls._resolve(fmt_mgr.formats, filters)
@staticmethod
def _resolve(formats, filters):
for flt_name, flt_cls in tuple(filters.items()):
ret = flt_cls(formats)
if ret is not None:
filters[flt_name] = ret
else:
filters.pop(flt_name)
return filters
@property
def filters(self):
return self._plugins
def __call__(self, which, in_decl, **kwargs):
flt = self._plugins[which]
in_obj = flt.in_format.as_instance(*in_decl)
return flt(in_obj, **kwargs)