| 1 | 1 |  | import sys | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 | 1 |  | import json | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 | 1 |  | from ..nodes import AbstractNode | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 | 1 |  | from .traceitem import TraceItem | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 | 1 |  | from ..exceptions import AttributeNotProvided | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 | 1 |  | from ..utils import SerializableAttributesHolder | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 | 1 |  | if sys.version_info[0] >= 3: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 | 1 |  |     basestring = str | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 11 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 12 | 1 |  | class Response(SerializableAttributesHolder): | 
            
                                                        
            
                                    
            
            
                | 13 |  |  |     """Represents a response. | 
            
                                                        
            
                                    
            
            
                | 14 |  |  |     https://github.com/ProjetPP/Documentation/blob/master/module-communication.md#response | 
            
                                                        
            
                                    
            
            
                | 15 |  |  |     """ | 
            
                                                        
            
                                    
            
            
                | 16 | 1 |  |     __slots__ = () | 
            
                                                        
            
                                    
            
            
                | 17 | 1 |  |     _possible_attributes = ('language', 'tree', 'measures', 'trace') | 
            
                                                        
            
                                    
            
            
                | 18 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 19 |  |  |  | 
            
                                                        
            
                                                                    
                                                                                                        
            
            
                | 20 | 1 | View Code Duplication |     def _check_attributes(self, attributes, extra=None): | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                        
            
                                    
            
            
                | 21 | 1 |  |         super(Response, self)._check_attributes(attributes) | 
            
                                                        
            
                                    
            
            
                | 22 | 1 |  |         missing_attributes = {'language', 'tree', 'measures', 'trace'} - 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 |  |         tree = attributes['tree'] | 
            
                                                        
            
                                    
            
            
                | 36 | 1 |  |         if isinstance(tree, dict): | 
            
                                                        
            
                                    
            
            
                | 37 |  |  |             tree = AbstractNode.from_dict(tree) | 
            
                                                        
            
                                    
            
            
                | 38 | 1 |  |         elif isinstance(tree, str): | 
            
                                                        
            
                                    
            
            
                | 39 |  |  |             tree = AbstractNode.from_json(tree) | 
            
                                                        
            
                                    
            
            
                | 40 | 1 |  |         attributes['tree'] = tree | 
            
                                                        
            
                                    
            
            
                | 41 | 1 |  |         attributes['trace'] = \ | 
            
                                                        
            
                                    
            
            
                | 42 |  |  |                 [x if isinstance(x, TraceItem) else TraceItem.from_dict(x) | 
            
                                                        
            
                                    
            
            
                | 43 |  |  |                  for x in attributes['trace']] | 
            
                                                        
            
                                    
            
            
                | 44 | 1 |  |         super(Response, self)._parse_attributes(attributes) | 
            
                                                        
            
                                    
            
            
                | 45 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 46 | 1 |  |     def __eq__(self, other): | 
            
                                                        
            
                                    
            
            
                | 47 | 1 |  |         if not isinstance(other, Response): | 
            
                                                        
            
                                    
            
            
                | 48 |  |  |             return False | 
            
                                                        
            
                                    
            
            
                | 49 |  |  |         else: | 
            
                                                        
            
                                    
            
            
                | 50 | 1 |  |             return super(Response, self).__eq__(other) | 
            
                                                        
            
                                    
            
            
                | 51 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 52 | 1 |  |     def __hash__(self): | 
            
                                                        
            
                                    
            
            
                | 53 |  |  |         return hash((Request, self._attributes)) | 
            
                                                        
            
                                    
            
            
                | 54 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 55 | 1 |  |     @classmethod | 
            
                                                        
            
                                    
            
            
                | 56 |  |  |     def deserialize_attribute(cls, key, value): | 
            
                                                        
            
                                    
            
            
                | 57 | 1 |  |         if key == 'tree': | 
            
                                                        
            
                                    
            
            
                | 58 | 1 |  |             return AbstractNode.deserialize_attribute(key, value) | 
            
                                                        
            
                                    
            
            
                | 59 | 1 |  |         elif key == 'trace': | 
            
                                                        
            
                                    
            
            
                | 60 |  |  |             return [TraceItem.from_dict(x) for x in value] | 
            
                                                        
            
                                    
            
            
                | 61 |  |  |         else: | 
            
                                                        
            
                                    
            
            
                | 62 |  |  |             return value | 
            
                                                        
            
                                    
            
            
                | 63 |  |  |  |