Example #1
0
    def getEncodableAttributes(self, obj, codec=None):
        """
        Returns a dict of attributes to be encoded or None.

        @param codec: An optional argument that will contain the en/decoder
            instance calling this function.
        @since: 0.5
        """
        if not self._compiled:
            self.compile()

        attrs = {}

        if self.static_attrs:
            for attr in self.static_attrs:
                attrs[attr] = getattr(obj, attr, Undefined)

        if not self.dynamic:
            if self.non_static_encodable_properties:
                for attr in self.non_static_encodable_properties:
                    attrs[attr] = getattr(obj, attr)

            if not attrs:
                attrs = None

            return attrs

        dynamic_props = util.get_properties(obj)

        if not self.shortcut_encode:
            dynamic_props = set(dynamic_props)

            if self.encodable_properties:
                dynamic_props.update(self.encodable_properties)

            if self.static_attrs:
                dynamic_props.difference_update(self.static_attrs)

            if self.exclude_attrs:
                dynamic_props.difference_update(self.exclude_attrs)

        if self.klass is dict:
            for attr in dynamic_props:
                attrs[attr] = obj[attr]
        else:
            for attr in dynamic_props:
                attrs[attr] = getattr(obj, attr)

        if self.proxy_attrs is not None and attrs and codec:
            context = codec.context

            for k, v in attrs.copy().iteritems():
                if k in self.proxy_attrs:
                    attrs[k] = context.getProxyForObject(v)

        if not attrs:
            attrs = None

        return attrs
Example #2
0
    def getEncodableAttributes(self, obj, codec=None):
        """
        Must return a C{dict} of attributes to be encoded, even if its empty.

        @param codec: An optional argument that will contain the encoder
            instance calling this function.
        @since: 0.5
        """
        if not self._compiled:
            self.compile()

        if self.is_dict:
            return dict(obj)

        if self.shortcut_encode and self.dynamic:
            return obj.__dict__.copy()

        attrs = {}

        if self.static_attrs:
            for attr in self.static_attrs:
                try:
                    attrs[attr] = self.getAttribute(obj, attr, codec=codec)
                except AttributeError:
                    attrs[attr] = pyamf.Undefined

        if not self.dynamic:
            if self.non_static_encodable_properties:
                for attr in self.non_static_encodable_properties:
                    attrs[attr] = self.getAttribute(obj, attr, codec=codec)

            return attrs

        dynamic_props = util.get_properties(obj)

        if not self.shortcut_encode:
            dynamic_props = set(dynamic_props)

            if self.encodable_properties:
                dynamic_props.update(self.encodable_properties)

            if self.static_attrs:
                dynamic_props.difference_update(self.static_attrs)

            if self.exclude_attrs:
                dynamic_props.difference_update(self.exclude_attrs)

        for attr in dynamic_props:
            attrs[attr] = self.getAttribute(obj, attr, codec=codec)

        if self.proxy_attrs is not None and attrs and codec:
            context = codec.context

            for k, v in six.iteritems(attrs.copy()):
                if k in self.proxy_attrs:
                    attrs[k] = context.getProxyForObject(v)

        if self.synonym_attrs:
            missing = object()

            for k, v in six.iteritems(self.synonym_attrs):
                value = attrs.pop(k, missing)

                if value is missing:
                    continue

                attrs[v] = value

        return attrs
Example #3
0
    def getEncodableAttributes(self, obj, codec=None):
        """
        Must return a C{dict} of attributes to be encoded, even if its empty.

        @param codec: An optional argument that will contain the encoder
            instance calling this function.
        @since: 0.5
        """
        if not self._compiled:
            self.compile()

        if self.is_dict:
            return dict(obj)

        if self.shortcut_encode and self.dynamic:
            return obj.__dict__.copy()

        attrs = {}

        if self.static_attrs:
            for attr in self.static_attrs:
                try:
                    attrs[attr] = self.getAttribute(obj, attr, codec=codec)
                except AttributeError:
                    attrs[attr] = pyamf.Undefined

        if not self.dynamic:
            if self.non_static_encodable_properties:
                for attr in self.non_static_encodable_properties:
                    attrs[attr] = self.getAttribute(obj, attr, codec=codec)

            return attrs

        dynamic_props = util.get_properties(obj)

        if not self.shortcut_encode:
            dynamic_props = set(dynamic_props)

            if self.encodable_properties:
                dynamic_props.update(self.encodable_properties)

            if self.static_attrs:
                dynamic_props.difference_update(self.static_attrs)

            if self.exclude_attrs:
                dynamic_props.difference_update(self.exclude_attrs)

        for attr in dynamic_props:
            attrs[attr] = self.getAttribute(obj, attr, codec=codec)

        if self.proxy_attrs is not None and attrs and codec:
            context = codec.context

            for k, v in attrs.copy().iteritems():
                if k in self.proxy_attrs:
                    attrs[k] = context.getProxyForObject(v)

        if self.synonym_attrs:
            missing = object()

            for k, v in self.synonym_attrs.iteritems():
                value = attrs.pop(k, missing)

                if value is missing:
                    continue

                attrs[v] = value

        return attrs
