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

MethodManager   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 12
dl 0
loc 42
ccs 0
cts 25
cp 0
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __init__() 0 5 2
B filter() 0 15 6
A start() 0 19 4
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