def _get_object_serializer(self):
        """Gets the encoder and decoder for an object. Uses the 'properties'
        key to build the encoders and decoders for the specified types,
        mapping them to their specified names and parameters. These include:
            * default  (Default: None)
            * required (Default: true)
            * readonly (Default: false)
            * update   (Default: true)
            * altname  (Default: None)
        """
        if not self._properties:
            raise ValueError('Must specify \'properties\' for \'object\' type')

        fields = {}
        for (name, definition) in six.iteritems(self._properties):
            fields[name] = SchemaField(definition)

        # Allow intraspection of field information via `fields` property
        self._object_fields = fields

        def get_pyname(name, field):
            return (field._altname if field._altname else name)

        def encode(value, fields=fields):
            """Encode an object (dictionary) for the Opsview API. Uses the
            altname for each field schema if specified.
            """
            if not isinstance(value, dict):
                # This is gonna be quite clever... if we're given a value which
                # ISNT a dictionary type, try and encode it anyway by finding
                # the only field it can go in
                required_fields = [
                    (name, field) for (name, field) in six.iteritems(fields)
                    if field._required
                ]

                mutable_fields = [
                    (name, field) for (name, field) in six.iteritems(fields)
                    if not field._readonly
                ]

                field = None
                if len(required_fields) == 1:
                    field = required_fields[0]
                elif len(mutable_fields) == 1:
                    field = mutable_fields[0]

                if field:  # Success!
                    pyname = get_pyname(field[0], field[1])
                    value = {pyname: value}
                else:
                    raise ValueError('Expected object type but got: \'{}\''
                                     .format(value))

            encoded = {}
            allowed_fields = []
            for name, field in six.iteritems(fields):
                if field._altname:
                    allowed_fields.append(field._altname)
                else:
                    allowed_fields.append(name)

            unknown_keys = [name for name in six.iterkeys(value)
                            if name not in allowed_fields]

            if unknown_keys and self._is_strict:
                # Don't allow unknown keys for encoding if strict
                raise ValueError('Unknown fields: \'{}\''
                                 .format('\', \''.join(unknown_keys)))
            else:
                # Otherwise pass the values through as they were specified
                for key in unknown_keys:
                    encoded[key] = value[key]

            for (key, field) in six.iteritems(fields):
                # Only encode if the field has a default value or the field has
                # actually been specified
                pyname = (field._altname if field._altname else key)
                if pyname in value or field._default is not None:
                    encoded[key] = field.encode(value.get(pyname))
                elif field._required:
                    raise ValueError('Missing required field: \'{}\''
                                     .format(pyname))

            return encoded

        def decode(value, fields=fields):
            if not isinstance(value, dict):
                raise ValueError('Cannot decode type {} as object'
                                 .format(type(value)))
            decoded = {}

            unknown_keys = [k for k in six.iterkeys(value) if k not in fields]
            if unknown_keys and self._is_strict:
                raise ValueError('Unknown fields: \'{}\''.format(
                    '\', \''.join(unknown_keys)
                ))
            else:
                for key in unknown_keys:
                    decoded[key] = value[key]

            for (key, field) in six.iteritems(fields):
                pyname = (field._altname if field._altname else key)
                # Only decode if the field has actually been specified
                if key in value:
                    try:
                        decoded[pyname] = field.decode(value.get(key))
                    except ValueError as ex:
                        print key
                        raise

            return decoded

        return (encode, decode)
def _get_object_serializer ( self ) : if not self . _properties : raise ValueError ( 'Must specify \'properties\' for \'object\' type' ) fields = { } for ( name , definition ) in six . iteritems ( self . _properties ) : fields [ name ] = SchemaField ( definition ) self . _object_fields = fields def get_pyname ( name , field ) : return ( field . _altname if field . _altname else name ) def encode ( value , fields = fields ) : if not isinstance ( value , dict ) : required_fields = [ ( name , field ) for ( name , field ) in six . iteritems ( fields ) if field . _required ] mutable_fields = [ ( name , field ) for ( name , field ) in six . iteritems ( fields ) if not field . _readonly ] field = None if len ( required_fields ) == 1 : field = required_fields [ 0 ] elif len ( mutable_fields ) == 1 : field = mutable_fields [ 0 ] if field : pyname = get_pyname ( field [ 0 ] , field [ 1 ] ) value = { pyname : value } else : raise ValueError ( 'Expected object type but got: \'{}\'' . format ( value ) ) encoded = { } allowed_fields = [ ] for name , field in six . iteritems ( fields ) : if field . _altname : allowed_fields . append ( field . _altname ) else : allowed_fields . append ( name ) unknown_keys = [ name for name in six . iterkeys ( value ) if name not in allowed_fields ] if unknown_keys and self . _is_strict : raise ValueError ( 'Unknown fields: \'{}\'' . format ( '\', \'' . join ( unknown_keys ) ) ) else : for key in unknown_keys : encoded [ key ] = value [ key ] for ( key , field ) in six . iteritems ( fields ) : pyname = ( field . _altname if field . _altname else key ) if pyname in value or field . _default is not None : encoded [ key ] = field . encode ( value . get ( pyname ) ) elif field . _required : raise ValueError ( 'Missing required field: \'{}\'' . format ( pyname ) ) return encoded def decode ( value , fields = fields ) : if not isinstance ( value , dict ) : raise ValueError ( 'Cannot decode type {} as object' . format ( type ( value ) ) ) decoded = { } unknown_keys = [ k for k in six . iterkeys ( value ) if k not in fields ] if unknown_keys and self . _is_strict : raise ValueError ( 'Unknown fields: \'{}\'' . format ( '\', \'' . join ( unknown_keys ) ) ) else : for key in unknown_keys : decoded [ key ] = value [ key ] for ( key , field ) in six . iteritems ( fields ) : pyname = ( field . _altname if field . _altname else key ) if key in value : try : decoded [ pyname ] = field . decode ( value . get ( key ) ) except ValueError as ex : print key raise return decoded return ( encode , decode )
Gets the encoder and decoder for an object. Uses the 'properties'
        key to build the encoders and decoders for the specified types,
        mapping them to their specified names and parameters. These include:
            * default  (Default: None)
            * required (Default: true)
            * readonly (Default: false)
            * update   (Default: true)
            * altname  (Default: None)