Passed
Push — develop ( 3d8444...ce4f6d )
by Dean
03:03
created

MethodManager.filter()   B

Complexity

Conditions 6

Size

Total Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 0
Metric Value
dl 0
loc 15
ccs 0
cts 12
cp 0
rs 8
c 0
b 0
f 0
cc 6
crap 42
1
import logging
2
3
log = logging.getLogger(__name__)
4
5
6
class MethodManager(object):
7
    def __init__(self, methods, single=True):
8
        self.methods = [
9
            (m.name, m) for m in methods
10
        ]
11
        self.single = single
12
13
    def filter(self, enabled):
14
        for name, method in self.methods:
15
            if enabled is not None and name not in enabled:
16
                log.debug('Method %r has been disabled', name)
17
                continue
18
19
            if not hasattr(method, 'test'):
20
                log.warn('Method %r is missing a test() method')
21
                continue
22
23
            if not method.test():
24
                log.info("Method %r is not available" % name)
0 ignored issues
show
Coding Style Best Practice introduced by
Specify string format arguments as logging function parameters
Loading history...
25
                continue
26
27
            yield method
28
29
    def start(self, enabled=None):
30
        started = []
31
32
        for method in self.filter(enabled):
33
            obj = method()
34
35
            # Store reference
36
            started.append(obj)
37
38
            if self.single:
39
                break
40
41
        log.info(
42
            'Started %s method(s): %s',
43
            len(started),
44
            ', '.join([m.__class__.__name__ for m in started])
45
        )
46
47
        return started
48