Completed
Push — master ( a11b7e...3fb325 )
by Tom
8s
created

ppp_datamodel.communication.Request   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Test Coverage

Coverage 75%
Metric Value
dl 0
loc 45
ccs 24
cts 32
cp 0.75
rs 10
wmc 16

5 Methods

Rating   Name   Duplication   Size   Complexity  
B _check_attributes() 0 13 6
A __hash__() 0 2 1
A deserialize_attribute() 0 8 4
A __eq__() 0 5 2
A _parse_attributes() 0 6 3
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')
19
20 1
    def _check_attributes(self, attributes, extra=None):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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
        if not isinstance(attributes['tree'], AbstractNode):
28
            raise TypeError('"tree" attribute is not an AbstractNode.')
29 1
        if not isinstance(attributes['measures'], dict):
30
            raise TypeError('"measures" attribute is not a dict.')
31 1
        if not isinstance(attributes['trace'], list):
32
            raise TypeError('"trace" attribute is not a list.')
33
34 1
    def _parse_attributes(self, attributes):
35 1
        attributes['measures'] = attributes.get('measures', {})
36 1
        attributes['trace'] = \
37
                [x if isinstance(x, TraceItem) else TraceItem.from_dict(x)
38
                 for x in attributes.get('trace', [])]
39 1
        super(Request, self)._parse_attributes(attributes)
40
41 1
    def __eq__(self, other):
42 1
        if not isinstance(other, Request):
43
            return False
44
        else:
45 1
            return super(Request, self).__eq__(other)
46
47 1
    def __hash__(self):
48
        return hash((Request, self._attributes))
49
50 1
    @classmethod
51
    def deserialize_attribute(cls, key, value):
52 1
        if key == 'tree':
53 1
            return AbstractNode.deserialize_attribute(key, value)
54 1
        elif key == 'trace':
55
            return [TraceItem.from_dict(x) for x in value]
56
        else:
57
            return value
58