Exemplo n.º 1
0
    def serialize(self, ctx, message):
        assert message in (self.REQUEST, self.RESPONSE)

        self.event_manager.fire_event("before_serialize", ctx)

        if ctx.out_error is not None:
            ctx.out_document = [ctx.out_error.to_dict(ctx.out_error)]

        else:
            # get the result message
            if message is self.REQUEST:
                out_type = ctx.descriptor.in_message
            elif message is self.RESPONSE:
                out_type = ctx.descriptor.out_message
            if out_type is None:
                return

            out_type_info = out_type._type_info

            # instantiate the result message
            out_instance = out_type()

            # assign raw result to its wrapper, result_message
            for i in range(len(out_type_info)):
                attr_name = out_type_info.keys()[i]
                setattr(out_instance, attr_name, ctx.out_object[i])

            # strip the wrappers if asked for
            out_type, out_instance = unwrap_instance(out_type, out_instance, self.skip_depth)

            ctx.out_document = self._object_to_doc(out_type, out_instance)
            self.event_manager.fire_event("after_serialize", ctx)
Exemplo n.º 2
0
    def _object_to_doc(cls, class_, value, wrapper_name=None, skip_depth=0):
        # strip the wrappers if asked for
        class_, value, skips_left = unwrap_instance(class_, value, skip_depth)

        # arrays get wrapped in [], whereas other objects get wrapped in
        # {wrapper_name: ...}
        if wrapper_name is None and not issubclass(class_, Array):
            wrapper_name = class_.get_type_name()

        # transform the results into a dict:
        if class_.Attributes.max_occurs > 1:
            retval = (cls._to_value(class_, inst, wrapper_name) for inst in value)
        else:
            retval = cls._to_value(class_, value, wrapper_name)

            for _ in range(skips_left):
                if isinstance(retval, dict):
                    _retval = iter(retval.values()).next()
                    if not isinstance(_retval, dict):
                        return retval.values()
                    else:
                        retval = _retval
                else:
                    retval = iter(retval).next()

            if not isinstance(retval, (list,tuple)):
                retval = retval,

        return retval
Exemplo n.º 3
0
    def _object_to_doc(cls, class_, value, wrapper_name=None, skip_depth=0):
        # strip the wrappers if asked for
        class_, value = unwrap_instance(class_, value, skip_depth)

        # arrays get wrapped in [], whereas other objects get wrapped in
        # {object_name: ...}
        if wrapper_name is None and not issubclass(class_, Array):
            wrapper_name = class_.get_type_name()

        # transform the results into a dict:
        if class_.Attributes.max_occurs > 1:
            return (cls._to_value(class_, inst, wrapper_name) for inst in value)
        else:
            return [cls._to_value(class_, value, wrapper_name)]
Exemplo n.º 4
0
    def _object_to_doc(cls, class_, value, wrapper_name=None, skip_depth=0):
        # strip the wrappers if asked for
        class_, value = unwrap_instance(class_, value, skip_depth)

        # arrays get wrapped in [], whereas other objects get wrapped in
        # {wrapper_name: ...}
        if wrapper_name is None and not issubclass(class_, Array):
            wrapper_name = class_.get_type_name()

        # transform the results into a dict:
        if class_.Attributes.max_occurs > 1:
            return (cls._to_value(class_, inst, wrapper_name) for inst in value)
        else:
            return [cls._to_value(class_, value, wrapper_name)]
Exemplo n.º 5
0
    def serialize(self, ctx, message):
        assert message in (self.REQUEST, self.RESPONSE)

        self.event_manager.fire_event('before_serialize', ctx)

        if ctx.out_error is not None:
            ctx.out_document = [ctx.out_error.to_dict(ctx.out_error)]

        else:
            # get the result message
            if message is self.REQUEST:
                out_type = ctx.descriptor.in_message
            elif message is self.RESPONSE:
                out_type = ctx.descriptor.out_message
            if out_type is None:
                return

            out_type_info = out_type._type_info

            # instantiate the result message
            out_instance = out_type()

            # assign raw result to its wrapper, result_message
            for i in range(len(out_type_info)):
                attr_name = out_type_info.keys()[i]
                setattr(out_instance, attr_name, ctx.out_object[i])

            # strip the wrappers if asked for
            out_type, out_instance = unwrap_instance(out_type, out_instance,
                                                                self.skip_depth)

            # arrays get wrapped in [], whereas other objects get wrapped in
            # {object_name: ...}
            wrapper_name = None
            if not issubclass(out_type, Array):
                wrapper_name = out_type.get_type_name()

            # transform the results into a dict:
            if out_type.Attributes.max_occurs > 1:
                ctx.out_document = (self._to_value(out_type, inst, wrapper_name)
                                                       for inst in out_instance)
            else:
                ctx.out_document = [self._to_value(out_type, out_instance, wrapper_name)]

            self.event_manager.fire_event('after_serialize', ctx)