U
    3d                     @   s   d dl Z G dd deZdS )    Nc                   @   sT   e Zd ZdZdddZdd Zdd	 Zd
d Zdd ZdddZ	dd Z
dd ZdS )Itemat  
    A ``dict`` sub-class that serves as an object representation of a
    SimpleDB item. An item in SDB is similar to a row in a relational
    database. Items belong to a :py:class:`Domain <boto.sdb.domain.Domain>`,
    which is similar to a table in a relational database.

    The keys on instances of this object correspond to attributes that are
    stored on the SDB item.

    .. tip:: While it is possible to instantiate this class directly, you may
        want to use the convenience methods on :py:class:`boto.sdb.domain.Domain`
        for that purpose. For example, :py:meth:`boto.sdb.domain.Domain.get_item`.
     Fc                 C   s>   t |  || _|| _|| _d| _d| _d| _| jjj	| _	dS )a5  
        :type domain: :py:class:`boto.sdb.domain.Domain`
        :param domain: The domain that this item belongs to.

        :param str name: The name of this item. This name will be used when
            querying for items using methods like
            :py:meth:`boto.sdb.domain.Domain.get_item`
        NF)
dict__init__domainnameactive
request_idencodingin_attribute
connection	converter)selfr   r   r    r   1/tmp/pip-unpacked-wheel-d7dsrkjd/boto/sdb/item.pyr   &   s    	
zItem.__init__c                 C   s    |dkrd| _ |dd | _d S )N	AttributeTr
   )r   getr
   )r   r   attrsr   r   r   r   startElement8   s    zItem.startElementc                 C   s"   | j dkrd | _ t|S |S d S )Nbase64)r
   r   decodestring)r   valuer   r   r   decode_value>   s    

zItem.decode_valuec                 C   s.  |dkr|  || _n|dkrB| jr4|  || _n|  || _n|dkr| j| krt| | j tsv| | j g| | j< |  |}| jr| j|}| | j | n&|  |}| jr| j|}|| | j< n^|dkrz| j	t
|7  _	W n   Y nX n0|dkr|| _n|dkrd| _nt| || d S )NZItemNameNameValueZBoxUsageZ	RequestIdr   F)r   r   r   Zlast_key
isinstancelistr   decodeappendZ	box_usagefloatr	   setattr)r   r   r   r   r   r   r   
endElementE   s8    




zItem.endElementc                 C   s   | j j| j| d dS )aR  
        Loads or re-loads this item's attributes from SDB.

        .. warning::
            If you have changed attribute values on an Item instance,
            this method will over-write the values if they are different in
            SDB. For any local attributes that don't yet exist in SDB,
            they will be safe.
        )itemN)r   Zget_attributesr   r   r   r   r   loadf   s    
z	Item.loadTc                 C   sZ   | j | j| | |rVg }| D ]}| | dkr|| qt|dkrV| j | j| dS )z
        Saves this item to SDB.

        :param bool replace: If ``True``, delete any attributes on the remote
            SDB item that have a ``None`` value on this object.
        Nr   )r   Zput_attributesr   r   lenZdelete_attributes)r   replaceZ	del_attrsr   r   r   r   saver   s    z	Item.savec                 C   s@   || kr4t | | ts$| | g| |< | | | n|| |< dS )a  
        Helps set or add to attributes on this item. If you are adding a new
        attribute that has yet to be set, it will simply create an attribute
        named ``key`` with your given ``value`` as its value. If you are
        adding a value to an existing attribute, this method will convert the
        attribute to a list (if it isn't already) and append your new value
        to said list.

        For clarification, consider the following interactive session:

        .. code-block:: python

            >>> item = some_domain.get_item('some_item')
            >>> item.has_key('some_attr')
            False
            >>> item.add_value('some_attr', 1)
            >>> item['some_attr']
            1
            >>> item.add_value('some_attr', 2)
            >>> item['some_attr']
            [1, 2]

        :param str key: The attribute to add a value to.
        :param object value: The value to set or append to the attribute.
        N)r   r   r   )r   keyr   r   r   r   	add_value   s
    zItem.add_valuec                 C   s   | j |  dS )z
        Deletes this item in SDB.

        .. note:: This local Python object remains in its current state
            after deletion, this only deletes the remote item in SDB.
        N)r   Zdelete_itemr#   r   r   r   delete   s    zItem.deleteN)r   F)T)__name__
__module____qualname____doc__r   r   r   r!   r$   r'   r)   r*   r   r   r   r   r      s   
!
'r   )r   r   r   r   r   r   r   <module>   s   