B
    ?F[8                 @   s   d dl mZ d dlmZ d dlmZ G dd deZG dd deZd dl	Z	d d	l
mZ G d
d deZd dlmZ G dd deZdS )    )print_function)SelectResultSet)sixc               @   s   e Zd Zd)ddZdd Zdd Zdd	 Zd
d Zdd Zd*ddZ	d+ddZ
d,ddZd-ddZdd Zd.ddZd/ddZdd  Zd!d" Zd0d#d$Zd%d& Zd'd( ZdS )1DomainNc             C   s   || _ || _d | _d S )N)
connectionname	_metadata)selfr   r    r
   .lib/python3.7/site-packages/boto/sdb/domain.py__init__    s    zDomain.__init__c             C   s
   d| j  S )Nz	Domain:%s)r   )r	   r
   r
   r   __repr__%   s    zDomain.__repr__c             C   s   t | d| j S )NzSELECT * FROM `%s`)iterselectr   )r	   r
   r
   r   __iter__(   s    zDomain.__iter__c             C   s   d S )Nr
   )r	   r   attrsr   r
   r
   r   startElement+   s    zDomain.startElementc             C   s    |dkr|| _ nt| || d S )NZ
DomainName)r   setattr)r	   r   valuer   r
   r
   r   
endElement.   s    zDomain.endElementc             C   s   | j s| j| | _ | j S )N)r   r   Zdomain_metadata)r	   r
   r
   r   get_metadata4   s    zDomain.get_metadataTc             C   s   | j | ||||S )a  
        Store attributes for a given item.

        :type item_name: string
        :param item_name: The name of the item whose attributes are being stored.

        :type attribute_names: dict or dict-like object
        :param attribute_names: The name/value pairs to store as attributes

        :type expected_value: list
        :param expected_value: If supplied, this is a list or tuple consisting
            of a single attribute name and expected value. The list can be
            of the form:

             * ['name', 'value']

            In which case the call will first verify that the attribute
            "name" of this item has a value of "value".  If it does, the delete
            will proceed, otherwise a ConditionalCheckFailed error will be
            returned. The list can also be of the form:

             * ['name', True|False]

            which will simply check for the existence (True) or non-existence
            (False) of the attribute.

        :type replace: bool
        :param replace: Whether the attribute values passed in will replace
                        existing values or will be added as addition values.
                        Defaults to True.

        :rtype: bool
        :return: True if successful
        )r   put_attributes)r	   	item_name
