kuon.api_response.LockedDict.__init__()   B
last analyzed

Complexity

Conditions 6

Size

Total Lines 10
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 6.3949

Importance

Changes 0
Metric Value
eloc 9
dl 0
loc 10
ccs 7
cts 9
cp 0.7778
rs 8.6666
c 0
b 0
f 0
cc 6
nop 3
crap 6.3949
1
#!/usr/bin/python
2
# -*- coding: utf-8 -*-
3 1
import json
4 1
from collections import MutableSequence
5
6 1
from kuon.exceptions import InvalidApiResponseType
7
8
9 1
class LockedList(MutableSequence):
10
    """Custom list where customizing values isn't possible by default
11
    Return LockedList on list or tuple types
12
    and LockedDict on dict types"""
13
14 1
    def __init__(self, data=None) -> None:
15
        """Initialize the class"""
16
        super(LockedList, self).__init__()
17
        if data is not None:
18
            self._list = list(data)
19
        else:
20
            self._list = list()
21
22
    def __repr__(self) -> str:
23
        return str(self._list)
24
25 1
    def __len__(self) -> int:
26
        return len(self._list)
27
28 1
    def __getitem__(self, ii):
29
        val = self._list[ii]
30
        if isinstance(val, list) or isinstance(val, tuple):
31
            val = LockedList(val)
32
        if isinstance(val, dict):
33
            val = LockedDict(val)
34
        return val
35
36 1
    def __delitem__(self, ii) -> None:
37
        del self._list[ii]
38
39 1
    def __setitem__(self, ii, val) -> None:
40
        pass
41
42 1
    def __str__(self) -> str:
43
        return str(self._list)
44
45 1
    def insert(self, ii, val) -> None:
46
        pass
47
48 1
    def append(self, val) -> None:
49
        pass
50
51
52 1
class LockedDict(dict):
53
    """Custom dict where customizing values isn't possible by default
54
    Return LockedList on list or tuple types
55
    and LockedDict on dict types"""
56
57 1
    def __init__(self, *args, **kwargs) -> None:
58 1
        super(LockedDict, self).__init__(*args, **kwargs)
59 1
        for arg in args:
60 1
            if isinstance(arg, dict):
61 1
                for k, v in arg.items():
62 1
                    self[k] = v
63
64 1
        if kwargs:
65
            for k, v in kwargs.items():
66
                self[k] = v
67
68 1
    def __getattr__(self, attr):
69 1
        val = self.get(attr)
70 1
        if isinstance(val, list) or isinstance(val, tuple):
71
            val = LockedList(val)
72 1
        if isinstance(val, dict):
73 1
            val = LockedDict(val)
74 1
        return val
75
76 1
    def __setattr__(self, key, value) -> None:
77
        pass
78
79 1
    def __setitem__(self, key, value) -> None:
80 1
        pass
81
82 1
    def __delattr__(self, item) -> None:
83
        self.__delitem__(item)
84
85 1
    def __delitem__(self, key) -> None:
86
        super(LockedDict, self).__delitem__(key)
87
        del self.__dict__[key]
88
89
90 1
class APIResponse(LockedDict):
91
    """Wrapper for the json response returned from the APIs of OPSkins and Bitskins
92
    Uses LockedDict and LockedList classes to prevent modifications to the retrieved values"""
93
94 1
    def __init__(self, data) -> None:
95 1
        try:
96 1
            data = json.loads(data)
97
        except ValueError:
98
            raise InvalidApiResponseType("The API didn't return a valid json format", data)
99
100
        super().__init__(data)
101