o
    ȷe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d/d#d$Zd%d& Zd'd( ZdS )0DomainNc                 C   s   || _ || _d | _d S N)
connectionname	_metadata)selfr   r	    r   F/var/www/ideatree/venv/lib/python3.10/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      d S r   r   r   r	   attrsr   r   r   r   startElement+      zDomain.startElementc                 C   s"   |dkr	|| _ d S t| || d S )N
DomainName)r	   setattrr   r	   valuer   r   r   r   
endElement.   s   
zDomain.endElementc                 C   s   | j s
| j| | _ | j S r   )r
   r   domain_metadatar   r   r   r   get_metadata4   s   zDomain.get_metadataTc                 C      | 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replace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   r    )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"   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#   expected_valuesr   r   r   r,      s   #zDomain.delete_attributesc                 C      | 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   queryr2   r*   r1   r   r   r   r      s   
zDomain.selectc                 C   s    | j ||d}|r| |_|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   r.   r   )r   item_clsr   r"   r   r   r   new_item   s   zDomain.new_itemc                 C   s   |  |j d S r   )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 | D ]_}td|j |d |D ]K}td| |d || }t|ts?|g}|D ],}tdd	|d
 t|tjrW|dd}ntj|dddd}|	| td|d qAtd|d q)td|d q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>)tempfiler:   printr	   
isinstancelistr   	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)xml.saxDomainDumpParsersaxparse)r   docxmlhandlerr   r   r   from_xml  s   zDomain.from_xmlc                 C   s   | j | S )z<
        Delete this domain, and all items under it
        )r   delete_domainr   r   r   r   delete  s   zDomain.delete)NN)TN)T)NFN)r0   NFN)Fr   )__name__
__module____qualname__r   r   r   r   r   r   r!   r&   r(   r,   r/   r   r5   r8   r9   rK   rS   rU   r   r   r   r   r      s0    


'

&


$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 r   )r4   
item_countitem_names_sizeattr_name_countattr_names_sizeattr_value_countattr_values_sizer   r4   r   r   r   r     s   
zDomainMetaData.__init__c                 C   r   r   r   r   r   r   r   r   (  r   zDomainMetaData.startElementc                 C   s   |dkrt || _d S |dkrt || _d S |dkr!t || _d S |dkr,t || _d S |dkr7t || _d S |dkrBt || _d S |dkrK|| _d S t| || d S )N	ItemCountItemNamesSizeBytesAttributeNameCountAttributeNamesSizeBytesAttributeValueCountAttributeValuesSizeBytes	Timestamp)	intrZ   r[   r\   r]   r^   r_   	timestampr   r   r   r   r   r   +  s   
zDomainMetaData.endElementr   )rV   rW   rX   r   r   r   r   r   r   r   rY     s    
	rY   N)ContentHandlerc                   @   s0   e Zd ZdZdd Zdd Zdd Zdd	 Zd
S )rM   z6
    SAX parser for a domain that has been dumped
    c                 C   s,   t || _d | _i | _d | _d| _|| _d S )Nr0   )UploaderThreaduploaderitem_idr   	attributer   r4   r`   r   r   r   r   D  s   

zDomainDumpParser.__init__c                 C   sH   |dkr|d | _ i | _d S |dkr|d | _d S |dkr"d| _d S d S )NItemidrn   r   r0   )rm   r   rn   r   )r   r	   r   r   r   r   r   L  s   


zDomainDumpParser.startElementc                 C   s   |  j |7  _ d S r   )r   )r   chr   r   r   
charactersU  s   zDomainDumpParser.charactersc                 C   s   |dkr/| j r+| jr-| j  }| j }|| jv r#| j| | d S |g| j|< d S d S d S |dkrR| j| jj| j< t| jjdkrP| j	  t
| j| _d S d S |dkr]| j	  d S d S )Nr   ro      r   )r   rn   stripr   appendrl   r'   rm   lenstartrk   r4   )r   r	   r   	attr_namer   r   r   r   X  s"   



zDomainDumpParser.endElementN)rV   rW   rX   __doc__r   r   rr   r   r   r   r   r   rM   ?  s    	rM   )Threadc                       s(   e Zd ZdZ fddZdd Z  ZS )rk   zUploader Threadc                    s   || _ i | _tt|   d S r   )dbr'   superrk   r   r`   	__class__r   r   r   o  s   zUploaderThread.__init__c              	   C   s^   z	| j | j W n   td | jD ]}| j || j|  qY tddd tj  d S )Nz5Exception using batch put, trying regular put instead.r<   )r=   )r{   r&   r'   r@   r!   sysstdoutrF   r7   r   r   r   runt  s   
zUploaderThread.run)rV   rW   rX   ry   r   r   __classcell__r   r   r}   r   rk   l  s    rk   )
__future__r   boto.sdb.queryresultsetr   boto.compatr   objectr   rY   r   xml.sax.handlerrj   rM   	threadingrz   rk   r   r   r   r   <module>   s      ,