Completed
Pull Request — master (#460)
by Manas
02:32
created

ResultSets.formatter()   A

Complexity

Conditions 3

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %
Metric Value
cc 3
dl 0
loc 8
rs 9.4285
1
from pytesla import Vehicle
2
3
__all__ = [
4
    'FieldLists',
5
    'ResultSets'
6
]
7
8
9
class FieldLists(object):
10
    """
11
    The lists of fields we want to return for each class
12
    """
13
    VEHICLE = ['id', 'vin', 'mobile_enabled',
14
               'charge_state', 'climate_state',
15
               'drive_state', 'vehicle_state']
16
17
18
class ResultSets(object):
19
20
    def selector(self, output):
21
        if isinstance(output, Vehicle):
22
            return self.parse(output, FieldLists.VEHICLE)
23
        else:
24
            return output
25
26
    def formatter(self, output):
27
        formatted = []
28
        if isinstance(output, list):
29
            for o in output:
30
                formatted.append(self.selector(o))
31
        else:
32
            formatted = self.selector(output)
33
        return formatted
34
35
    def _getval(self, obj, field):
36
        return self.selector(getattr(obj, field))
37
38
    def parse(self, output, field_list):
39
        instance_data = {field: self._getval(output, field)
40
                         for field in field_list}
41
        return instance_data
42