| 1 | 1 |  | import sys | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 | 1 |  | import json | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 | 1 |  | from ..nodes import AbstractNode | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 | 1 |  | from ..utils import SerializableAttributesHolder | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 | 1 |  | if sys.version_info[0] >= 3: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 | 1 |  |     basestring = str | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 | 1 |  | class TraceItem(SerializableAttributesHolder): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |     """Represents a trace item. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |     https://github.com/ProjetPP/Documentation/blob/master/module-communication.md#format-of-a-trace-item | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |     """ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 | 1 |  |     __slots__ = () | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 | 1 |  |     _possible_attributes = ('module', 'tree', 'measures', 'times') | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 16 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 17 | 1 |  |     def _check_attributes(self, attributes, extra=None): | 
            
                                                                        
                            
            
                                    
            
            
                | 18 | 1 |  |         super(TraceItem, self)._check_attributes(attributes) | 
            
                                                                        
                            
            
                                    
            
            
                | 19 |  |  |         # Allow missing 'time' attribute for now (transitioning) | 
            
                                                                        
                            
            
                                    
            
            
                | 20 | 1 |  |         assert {'module', 'tree', 'measures'}.issubset(set(attributes.keys())),\ | 
            
                                                                        
                            
            
                                    
            
            
                | 21 |  |  |                 (attributes, extra) | 
            
                                                                        
                            
            
                                    
            
            
                | 22 | 1 |  |         assert isinstance(attributes['module'], basestring), module | 
            
                                                                        
                            
            
                                    
            
            
                | 23 | 1 |  |         assert isinstance(attributes['tree'], AbstractNode) | 
            
                                                                        
                            
            
                                    
            
            
                | 24 | 1 |  |         assert isinstance(attributes['measures'], dict) | 
            
                                                                        
                            
            
                                    
            
            
                | 25 | 1 |  |         assert isinstance(attributes.get('times', {}), dict) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 | 1 |  |     def _parse_attributes(self, attributes): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |         # Allow missing 'time' attribute for now (transitioning) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 | 1 |  |         attributes.setdefault('times', {}) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 | 1 |  |         super(TraceItem, self)._parse_attributes(attributes) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 | 1 |  |     def __eq__(self, other): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 | 1 |  |         if not isinstance(other, TraceItem): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |             return False | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |         else: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 | 1 |  |             return super(TraceItem, self).__eq__(other) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 | 1 |  |     def __hash__(self): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |         return hash((TraceItem, self._attributes)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 | 1 |  |     @classmethod | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |     def deserialize_attribute(cls, key, value): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 | 1 |  |         if key == 'tree': | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 | 1 |  |             return AbstractNode.deserialize_attribute(key, value) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |         else: | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 46 |  |  |             return value | 
            
                                                        
            
                                    
            
            
                | 47 |  |  |  |