Completed
Pull Request — master (#340)
by Arma
03:46
created

ResultSets.selector()   F

Complexity

Conditions 13

Size

Total Lines 27

Duplication

Lines 0
Ratio 0 %
Metric Value
cc 13
dl 0
loc 27
rs 2.7716

How to fix   Complexity   

Complexity

Complex classes like ResultSets.selector() often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

1
#!/usr/bin/env python
2
3
import libcloud.compute.base as compute_base
4
import libcloud.dns.base as dns_base
5
import libcloud.loadbalancer.base as lb_base
6
7
__all__ = [
8
    'FieldLists',
9
    'ResultSets'
10
]
11
12
13
class FieldLists(object):
14
    """
15
    The lists of fields we want to return for each class
16
    """
17
    NODE = ['id', 'name', 'state', 'public_ips', 'private_ips', 'size', 'image']
18
    NODE_SIZE = ['id', 'name', 'ram', 'disk', 'bandwidth', 'price']
19
    NODE_IMAGE = ['id', 'name']
20
    LOCATION = ['id', 'name', 'country']
21
    NODE_KEY = ['pubkey']
22
    NODE_PASSWORD = ['password', 'generated']
23
    STORAGE_VOLUME = ['id', 'name', 'size', 'state']
24
    VOLUME_SNAPSHOT = ['id', 'size', 'state']
25
    ZONE = ['id', 'domain', 'type', 'ttl']
26
    RECORD = ['id', 'name', 'type', 'data', 'zone']
27
    MEMBER = ['id', 'ip', 'port', 'balancer']
28
    BALANCER = ['id', 'name', 'state', 'port']
29
30
31
class ResultSets(object):
32
33
    def selector(self, output):
34
        if isinstance(output, compute_base.Node):
35
            return self.parse(output, FieldLists.NODE)
36
        elif isinstance(output, compute_base.NodeSize):
37
            return self.parse(output, FieldLists.NODE_SIZE)
38
        elif isinstance(output, compute_base.NodeImage):
39
            return self.parse(output, FieldLists.NODE_IMAGE)
40
        elif isinstance(output, compute_base.NodeLocation):
41
            return self.parse(output, FieldLists.LOCATION)
42
        elif isinstance(output, compute_base.NodeAuthSSHKey):
43
            return self.parse(output, FieldLists.NODE_KEY)
44
        elif isinstance(output, compute_base.NodeAuthPassword):
45
            return self.parse(output, FieldLists.NODE_PASSWORD)
46
        elif isinstance(output, compute_base.StorageVolume):
47
            return self.parse(output, FieldLists.STORAGE_VOLUME)
48
        elif isinstance(output, compute_base.VolumeSnapshot):
49
            return self.parse(output, FieldLists.VOLUME_SNAPSHOT)
50
        elif isinstance(output, dns_base.Zone):
51
            return self.parse(output, FieldLists.ZONE)
52
        elif isinstance(output, dns_base.Record):
53
            return self.parse(output, FieldLists.RECORD)
54
        elif isinstance(output, lb_base.Member):
55
            return self.parse(output, FieldLists.MEMBER)
56
        elif isinstance(output, lb_base.LoadBalancer):
57
            return self.parse(output, FieldLists.BALANCER)
58
        else:
59
            return output
60
61
    def formatter(self, output):
62
        formatted = []
63
        if isinstance(output, list):
64
            for o in output:
65
                formatted.append(self.selector(o))
66
        else:
67
            formatted = self.selector(output)
68
        return formatted
69
70
    def _getval(self, obj, field):
71
        return self.selector(getattr(obj, field))
72
73
    def parse(self, output, field_list):
74
        instance_data = {field: self._getval(output, field) for field in field_list}
75
        return instance_data
76