Completed
Push — master ( 4c7c7c...5d083f )
by
unknown
02:15
created

ResultSets   B

Complexity

Total Complexity 47

Size/Duplication

Total Lines 112
Duplicated Lines 0 %
Metric Value
wmc 47
dl 0
loc 112
rs 8.439

15 Methods

Rating   Name   Duplication   Size   Complexity  
C parseEC2Object() 0 25 7
A parseEC2Zone() 0 3 2
A parseAddress() 0 3 2
A parseRecord() 0 3 2
A parseR53Zone() 0 3 2
A parseReservation() 0 7 2
A parseVolume() 0 3 2
A parseR53Status() 0 3 2
A parseBucket() 0 3 2
A parseTag() 0 3 2
A __init__() 0 2 1
A parseBlockDeviceType() 0 3 2
A parseInstance() 0 3 2
B formatter() 0 7 5
F selector() 0 25 12

How to fix   Complexity   

Complex Class

Complex classes like ResultSets 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
import boto
2
import six
3
4
5
class FieldLists():
6
    ADDRESS = [
7
        'allocation_id',
8
        'association_id',
9
        'domain',
10
        'instance_id',
11
        'network_interface_id',
12
        'network_interface_owner_id',
13
        'private_ip_address',
14
        'public_ip'
15
    ]
16
17
    BLOCK_DEVICE_TYPE = [
18
        'attach_time',
19
        'delete_on_termination',
20
        'encrypted',
21
        'ephemeral_name',
22
        'iops',
23
        'size',
24
        'snapshot_id',
25
        'status',
26
        'volume_id',
27
        'volume_type'
28
    ]
29
30
    BUCKET = [
31
        'connection',
32
        'creation_date',
33
        'LoggingGroup',
34
        'name'
35
    ]
36
37
    EC2ZONE = [
38
        'messages',
39
        'name',
40
        'region_name',
41
        'state'
42
    ]
43
44
    INSTANCE = [
45
        'ami_launch_index',
46
        'architecture',
47
        'hypervisor',
48
        'id',
49
        'image_id',
50
        'instance_type',
51
        'ip_address',
52
        'kernel',
53
        'key_name',
54
        'launch_time',
55
        'monitored',
56
        'monitoring_state',
57
        'placement',
58
        'placement_group',
59
        'placement_tenancy',
60
        'platform',
61
        'previous_state',
62
        'previous_state_code',
63
        'private_dns_name',
64
        'private_ip_address',
65
        'public_dns_name',
66
        'ramdisk',
67
        'root_device_name',
68
        'root_device_type',
69
        'spot_instance_request_id',
70
        'state',
71
        'state_code',
72
        'state_reason',
73
        'subnet_id',
74
        'tags',
75
        'virtualization_type',
76
        'vpc_id',
77
    ]
78
79
    RECORD = [
80
        'alias_dns_name',
81
        'alias_evaluate_target_health',
82
        'alias_hosted_zone_id',
83
        'failover',
84
        'health_check',
85
        'identifier',
86
        'name',
87
        'region',
88
        'resource_records',
89
        'ttl',
90
        'type',
91
        'weight'
92
    ]
93
94
    R53ZONE = [
95
        'callerreference',
96
        'config',
97
        'id',
98
        'name',
99
        'resourcerecordsetcount'
100
    ]
101
102
    R53STATUS = [
103
        'comment',
104
        'id',
105
        'status',
106
        'submittedat'
107
    ]
108
109
    VOLUME = [
110
        'create_time',
111
        'encrypted',
112
        'id',
113
        'iops',
114
        'size',
115
        'snapshot_id',
116
        'status',
117
        'type',
118
        'zone'
119
    ]
120
121
    TAG = [
122
        'name',
123
        'value',
124
        'res_type',
125
        'res_id'
126
    ]
127
128
129
class ResultSets(object):
130
131
    def __init__(self):
132
        self.foo = ''
133
134
    def selector(self, output):
135
        if isinstance(output, boto.ec2.instance.Reservation):
136
            return self.parseReservation(output)
137
        elif isinstance(output, boto.ec2.instance.Instance):
138
            return self.parseInstance(output)
139
        elif isinstance(output, boto.ec2.volume.Volume):
140
            return self.parseVolume(output)
141
        elif isinstance(output, boto.ec2.blockdevicemapping.BlockDeviceType):
