Completed
Push — master ( 3008cd...e7155f )
by Valentin
03:11
created

Request.__eq__()   A

Complexity

Conditions 2

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2.0625

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 2
c 3
b 0
f 0
dl 0
loc 5
ccs 3
cts 4
cp 0.75
crap 2.0625
rs 9.4285
1 1
import sys
2 1
import json
3
4 1
from .traceitem import TraceItem
5 1
from ..nodes import AbstractNode
6 1
from .traceitem import TraceItem
7 1
from ..exceptions import AttributeNotProvided
8 1
from ..utils import SerializableAttributesHolder
9
10 1
if sys.version_info[0] >= 3:
11 1
    basestring = str
12
13 1
class Request(SerializableAttributesHolder):
14
    """Represents a request.
15
    https://github.com/ProjetPP/Documentation/blob/master/module-communication.md#request
16
    """
17 1
    __slots__ = ()
18 1
    _possible_attributes = ('id', 'language', 'tree', 'measures', 'trace', 'response_language')
19
20 1
    def _check_attributes(self, attributes, extra=None):
21 1
        super(Request, self)._check_attributes(attributes)
22 1
        missing_attributes = {'id', 'language', 'tree'} - set(attributes.keys())
23 1
        if missing_attributes:
24
            raise AttributeNotProvided('Missing attributes: %s' % ', '.join(missing_attributes))
25 1
        if not isinstance(attributes['language'], basestring):
26
            raise TypeError('"language" attribute is not a string.')
27 1
        attributes.setdefault('measures', {})
28 1
        attributes.setdefault('trace', [])
29 1
        if 'response_language' not in attributes:
30 1
            attributes['response_language'] = attributes['language']
31 1
        elif not isinstance(attributes['response_language'], basestring):
32
            raise TypeError('"response_language" attribute is not a string.')
33 1
        if not isinstance(attributes['tree'], AbstractNode):
34
            raise TypeError('"tree" attribute is not an AbstractNode.')
35 1
        if not isinstance(attributes['measures'], dict):
36
            raise TypeError('"measures" attribute is not a dict.')
37 1
        if not isinstance(attributes['trace'], list):
38
            raise TypeError('"trace" attribute is not a list.')
39
40 1
    def _parse_attributes(self, attributes):
41 1
        attributes['measures'] = attributes.get('measures', {})
42 1
        attributes['trace'] = \
43
                [x if isinstance(x, TraceItem) else TraceItem.from_dict(x)
44
                 for x in attributes.get('trace', [])]
45 1
        super(Request, self)._parse_attributes(attributes)
46
47 1
    def __eq__(self, other):
48 1
        if not isinstance(other, Request):
49
            return False
50
        else:
51 1
            return super(Request, self).__eq__(other)
52
53 1
    def __hash__(self):
54
        return hash((Request, self._attributes))
55
56 1
    @classmethod
57
    def deserialize_attribute(cls, key, value):
58 1
        if key == 'tree':
59 1
            return AbstractNode.deserialize_attribute(key, value)
60 1
        elif key == 'trace':
61
            return [TraceItem.from_dict(x) for x in value]
62
        else:
63
            return value
64