Example #4
0
    def getEncodableAttributes(self, obj, codec=None):
        """
        Returns a C{tuple} containing a dict of static and dynamic attributes
        for an object to encode.

        @param codec: An optional argument that will contain the en/decoder
            instance calling this function.
        @since: 0.5
        """
        if not self._compiled:
            self.compile()

        static_attrs = {}
        dynamic_attrs = {}

        if self.static_attrs:
            for attr in self.static_attrs:
                try:
                    static_attrs[attr] = getattr(obj, attr)
                except AttributeError:
                    static_attrs[attr] = Undefined

        if not self.dynamic:
            if self.non_static_encodable_properties:
                for attr in self.non_static_encodable_properties:
                    dynamic_attrs[attr] = getattr(obj, attr)

            if not static_attrs:
                static_attrs = None

            if not dynamic_attrs:
                dynamic_attrs = None

            return static_attrs, dynamic_attrs

        dynamic_props = util.get_properties(obj)

        if not self.shortcut_encode:
            dynamic_props = set(dynamic_props)

            if self.encodable_properties:
                dynamic_props.update(self.encodable_properties)

            if self.static_attrs:
                dynamic_props.difference_update(self.static_attrs)

            if self.exclude_attrs:
                dynamic_props.difference_update(self.exclude_attrs)

        if self.klass is dict:
            for attr in dynamic_props:
                dynamic_attrs[attr] = obj[attr]
        else:
            for attr in dynamic_props:
                dynamic_attrs[attr] = getattr(obj, attr)

        if self.proxy_attrs is not None:
            if static_attrs:
                for k, v in static_attrs.copy().iteritems():
                    if k in self.proxy_attrs:
                        static_attrs[k] = self.getProxiedAttribute(k, v)

            if dynamic_attrs:
                for k, v in dynamic_attrs.copy().iteritems():
                    if k in self.proxy_attrs:
                        dynamic_attrs[k] = self.getProxiedAttribute(k, v)

        if not static_attrs:
            static_attrs = None

        if not dynamic_attrs:
            dynamic_attrs = None

        return static_attrs, dynamic_attrs
Example #5
0
    def getEncodableAttributes(self, obj, codec=None):
        """
        Returns a C{tuple} containing a dict of static and dynamic attributes
        for an object to encode.

        @param codec: An optional argument that will contain the en/decoder
            instance calling this function.
        @since: 0.5
        """
        if not self._compiled:
            self.compile()

        static_attrs = {}
        dynamic_attrs = {}

        if self.static_attrs:
            for attr in self.static_attrs:
                if hasattr(obj, attr):
                    static_attrs[attr] = getattr(obj, attr)
                else:
                    static_attrs[attr] = Undefined

        if not self.dynamic:
            if self.non_static_encodable_properties:
                for attr in self.non_static_encodable_properties:
                    dynamic_attrs[attr] = getattr(obj, attr)

            if not static_attrs:
                static_attrs = None

            if not dynamic_attrs:
                dynamic_attrs = None

            return static_attrs, dynamic_attrs

        dynamic_props = util.get_properties(obj)

        if not self.shortcut_encode:
            dynamic_props = set(dynamic_props)

            if self.encodable_properties:
                dynamic_props.update(self.encodable_properties)

            if self.static_attrs:
                dynamic_props.difference_update(self.static_attrs)

            if self.exclude_attrs:
                dynamic_props.difference_update(self.exclude_attrs)

        if self.klass is dict:
            for attr in dynamic_props:
                dynamic_attrs[attr] = obj[attr]
        else:
            for attr in dynamic_props:
                dynamic_attrs[attr] = getattr(obj, attr)

        if self.proxy_attrs is not None:
            if static_attrs:
                for k, v in static_attrs.copy().iteritems():
                    if k in self.proxy_attrs:
                        static_attrs[k] = self.getProxiedAttribute(k, v)

            if dynamic_attrs:
                for k, v in dynamic_attrs.copy().iteritems():
                    if k in self.proxy_attrs:
                        dynamic_attrs[k] = self.getProxiedAttribute(k, v)

        if not static_attrs:
            static_attrs = None

        if not dynamic_attrs:
            dynamic_attrs = None

        return static_attrs, dynamic_attrs