142
            return self.parseBlockDeviceType(output)
143
        elif isinstance(output, boto.ec2.zone.Zone):
144
            return self.parseEC2Zone(output)
145
        elif isinstance(output, boto.ec2.address.Address):
146
            return self.parseAddress(output)
147
        elif isinstance(output, boto.route53.record.Record):
148
            return self.parseRecord(output)
149
        elif isinstance(output, boto.route53.zone.Zone):
150
            return self.parseR53Zone(output)
151
        elif isinstance(output, boto.route53.status.Status):
152
            return self.parseR53Status(output)
153
        elif isinstance(output, boto.ec2.tag.Tag):
154
            return self.parseTag(output)
155
        elif isinstance(output, boto.ec2.ec2object.EC2Object):
156
            return self.parseEC2Object(output)
157
        else:
158
            return output
159
160
    def formatter(self, output):
161
        if isinstance(output, list):
162
            return [self.formatter(item) for item in output]
163
        elif isinstance(output, dict):
164
            return {key: self.formatter(value) for key, value in six.iteritems(output)}
165
        else:
166
            return self.selector(output)
167
168
    def parseReservation(self, output):
169
        instance_list = []
170
        for instance in output.instances:
171
            instance_data = self.parseInstance(instance)
172
            instance_data['owner_id'] = output.owner_id
173
            instance_list.append(instance_data)
174
        return instance_list
175
176
    def parseAddress(self, output):
177
        instance_data = {field: getattr(output, field) for field in FieldLists.ADDRESS}
178
        return instance_data
179
180
    def parseInstance(self, output):
181
        instance_data = {field: getattr(output, field) for field in FieldLists.INSTANCE}
182
        return instance_data
183
184
    def parseVolume(self, output):
185
        volume_data = {field: getattr(output, field) for field in FieldLists.VOLUME}
186
        return volume_data
187
188
    def parseBlockDeviceType(self, output):
189
        data = {field: getattr(output, field) for field in FieldLists.BLOCK_DEVICE_TYPE}
190
        return data
191
192
    def parseEC2Zone(self, output):
193
        zone_data = {field: getattr(output, field) for field in FieldLists.EC2ZONE}
194
        return zone_data
195
196
    def parseRecord(self, output):
197
        record_data = {field: getattr(output, field) for field in FieldLists.RECORD}
198
        return record_data
199
200
    def parseR53Zone(self, output):
201
        zone_data = {field: getattr(output, field) for field in FieldLists.R53ZONE}
202
        return zone_data
203
204
    def parseR53Status(self, output):
205
        status_data = {field: getattr(output, field) for field in FieldLists.R53STATUS}
206
        return status_data
207
208
    def parseBucket(self, output):
209
        bucket_data = {field: getattr(output, field) for field in FieldLists.BUCKET}
210
        return bucket_data
211
212
    def parseTag(self, output):
213
        tag_data = {field: getattr(output, field) for field in FieldLists.TAG}
214
        return tag_data
215
216
    def parseEC2Object(self, output):
217
        # Looks like everything that is an EC2Object pretty much only has these extra
218
        # 'unparseable' properties so handle region and connection specially.
219
        output = vars(output)
220
        del output['connection']
221
        # special handling for region since name here is better than id.
222
        region = output.get('region', None)
223
        output['region'] = region.name if region else ''
224
        # now anything that is an EC2Object get some special marshalling care.
225
        for k, v in six.iteritems(output):
226
            if isinstance(v, boto.ec2.ec2object.EC2Object):
227
                # Better not to assume each EC2Object has an id. If not found
228
                # resort to the str of the object which should have something meaningful.
229
                output[k] = getattr(v, 'id', str(v))
230
            # Generally unmarshallable object might be hiding in list so better to
231
            if isinstance(v, list):
232
                v_list = []
233
                for item in v:
234
                    # avoid touching the basic types.
235
                    if isinstance(item, (basestring, bool, int, long, float)):
0 ignored issues
show
Comprehensibility Best Practice introduced by
Undefined variable 'basestring'
Loading history...
Comprehensibility Best Practice introduced by
Undefined variable 'long'
Loading history...
236
                        v_list.append(v)
237
                    else:
238
                        v_list.append(str(item))
239
                output[k] = v_list
240
        return output
241