attributesreplaceZexpected_valuer
   r
   r   r   9   s    $zDomain.put_attributesc             C   s   | j | ||S )a  
        Store attributes for multiple items.

        :type items: dict or dict-like object
        :param items: A dictionary-like object.  The keys of the dictionary are
                      the item names and the values are themselves dictionaries
                      of attribute names/values, exactly the same as the
                      attribute_names parameter of the scalar put_attributes
                      call.

        :type replace: bool
        :param replace: Whether the attribute values passed in will replace
                        existing values or will be added as addition values.
                        Defaults to True.

        :rtype: bool
        :return: True if successful
        )r   batch_put_attributes)r	   itemsr   r
   r
   r   r   `   s    zDomain.batch_put_attributesFc             C   s   | j | ||||S )aU  
        Retrieve attributes for a given item.

        :type item_name: string
        :param item_name: The name of the item whose attributes are being retrieved.

        :type attribute_names: string or list of strings
        :param attribute_names: An attribute name or list of attribute names.  This
                                parameter is optional.  If not supplied, all attributes
                                will be retrieved for the item.

        :rtype: :class:`boto.sdb.item.Item`
        :return: An Item mapping type containing the requested attribute name/values
        )r   get_attributes)r	   r   Zattribute_nameconsistent_readitemr
   r
   r   r   u   s    zDomain.get_attributesc             C   s   | j | |||S )a  
        Delete attributes from a given item.

        :type item_name: string
        :param item_name: The name of the item whose attributes are being deleted.

        :type attributes: dict, list or :class:`boto.sdb.item.Item`
        :param attributes: Either a list containing attribute names which will cause
                           all values associated with that attribute name to be deleted or
                           a dict or Item containing the attribute names and keys and list
                           of values to delete as the value.  If no value is supplied,
                           all attribute name/values for the item will be deleted.

        :type expected_value: list
        :param expected_value: If supplied, this is a list or tuple consisting
            of a single attribute name and expected value. The list can be of
            the form:

             * ['name', 'value']

            In which case the call will first verify that the attribute "name"
            of this item has a value of "value".  If it does, the delete
            will proceed, otherwise a ConditionalCheckFailed error will be
            returned. The list can also be of the form:

             * ['name', True|False]

            which will simply check for the existence (True) or
            non-existence (False) of the attribute.

        :rtype: bool
        :return: True if successful
        )r   delete_attributes)r	   r   r   Zexpected_valuesr
   r
   r   r       s    #zDomain.delete_attributesc             C   s   | j | |S )a  
        Delete multiple items in this domain.

        :type items: dict or dict-like object
        :param items: A dictionary-like object.  The keys of the dictionary are
            the item names and the values are either:

                * dictionaries of attribute names/values, exactly the
                  same as the attribute_names parameter of the scalar
                  put_attributes call.  The attribute name/value pairs
                  will only be deleted if they match the name/value
                  pairs passed in.
                * None which means that all attributes associated
                  with the item should be deleted.

        :rtype: bool
        :return: True if successful
        )r   batch_delete_attributes)r	   r   r
   r
   r   r!      s    zDomain.batch_delete_attributes c             C   s   t | ||||dS )a  
        Returns a set of Attributes for item names within domain_name that match the query.
        The query must be expressed in using the SELECT style syntax rather than the
        original SimpleDB query language.

        :type query: string
        :param query: The SimpleDB query to be performed.

        :rtype: iter
        :return: An iterator containing the results.  This is actually a generator
                 function that will iterate across all search results, not just the
                 first page.
        )	max_items
next_tokenr   )r   )r	   Zqueryr$   r   r#   r
   r
   r   r      s    
zDomain.selectc             C   s$   | j ||d}|r| |_|S dS dS )a  
        Retrieves an item from the domain, along with all of its attributes.

        :param string item_name: The name of the item to retrieve.
        :rtype: :class:`boto.sdb.item.Item` or ``None``
        :keyword bool consistent_read: When set to true, ensures that the most
                                       recent data is returned.
        :return: The requested item, or ``None`` if there was no match found
        )r   N)r   domain)r	   r   r   r   r
   r
   r   get_item   s
    
zDomain.get_itemc             C   s   | j | |S )N)r   Zitem_cls)r	   r   r
   r
   r   new_item   s    zDomain.new_itemc             C   s   |  |j d S )N)r    r   )r	   r   r
   r
   r   delete_item   s    zDomain.delete_itemc             C   s&  |sddl m} | }td|d td| j |d x| D ]}td|j |d x|D ]}td| |d || }t|ts|g}x`|D ]X}tdd	|d
 t|tjr|dd}ntj|dddd}|	| td|d qW td|d qVW td|d q:W td|d |
  |d |S )zGet this domain as an XML DOM Document
        :param f: Optional File to dump directly to
        :type f: File or Stream

        :return: File object where the XML has been dumped to
        :rtype: file
        r   )TemporaryFilez&<?xml version="1.0" encoding="UTF-8"?>)filez<Domain id="%s">z	<Item id="%s">z		<attribute id="%s">z			<value><![CDATA[ )endr*   zutf-8r   )errorsz]]></value>z		</attribute>z	</Item>z	</Domain>)Ztempfiler)   printr   
isinstancelistr   Z	text_typeencodewriteflushseek)r	   fr)   r   kvaluesr   r
   r
   r   to_xml   s2    





