| 1 |  |  | # -*- coding: utf-8 -*- | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | from __future__ import unicode_literals | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | from builtins import int | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | from abc import ABCMeta, abstractproperty | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | from six import with_metaclass, string_types | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | from .definitions import Enum, APIArgumentSerializer | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | class StringSerializer(APIArgumentSerializer): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |     def to_api(self, python_value): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |         if not isinstance(python_value, string_types): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |             raise ValueError('Invalid value. Expects str or unicode.') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |         return python_value | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |     def to_python(self, api_value): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |         if not isinstance(api_value, string_types): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |             raise ValueError('Invalid value. Expects str or unicode.') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |         return api_value | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  | class IntegerSerializer(APIArgumentSerializer): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |     def to_api(self, python_value): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |         if not isinstance(python_value, int): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |             raise ValueError('Invalid value. Expects int or long.') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |         return str(python_value) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |     def to_python(self, api_value): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |         return int(api_value) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |  | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 32 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 33 |  |  | class PositiveIntegerSerializer(IntegerSerializer): | 
            
                                                                        
                            
            
                                    
            
            
                | 34 |  |  |     def to_api(self, python_value): | 
            
                                                                        
                            
            
                                    
            
            
                | 35 |  |  |         if not isinstance(python_value, int) or python_value < 0: | 
            
                                                                        
                            
            
                                    
            
            
                | 36 |  |  |             raise ValueError('Invalid value. Expects positive int or long.') | 
            
                                                                        
                            
            
                                    
            
            
                | 37 |  |  |         return super(PositiveIntegerSerializer, self).to_api(python_value) | 
            
                                                                        
                            
            
                                    
            
            
                | 38 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 39 |  |  |     def to_python(self, api_value): | 
            
                                                                        
                            
            
                                    
            
            
                | 40 |  |  |         result = super(PositiveIntegerSerializer, self).to_python(api_value) | 
            
                                                                        
                            
            
                                    
            
            
                | 41 |  |  |         if result < 0: | 
            
                                                                        
                            
            
                                    
            
            
                | 42 |  |  |             raise ValueError('Invalid value. Expects positive int or long.') | 
            
                                                                        
                            
            
                                    
            
            
                | 43 |  |  |         return result | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  | class BooleanSerializer(APIArgumentSerializer): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |     def to_api(self, python_value): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |         if not isinstance(python_value, bool): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |             raise ValueError('Invalid value. Expects bool.') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |         return 'true' if python_value else 'false' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |     def to_python(self, api_value): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |         if api_value not in ('true', 'false'): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |             raise ValueError('Invalid value. Expects true|false.') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |         return api_value == 'true' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  | class EnumSerializer(with_metaclass(ABCMeta, StringSerializer)): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |     @abstractproperty | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |     def enum_class(self):  # pragma: no cover | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |         pass | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |     def to_api(self, python_value): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |         assert issubclass(self.enum_class, Enum) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |         if isinstance(python_value, self.enum_class): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |             return python_value.name | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |         elif isinstance(python_value, string_types): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |             return python_value | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |         elif isinstance(python_value, int): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |             return self.enum_class.get_by_value(python_value).name | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |         raise ValueError('Invalid value for enum %s: %s' % (self.enum_class, python_value)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |     def to_python(self, api_value): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |         assert issubclass(self.enum_class, Enum) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |         if not isinstance(api_value, string_types): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |             raise ValueError('Invalid value. Expects str or unicode') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |         result = self.enum_class.get_by_name(api_value) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |         if result is None: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |             raise ValueError('Invalid value. Expects enum value for %s.' % self.enum_class) | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 80 |  |  |         return result | 
            
                                                        
            
                                    
            
            
                | 81 |  |  |  |