o ckF[8@sddlmZ ddlmZddlmZGdddeZGdddeZddl Z dd l m Z Gd d d e Z dd l mZGd ddeZdS))print_functionSelectResultSet)sixc@seZdZd)ddZddZddZdd Zd d Zd d Z d*ddZ d+ddZ  d,ddZ  d)ddZ ddZ d-ddZd.ddZdd Zd!d"Zd/d#d$Zd%d&Zd'd(ZdS)0DomainNcCs||_||_d|_dSN) connectionname _metadata)selfrr r 1/usr/lib/python3/dist-packages/boto/sdb/domain.py__init__ s zDomain.__init__cCs d|jS)Nz Domain:%s)r r r r r __repr__%s zDomain.__repr__cCst|d|jS)NzSELECT * FROM `%s`)iterselectr rr r r __iter__(szDomain.__iter__cCdSrr r r attrsrr r r startElement+zDomain.startElementcCs"|dkr ||_dSt|||dS)N DomainName)r setattrr r valuerr r r endElement.s zDomain.endElementcCs|js |j||_|jSr)r rdomain_metadatarr r r get_metadata4szDomain.get_metadataTcC|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 )rput_attributes)r item_name attributesreplaceexpected_valuer r r r!9s $zDomain.put_attributescCs|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 )rbatch_put_attributes)r itemsr$r r r r&`szDomain.batch_put_attributesFcCr )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 )rget_attributes)r r"attribute_nameconsistent_readitemr r r r(us zDomain.get_attributescCs|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 )rdelete_attributes)r r"r#expected_valuesr r r r,s #zDomain.delete_attributescC|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 )rbatch_delete_attributes)r r'r r r r/szDomain.batch_delete_attributescCst|||||dS)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 queryr2r*r1r r r rs z Domain.selectcCs |j||d}|r||_|SdS)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_items  zDomain.get_itemcCr.r)ritem_clsr r"r r r new_itemszDomain.new_itemcCs||jdSr)r,r )r r+r r r delete_itemszDomain.delete_itemcCs|s ddlm}|}td|dtd|j|d|D]_}td|j|d|D]K}td||d||}t|ts?|g}|D],}tdd |d t|tjrW|d d }n tj|d d d d }| |td|dqAtd|dq)td|dqtd|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&)filezz z z z z z ) tempfiler:printr isinstancelistr text_typeencodewriteflushseek)r fr:r+kvaluesrr r r to_xmls2       z Domain.to_xmlcCs"ddl}t|}|j|||S)z)Load this domain based on an XML documentrN)xml.saxDomainDumpParsersaxparse)r docxmlhandlerr r r from_xmlszDomain.from_xmlcCs |j|S)z< Delete this domain, and all items under it )r delete_domainrr r r deletes z Domain.delete)NN)TN)T)NFN)r0NFN)Fr)__name__ __module__ __qualname__rrrrrrr!r&r(r,r/rr5r8r9rKrSrUr r r r rs0  '  &   $ rc@s&eZdZdddZddZddZdS) DomainMetaDataNcCs.||_d|_d|_d|_d|_d|_d|_dSr)r4 item_countitem_names_sizeattr_name_countattr_names_sizeattr_value_countattr_values_sizer r4r r r rs zDomainMetaData.__init__cCrrr rr r r r(rzDomainMetaData.startElementcCs|dkr t||_dS|dkrt||_dS|dkr!t||_dS|dkr,t||_dS|dkr7t||_dS|dkrBt||_dS|dkrK||_dSt|||dS)N ItemCountItemNamesSizeBytesAttributeNameCountAttributeNamesSizeBytesAttributeValueCountAttributeValuesSizeBytes Timestamp) intrZr[r\r]r^r_ timestamprrr r r r+s zDomainMetaData.endElementr)rVrWrXrrrr r r r rYs  rYN)ContentHandlerc@s0eZdZdZddZddZddZdd Zd S) rMz6 SAX parser for a domain that has been dumped cCs,t||_d|_i|_d|_d|_||_dS)Nr0)UploaderThreaduploaderitem_idr attributerr4r`r r r rDs  zDomainDumpParser.__init__cCsH|dkr|d|_i|_dS|dkr|d|_dS|dkr"d|_dSdS)NItemidrnrr0)rmrrnr)r r rr r r rLs   zDomainDumpParser.startElementcCs|j|7_dSr)r)r chr r r charactersUszDomainDumpParser.characterscCs|dkr/|jr+|jr-|j}|j}||jvr#|j||dS|g|j|<dSdSdS|dkrR|j|jj|j<t|jjdkrP|j t |j |_dSdS|dkr]|j dSdS)Nrror) rrnstriprappendrlr'rmlenstartrkr4)r r r attr_namer r r rXs"     zDomainDumpParser.endElementN)rVrWrX__doc__rrrrrr r r r rM?s  rM)Threadcs(eZdZdZfddZddZZS)rkzUploader Threadcs||_i|_tt|dSr)dbr'superrkrr` __class__r r roszUploaderThread.__init__c Cs^z |j|jWntd|jD] }|j||j|qYtdddtjdS)Nz5Exception using batch put, trying regular put instead.r<)r=)r{r&r'r@r!sysstdoutrFr7r r r runts  zUploaderThread.run)rVrWrXryrr __classcell__r r r}r rkls rk) __future__rboto.sdb.queryresultsetr boto.compatrobjectrrYrxml.sax.handlerrjrM threadingrzrkr r r r s     ,