示例#1
0
    def modify_attribute(self, **kwargs):
        """
        Modify the attribute by key / value pair.
        Add append_lists=True kwarg if dict leaf is a list
        and you want to append, default: replace

        :param dict kwargs: key=value pair to change
        :param bool append_lists: if change is a list, append or overwrite
        :raises ElementNotFound: cannot find element specified
        :raises ModificationFailed, UpdateElementFailed: failure applying
            change with reason
        :return: href of the element modified
        :rtype: str
        """
        if self.data.get('system', False):
            raise ModificationFailed('Cannot modify system element: %s' %
                                     self.name)

        params = {'href': self.href, 'etag': self.etag}
        append_lists = kwargs.pop('append_lists', False)
        merge_dicts(self.data, kwargs, append_lists)
        params.update(json=self.data)
        del self.data

        request = SMCRequest(**params)
        request.exception = UpdateElementFailed
        return request.update().href
示例#2
0
    def update(self, *exception, **kwargs):
        """
        Update the existing element and clear the instance cache.
        Removing the cache will ensure subsequent calls requiring element
        attributes will force a new fetch to obtain the latest copy.
        
        Calling update() with no args will assume the element has already
        been modified directly and the data cache will be used to update.
        You can also override the following attributes: href, etag and
        json. If json is sent, it is expected to the be a complete payload
        to satisfy the update.
        
        For kwargs, if attribute values are a list, you can pass
        'append_lists=True' to add to an existing list, otherwise overwrite
        (default: overwrite)

        .. seealso:: To see different ways to utilize this method for updating,
            see: :ref:`update-elements-label`.

        :param exception: pass a custom exception to throw if failure
        :param kwargs: optional kwargs to update request data to server.
        :raises ModificationFailed: raised if element is tagged as System element
        :raises UpdateElementFailed: failed to update element with reason
        :return: href of the element modified
        :rtype: str
        """
        if self.data.get('system', False):
            raise ModificationFailed('Cannot modify system element: %s' %
                                     self.name)

        if not exception:
            exception = UpdateElementFailed
        else:
            exception = exception[0]

        params = {'href': self.href, 'etag': self.etag}

        if 'href' in kwargs:
            params.update(href=kwargs.pop('href'))

        if 'etag' in kwargs:
            params.update(etag=kwargs.pop('etag'))

        name = kwargs.get('name', None)

        json = kwargs.pop('json') if 'json' in kwargs else self.data
        del self.data  # Delete the cache before processing attributes

        # If kwarg settings are provided AND instance variables, kwargs
        # will overwrite collected instance attributes with the same name.
        if kwargs:
            append_lists = kwargs.pop('append_lists', False)
            merge_dicts(json, kwargs, append_lists)

        params.update(json=json)

        request = SMCRequest(**params)
        request.exception = exception
        result = request.update()

        if name:  # Reset instance name
            self._meta = Meta(name=name, href=self.href, type=self._meta.type)
            self._name = name

        return result.href
示例#3
0
    def update(self, *exception, **kwargs):
        """
        Update the existing element and clear the instance cache.
        Removing the cache will ensure subsequent calls requiring element
        attributes will force a new fetch to obtain the latest copy.

        If attributes are set via kwargs and instance attributes are also
        set, instance attributes are updated first, then kwargs. Typically
        you will want to use either instance attributes OR kwargs, not both.
        
        Calling update() with no args will assume the element has already
        been modified directly and the data cache will be used to update.
        You can also override the following attributes: href, etag and
        json. If json is sent, it is expected to the be a complete payload
        to satisfy the update.
        
        For kwargs, if attribute values are a list, you can pass
        'append_lists=True' to add to an existing list, otherwise overwrite
        (default: overwrite)

        If using instance attributes, the attribute value can be a callable
        and it will be evaluated and merged.

        .. seealso:: To see different ways to utilize this method for updating,
            see: :ref:`update-elements-label`.

        :param exception: pass a custom exception to throw if failure
        :param kwargs: optional kwargs to update request data to server.
        :return: href of the element modified
        :rtype: str
        """
        if not exception:
            exception = UpdateElementFailed
        else:
            exception = exception[0]

        params = {
            'href': self.href,
            'etag': self.etag
        }

        if 'href' in kwargs:
            params.update(href=kwargs.pop('href'))

        if 'etag' in kwargs:
            params.update(etag=kwargs.pop('etag'))

        name = kwargs.get('name', None)
        
        json = kwargs.pop('json') if 'json' in kwargs else self.data
        del self.data       # Delete the cache before processing attributes
        
        instance_attr = {k: v() if callable(v) else v
                         for k, v in vars(self).items()
                         if not k.startswith('_')}
        
        if instance_attr:
            json.update(**instance_attr)

        # If kwarg settings are provided AND instance variables, kwargs
        # will overwrite collected instance attributes with the same name.
        if kwargs:
            append_lists = kwargs.pop('append_lists', False)
            merge_dicts(json, kwargs, append_lists)

        params.update(json=json)
        
        # Remove attributes from instance if previously set
        if instance_attr:
            for attr in instance_attr:
                delattr(self, attr)
        
        request = SMCRequest(**params) 
        request.exception = exception
        result = request.update()
        
        if name: # Reset instance name
            self._meta = Meta(name=name, href=self.href, type=self._meta.type)
            self._name = name
        
        return result.href