1 | 1 | import sys |
|
2 | 1 | import json |
|
3 | |||
4 | 1 | from ..nodes import AbstractNode |
|
5 | 1 | from ..exceptions import AttributeNotProvided |
|
6 | 1 | from ..utils import SerializableAttributesHolder |
|
7 | |||
8 | 1 | if sys.version_info[0] >= 3: |
|
9 | 1 | basestring = str |
|
10 | |||
11 | 1 | class TraceItem(SerializableAttributesHolder): |
|
12 | """Represents a trace item. |
||
13 | https://github.com/ProjetPP/Documentation/blob/master/module-communication.md#format-of-a-trace-item |
||
14 | """ |
||
15 | 1 | __slots__ = () |
|
16 | 1 | _possible_attributes = ('module', 'tree', 'measures', 'times') |
|
17 | |||
18 | 1 | View Code Duplication | def _check_attributes(self, attributes, extra=None): |
0 ignored issues
–
show
Duplication
introduced
by
![]() |
|||
19 | 1 | super(TraceItem, self)._check_attributes(attributes) |
|
20 | # Allow missing 'time' attribute for now (transitioning) |
||
21 | 1 | missing_attributes = {'module', 'tree', 'measures'} - set(attributes.keys()) |
|
22 | 1 | if missing_attributes: |
|
23 | raise AttributeNotProvided('Missing attributes: %s' % ', '.join(missing_attributes)) |
||
24 | 1 | if not isinstance(attributes['module'], basestring): |
|
25 | raise TypeError('"module" attribute is not a string.') |
||
26 | 1 | if not isinstance(attributes['tree'], AbstractNode): |
|
27 | raise TypeError('"tree" attribute is not an AbstractNode.') |
||
28 | 1 | if not isinstance(attributes['measures'], dict): |
|
29 | raise TypeError('"measures" attribute is not a dict.') |
||
30 | 1 | if 'times' in attributes and not isinstance(attributes['times'], dict): |
|
31 | raise TypeError('"times" attribute is not a dict.') |
||
32 | |||
33 | 1 | def _parse_attributes(self, attributes): |
|
34 | # Allow missing 'time' attribute for now (transitioning) |
||
35 | 1 | attributes.setdefault('times', {}) |
|
36 | 1 | super(TraceItem, self)._parse_attributes(attributes) |
|
37 | |||
38 | 1 | def __eq__(self, other): |
|
39 | 1 | if not isinstance(other, TraceItem): |
|
40 | return False |
||
41 | else: |
||
42 | 1 | return super(TraceItem, self).__eq__(other) |
|
43 | |||
44 | 1 | def __hash__(self): |
|
45 | return hash((TraceItem, self._attributes)) |
||
46 | |||
47 | 1 | @classmethod |
|
48 | def deserialize_attribute(cls, key, value): |
||
49 | 1 | if key == 'tree': |
|
50 | 1 | return AbstractNode.deserialize_attribute(key, value) |
|
51 | else: |
||
52 | return value |
||
53 |