zDomain.to_xmlc             C   s"   ddl }t| }|j|| |S )z)Load this domain based on an XML documentr   N)Zxml.saxDomainDumpParserZsaxparse)r	   docZxmlZhandlerr
   r
   r   from_xml  s    zDomain.from_xmlc             C   s   | j | S )z<
        Delete this domain, and all items under it
        )r   Zdelete_domain)r	   r
   r
   r   delete  s    zDomain.delete)NN)TN)T)NFN)NN)r"   NFN)F)N)__name__
__module____qualname__r   r   r   r   r   r   r   r   r   r    r!   r   r&   r'   r(   r8   r<   r=   r
   r
   r
   r   r      s(   

&
 
 
%


$r   c               @   s&   e Zd ZdddZdd Zdd ZdS )	DomainMetaDataNc             C   s.   || _ d | _d | _d | _d | _d | _d | _d S )N)r%   
item_countitem_names_sizeattr_name_countattr_names_sizeattr_value_countattr_values_size)r	   r%   r
   r
   r   r     s    zDomainMetaData.__init__c             C   s   d S )Nr
   )r	   r   r   r   r
   r
   r   r   (  s    zDomainMetaData.startElementc             C   s   |dkrt || _n|dkr(t || _nl|dkr<t || _nX|dkrPt || _nD|dkrdt || _n0|dkrxt || _n|dkr|| _nt| || d S )NZ	ItemCountZItemNamesSizeBytesZAttributeNameCountZAttributeNamesSizeBytesZAttributeValueCountZAttributeValuesSizeBytesZ	Timestamp)	intrB   rC   rD   rE   rF   rG   Z	timestampr   )r	   r   r   r   r
   r
   r   r   +  s    zDomainMetaData.endElement)N)r>   r?   r@   r   r   r   r
   r
   r
   r   rA     s   
	rA   N)ContentHandlerc               @   s0   e Zd ZdZdd Zdd Zdd Zdd	 Zd
S )r9   z6
    SAX parser for a domain that has been dumped
    c             C   s,   t || _d | _i | _d | _d| _|| _d S )Nr"   )UploaderThreaduploaderitem_idr   	attributer   r%   )r	   r%   r
   r
   r   r   D  s    
zDomainDumpParser.__init__c             C   s@   |dkr|d | _ i | _n"|dkr.|d | _n|dkr<d| _d S )NItemidrM   r   r"   )rL   r   rM   r   )r	   r   r   r
   r
   r   r   L  s    
zDomainDumpParser.startElementc             C   s   |  j |7  _ d S )N)r   )r	   Zchr
   r
   r   
charactersU  s    zDomainDumpParser.charactersc             C   s   |dkrR| j r| jr| j  }| j }|| jkrD| j| | q|g| j|< nR|dkr| j| jj| j< t| jjdkr| j	  t
| j| _n|dkr| j	  d S )Nr   rN      r   )r   rM   stripr   appendrK   r   rL   lenstartrJ   r%   )r	   r   r   Z	attr_namer
   r
   r   r   X  s    



zDomainDumpParser.endElementN)r>   r?   r@   __doc__r   r   rP   r   r
   r
   r
   r   r9   ?  s
   	r9   )Threadc                   s(   e Zd ZdZ fddZdd Z  ZS )rJ   zUploader Threadc                s   || _ i | _tt|   d S )N)dbr   superrJ   r   )r	   r%   )	__class__r
   r   r   o  s    zUploaderThread.__init__c          	   C   sf   y| j | j W n8   td x"| jD ]}| j || j|  q*W Y nX tddd tj  d S )Nz5Exception using batch put, trying regular put instead.r+   )r,   )rX   r   r   r.   r   sysstdoutr3   )r	   r   r
   r
   r   runt  s    zUploaderThread.run)r>   r?   r@   rV   r   r^   __classcell__r
   r
   )rZ   r   rJ   l  s   rJ   )Z
__future__r   Zboto.sdb.queryresultsetr   Zboto.compatr   objectr   rA   r\   Zxml.sax.handlerrI   r9   Z	threadingrW   rJ   r
   r
   r
   r   <module>   s      ,