<s> import base64 <EOL> import hmac <EOL> import hashlib <EOL> import urllib <EOL> import xml . etree . cElementTree as ET <EOL> from datetime import datetime <EOL> import time <EOL> import dateutil . parser <EOL> import logging <EOL> from twisted . internet . defer import DeferredList <EOL> from . . requestqueuer import RequestQueuer <EOL> from . lib import etree_to_dict , safe_quote_tuple <EOL> LOGGER = logging . getLogger ( "<STR_LIT>" ) <EOL> SDB_NAMESPACE = "<STR_LIT>" <EOL> def base10toN ( num , n ) : <EOL> """<STR_LIT>""" <EOL> num_rep = { <NUM_LIT:10> : '<STR_LIT:a>' , <EOL> <NUM_LIT:11> : '<STR_LIT:b>' , <EOL> <NUM_LIT:12> : '<STR_LIT:c>' , <EOL> <NUM_LIT> : '<STR_LIT:d>' , <EOL> <NUM_LIT> : '<STR_LIT:e>' , <EOL> <NUM_LIT:15> : '<STR_LIT:f>' , <EOL> <NUM_LIT:16> : '<STR_LIT:g>' , <EOL> <NUM_LIT> : '<STR_LIT:h>' , <EOL> <NUM_LIT> : '<STR_LIT:i>' , <EOL> <NUM_LIT> : '<STR_LIT>' , <EOL> <NUM_LIT:20> : '<STR_LIT:k>' , <EOL> <NUM_LIT> : '<STR_LIT:l>' , <EOL> <NUM_LIT> : '<STR_LIT:m>' , <EOL> <NUM_LIT> : '<STR_LIT:n>' , <EOL> <NUM_LIT> : '<STR_LIT:o>' , <EOL> <NUM_LIT> : '<STR_LIT:p>' , <EOL> <NUM_LIT> : '<STR_LIT:q>' , <EOL> <NUM_LIT> : '<STR_LIT:r>' , <EOL> <NUM_LIT> : '<STR_LIT:s>' , <EOL> <NUM_LIT> : '<STR_LIT:t>' , <EOL> <NUM_LIT:30> : '<STR_LIT:u>' , <EOL> <NUM_LIT> : '<STR_LIT:v>' , <EOL> <NUM_LIT:32> : '<STR_LIT:w>' , <EOL> <NUM_LIT> : '<STR_LIT:x>' , <EOL> <NUM_LIT> : '<STR_LIT:y>' , <EOL> <NUM_LIT> : '<STR_LIT:z>' } <EOL> new_num_string = '<STR_LIT>' <EOL> current = num <EOL> while current != <NUM_LIT:0> : <EOL> remainder = current % n <EOL> if <NUM_LIT> > remainder > <NUM_LIT:9> : <EOL> remainder_string = num_rep [ remainder ] <EOL> elif remainder >= <NUM_LIT> : <EOL> remainder_string = '<STR_LIT:(>' + str ( remainder ) + '<STR_LIT:)>' <EOL> else : <EOL> remainder_string = str ( remainder ) <EOL> new_num_string = remainder_string + new_num_string <EOL> current = current / n <EOL> return new_num_string <EOL> def base10to36 ( i ) : <EOL> return base10toN ( i , <NUM_LIT> ) <EOL> def base36to10 ( s ) : <EOL> return int ( s , <NUM_LIT> ) <EOL> def sdb_now ( offset = <NUM_LIT:0> ) : <EOL> """<STR_LIT>""" <EOL> return str ( int ( offset + time . time ( ) ) ) . zfill ( <NUM_LIT:11> ) <EOL> def sdb_now_add ( seconds , offset = <NUM_LIT:0> ) : <EOL> """<STR_LIT>""" <EOL> return str ( int ( offset + time . time ( ) + seconds ) ) . zfill ( <NUM_LIT:11> ) <EOL> def sdb_parse_time ( date_string , offset = <NUM_LIT:0> ) : <EOL> """<STR_LIT>""" <EOL> parsed_time = time . mktime ( dateutil . parser . parse ( date_string ) . timetuple ( ) ) <EOL> return str ( int ( offset + parsed_time ) ) . zfill ( <NUM_LIT:11> ) <EOL> def sdb_latitude ( latitude ) : <EOL> """<STR_LIT>""" <EOL> adjusted = ( <NUM_LIT> + float ( latitude ) ) * <NUM_LIT> <EOL> return str ( int ( adjusted ) ) . zfill ( <NUM_LIT:8> ) <EOL> def sdb_longitude ( longitude ) : <EOL> """<STR_LIT>""" <EOL> adjusted = ( <NUM_LIT> + float ( longitude ) ) * <NUM_LIT> <EOL> return str ( int ( adjusted ) ) . zfill ( <NUM_LIT:8> ) <EOL> class AmazonSDB : <EOL> """<STR_LIT>""" <EOL> host = "<STR_LIT>" <EOL> box_usage = <NUM_LIT:0.0> <EOL> def __init__ ( self , aws_access_key_id , aws_secret_access_key , rq = None ) : <EOL> """<STR_LIT>""" <EOL> if rq is None : <EOL> self . rq = RequestQueuer ( ) <EOL> else : <EOL> self . rq = rq <EOL> self . aws_access_key_id = aws_access_key_id <EOL> self . aws_secret_access_key = aws_secret_access_key <EOL> self . rq . setHostMaxRequestsPerSecond ( self . host , <NUM_LIT:0> ) <EOL> self . rq . setHostMaxSimultaneousRequests ( self . host , <NUM_LIT:0> ) <EOL> def copyDomain ( self , source_domain , destination_domain ) : <EOL> """<STR_LIT>""" <EOL> d = self . checkAndCreateDomain ( destination_domain ) <EOL> d . addCallback ( self . _copyDomainCallback , source_domain , <EOL> destination_domain ) <EOL> return d <EOL> def _copyDomainCallback ( self , data , source_domain , destination_domain ) : <EOL> return self . _copyDomainCallback2 ( source_domain , destination_domain ) <EOL> def _copyDomainCallback2 ( self , source_domain , destination_domain , <EOL> next_token = None , total_box_usage = <NUM_LIT:0> ) : <EOL> parameters = { } <EOL> parameters [ "<STR_LIT>" ] = "<STR_LIT>" <EOL> parameters [ "<STR_LIT>" ] = "<STR_LIT>" % source_domain <EOL> if next_token is not None : <EOL> parameters [ "<STR_LIT>" ] = next_token <EOL> d = self . _request ( parameters ) <EOL> d . addCallback ( self . _copyDomainCallback3 , <EOL> source_domain = source_domain , <EOL> destination_domain = destination_domain , <EOL> total_box_usage = total_box_usage ) <EOL> d . addErrback ( self . _genericErrback ) <EOL> return d <EOL> def _copyDomainCallback3 ( self , data , source_domain , destination_domain , <EOL> total_box_usage = <NUM_LIT:0> ) : <EOL> xml = ET . fromstring ( data [ "<STR_LIT>" ] ) <EOL> box_usage = float ( xml . find ( "<STR_LIT>" % SDB_NAMESPACE ) . text ) <EOL> self . box_usage += box_usage <EOL> total_box_usage += box_usage <EOL> next_token_element = xml . find ( "<STR_LIT>" % SDB_NAMESPACE ) <EOL> if next_token_element is not None : <EOL> next_token = next_token_element . text <EOL> else : <EOL> next_token = None <EOL> items = xml . findall ( "<STR_LIT>" % SDB_NAMESPACE ) <EOL> results = { } <EOL> for item in items : <EOL> key = item . find ( "<STR_LIT>" % SDB_NAMESPACE ) . text <EOL> attributes = item . findall ( "<STR_LIT>" % SDB_NAMESPACE ) <EOL> attribute_dict = { } <EOL> for attribute in attributes : <EOL> attr_name = attribute . find ( "<STR_LIT>" % SDB_NAMESPACE ) . text <EOL> attr_value = attribute . find ( "<STR_LIT>" % SDB_NAMESPACE ) . text <EOL> if attr_name in attribute_dict : <EOL> attribute_dict [ attr_name ] . append ( attr_value ) <EOL> else : <EOL> attribute_dict [ attr_name ] = [ attr_value ] <EOL> results [ key ] = attribute_dict <EOL> deferreds = [ ] <EOL> for key in results : <EOL> d = self . putAttributes ( destination_domain , key , results [ key ] ) <EOL> d . addErrback ( self . _copyPutAttributesErrback , destination_domain , key , results [ key ] ) <EOL> deferreds . append ( d ) <EOL> d = DeferredList ( deferreds , consumeErrors = True ) <EOL> d . addCallback ( self . _copyDomainCallback4 , source_domain , <EOL> destination_domain , next_token = next_token , total_box_usage = total_box_usage ) <EOL> return d <EOL> def _copyDomainCallback4 ( self , data , source_domain , destination_domain , <EOL> next_token = None , total_box_usage = <NUM_LIT:0> ) : <EOL> for row in data : <EOL> if row [ <NUM_LIT:0> ] == False : <EOL> raise row [ <NUM_LIT:1> ] <EOL> if next_token is not None : <EOL> return self . _copyDomainCallback2 ( <EOL> source_domain = source_domain , <EOL> destination_domain = destination_domain , <EOL> next_token = next_token , <EOL> total_box_usage = total_box_usage ) <EOL> LOGGER . debug ( """<STR_LIT>""" % ( <EOL> source_domain , <EOL> destination_domain , <EOL> total_box_usage ) ) <EOL> return True <EOL> def _copyPutAttributesErrback ( self , error , destination_domain , key , attributes , count = <NUM_LIT:0> ) : <EOL> if count < <NUM_LIT:3> : <EOL> d = self . putAttributes ( destination_domain , key , attributes ) <EOL> d . addErrback ( self . _copyPutAttributesErrback , destination_domain , key , attributes , count = count + <NUM_LIT:1> ) <EOL> return d <EOL> return error <EOL> def checkAndCreateDomain ( self , domain ) : <EOL> """<STR_LIT>""" <EOL> d = self . domainMetadata ( domain ) <EOL> d . addErrback ( self . _checkAndCreateDomainErrback , domain ) <EOL> return d <EOL> def _checkAndCreateDomainErrback ( self , error , domain ) : <EOL> if hasattr ( error , "<STR_LIT:value>" ) and hasattr ( error . value , "<STR_LIT:status>" ) : <EOL> if int ( error . value . status ) == <NUM_LIT> : <EOL> d = self . createDomain ( domain ) <EOL> d . addErrback ( self . _checkAndCreateDomainErrback2 , domain ) <EOL> return d <EOL> message = "<STR_LIT>" % domain <EOL> raise Exception ( message ) <EOL> def _checkAndCreateDomainErrback2 ( self , error , domain ) : <EOL> message = "<STR_LIT>" % domain <EOL> raise Exception ( message ) <EOL> def createDomain ( self , domain ) : <EOL> """<STR_LIT>""" <EOL> parameters = { <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : domain <EOL> } <EOL> d = self . _request ( parameters ) <EOL> d . addCallback ( self . _createDomainCallback , domain ) <EOL> d . addErrback ( self . _genericErrback ) <EOL> return d <EOL> def _createDomainCallback ( self , data , domain ) : <EOL> xml = ET . fromstring ( data [ "<STR_LIT>" ] ) <EOL> box_usage = float ( xml . find ( "<STR_LIT>" % SDB_NAMESPACE ) . text ) <EOL> self . box_usage += box_usage <EOL> LOGGER . debug ( "<STR_LIT>" % ( domain , <EOL> box_usage ) ) <EOL> return True <EOL> def deleteDomain ( self , domain ) : <EOL> """<STR_LIT>""" <EOL> parameters = { } <EOL> parameters [ "<STR_LIT>" ] = "<STR_LIT>" <EOL> parameters [ "<STR_LIT>" ] = domain <EOL> d = self . _request ( parameters ) <EOL> d . addCallback ( self . _deleteDomainCallback , domain ) <EOL> d . addErrback ( self . _genericErrback ) <EOL> return d <EOL> def _deleteDomainCallback ( self , data , domain ) : <EOL> xml = ET . fromstring ( data [ "<STR_LIT>" ] ) <EOL> box_usage = float ( xml . find ( "<STR_LIT>" % SDB_NAMESPACE ) . text ) <EOL> self . box_usage += box_usage <EOL> LOGGER . debug ( "<STR_LIT>" % ( domain , <EOL> box_usage ) ) <EOL> return True <EOL> def listDomains ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _listDomains ( ) <EOL> def _listDomains ( self , <EOL> next_token = None , <EOL> previous_results = None , <EOL> total_box_usage = <NUM_LIT:0> ) : <EOL> parameters = { } <EOL> parameters [ "<STR_LIT>" ] = "<STR_LIT>" <EOL> if next_token is not None : <EOL> parameters [ "<STR_LIT>" ] = next_token <EOL> d = self . _request ( parameters ) <EOL> d . addCallback ( self . _listDomainsCallback , <EOL> previous_results = previous_results , <EOL> total_box_usage = total_box_usage ) <EOL> d . addErrback ( self . _genericErrback ) <EOL> return d <EOL> def _listDomainsCallback ( self , <EOL> data , <EOL> previous_results = None , <EOL> total_box_usage = <NUM_LIT:0> ) : <EOL> xml = ET . fromstring ( data [ "<STR_LIT>" ] ) <EOL> box_usage = float ( xml . find ( "<STR_LIT>" % SDB_NAMESPACE ) . text ) <EOL> self . box_usage += box_usage <EOL> total_box_usage += box_usage <EOL> xml_response = etree_to_dict ( xml , namespace = SDB_NAMESPACE ) <EOL> if "<STR_LIT>" in xml_response [ "<STR_LIT>" ] [ <NUM_LIT:0> ] : <EOL> results = xml_response [ "<STR_LIT>" ] [ <NUM_LIT:0> ] [ "<STR_LIT>" ] <EOL> else : <EOL> results = [ ] <EOL> if previous_results is not None : <EOL> results . extend ( previous_results ) <EOL> if "<STR_LIT>" in xml_response [ "<STR_LIT>" ] : <EOL> next_token = xml_response [ "<STR_LIT>" ] [ <NUM_LIT:0> ] [ "<STR_LIT>" ] [ <NUM_LIT:0> ] <EOL> return self . _listDomains ( next_token = next_token , <EOL> previous_results = results , <EOL> total_box_usage = total_box_usage ) <EOL> LOGGER . debug ( "<STR_LIT>" % total_box_usage ) <EOL> return results <EOL> def domainMetadata ( self , domain ) : <EOL> """<STR_LIT>""" <EOL> parameters = { } <EOL> parameters [ "<STR_LIT>" ] = "<STR_LIT>" <EOL> parameters [ "<STR_LIT>" ] = domain <EOL> d = self . _request ( parameters ) <EOL> d . addCallback ( self . _domainMetadataCallback , domain ) <EOL> d . addErrback ( self . _genericErrback ) <EOL> return d <EOL> def _domainMetadataCallback ( self , data , domain ) : <EOL> xml = ET . fromstring ( data [ "<STR_LIT>" ] ) <EOL> box_usage = float ( xml . find ( "<STR_LIT>" % SDB_NAMESPACE ) . text ) <EOL> self . box_usage += box_usage <EOL> LOGGER . debug ( "<STR_LIT>" % ( <EOL> domain , <EOL> box_usage ) ) <EOL> xml_response = etree_to_dict ( xml , namespace = SDB_NAMESPACE ) <EOL> return xml_response [ "<STR_LIT>" ] [ <NUM_LIT:0> ] <EOL> def batchPutAttributes ( self , domain , attributes_by_item_name , <EOL> replace_by_item_name = None ) : <EOL> """<STR_LIT>""" <EOL> if replace_by_item_name is None : <EOL> replace_by_item_name = { } <EOL> if len ( attributes_by_item_name ) > <NUM_LIT> : <EOL> raise Exception ( "<STR_LIT>" ) <EOL> for item_name in replace_by_item_name : <EOL> if not isinstance ( replace_by_item_name [ item_name ] , list ) : <EOL> raise Exception ( "<STR_LIT>" % item_name ) <EOL> for item_name in attributes_by_item_name : <EOL> if not isinstance ( attributes_by_item_name [ item_name ] , dict ) : <EOL> raise Exception ( "<STR_LIT>" % item_name ) <EOL> parameters = { } <EOL> parameters [ "<STR_LIT>" ] = "<STR_LIT>" <EOL> parameters [ "<STR_LIT>" ] = domain <EOL> i = <NUM_LIT:0> <EOL> for item_name in attributes_by_item_name : <EOL> parameters [ "<STR_LIT>" % i ] = item_name <EOL> attributes_list = [ ] <EOL> for attribute in attributes_by_item_name [ item_name ] . items ( ) : <EOL> if isinstance ( attribute [ <NUM_LIT:1> ] , list ) : <EOL> for value in attribute [ <NUM_LIT:1> ] : <EOL> attributes_list . append ( ( attribute [ <NUM_LIT:0> ] , value ) ) <EOL> else : <EOL> attributes_list . append ( attribute ) <EOL> j = <NUM_LIT:0> <EOL> for attribute in attributes_list : <EOL> parameters [ "<STR_LIT>" % ( i , j ) ] = attribute [ <NUM_LIT:0> ] <EOL> parameters [ "<STR_LIT>" % ( i , j ) ] = attribute [ <NUM_LIT:1> ] <EOL> if item_name in replace_by_item_name : <EOL> if attribute [ <NUM_LIT:0> ] in replace_by_item_name [ item_name ] : <EOL> parameters [ "<STR_LIT>" % ( i , j ) ] = "<STR_LIT:true>" <EOL> j += <NUM_LIT:1> <EOL> i += <NUM_LIT:1> <EOL> d = self . _request ( parameters ) <EOL> d . addCallback ( <EOL> self . _batchPutAttributesCallback , <EOL> domain , <EOL> attributes_by_item_name ) <EOL> d . addErrback ( self . _genericErrback ) <EOL> return d <EOL> def _batchPutAttributesCallback ( self , <EOL> data , <EOL> domain , <EOL> attributes_by_item_name ) : <EOL> xml = ET . fromstring ( data [ "<STR_LIT>" ] ) <EOL> box_usage = float ( xml . find ( "<STR_LIT>" % SDB_NAMESPACE ) . text ) <EOL> self . box_usage += box_usage <EOL> LOGGER . debug ( """<STR_LIT>""" % ( <EOL> attributes_by_item_name , <EOL> domain , <EOL> box_usage ) ) <EOL> return True <EOL> def putAttributes ( self , domain , item_name , attributes , replace = None ) : <EOL> """<STR_LIT>""" <EOL> if replace is None : <EOL> replace = [ ] <EOL> if not isinstance ( replace , list ) : <EOL> raise Exception ( "<STR_LIT>" ) <EOL> if not isinstance ( attributes , dict ) : <EOL> raise Exception ( "<STR_LIT>" ) <EOL> parameters = { } <EOL> parameters [ "<STR_LIT>" ] = "<STR_LIT>" <EOL> parameters [ "<STR_LIT>" ] = domain <EOL> parameters [ "<STR_LIT>" ] = item_name <EOL> attributes_list = [ ] <EOL> for attribute in attributes . items ( ) : <EOL> if isinstance ( attribute [ <NUM_LIT:1> ] , list ) : <EOL> for value in attribute [ <NUM_LIT:1> ] : <EOL> attributes_list . append ( ( attribute [ <NUM_LIT:0> ] , value ) ) <EOL> else : <EOL> attributes_list . append ( attribute ) <EOL> i = <NUM_LIT:0> <EOL> for attribute in attributes_list : <EOL> parameters [ "<STR_LIT>" % i ] = attribute [ <NUM_LIT:0> ] <EOL> parameters [ "<STR_LIT>" % i ] = attribute [ <NUM_LIT:1> ] <EOL> if attribute [ <NUM_LIT:0> ] in replace : <EOL> parameters [ "<STR_LIT>" % i ] = "<STR_LIT:true>" <EOL> i += <NUM_LIT:1> <EOL> d = self . _request ( parameters ) <EOL> d . addCallback ( self . _putAttributesCallback , domain , item_name , attributes ) <EOL> d . addErrback ( self . _genericErrback ) <EOL> return d <EOL> def _putAttributesCallback ( self , data , domain , item_name , attributes ) : <EOL> xml = ET . fromstring ( data [ "<STR_LIT>" ] ) <EOL> box_usage = float ( xml . find ( "<STR_LIT>" % SDB_NAMESPACE ) . text ) <EOL> self . box_usage += box_usage <EOL> LOGGER . debug ( """<STR_LIT>""" % ( <EOL> attributes , <EOL> item_name , <EOL> domain , <EOL> box_usage ) ) <EOL> return True <EOL> def getAttributes ( self , domain , item_name , attribute_name = None ) : <EOL> """<STR_LIT>""" <EOL> parameters = { } <EOL> parameters [ "<STR_LIT>" ] = "<STR_LIT>" <EOL> parameters [ "<STR_LIT>" ] = domain <EOL> parameters [ "<STR_LIT>" ] = item_name <EOL> if attribute_name is not None : <EOL> parameters [ "<STR_LIT>" ] = attribute_name <EOL> d = self . _request ( parameters ) <EOL> d . addCallback ( self . _getAttributesCallback , domain , item_name ) <EOL> d . addErrback ( self . _genericErrback ) <EOL> return d <EOL> def _getAttributesCallback ( self , data , domain , item_name ) : <EOL> xml = ET . fromstring ( data [ "<STR_LIT>" ] ) <EOL> box_usage = float ( xml . find ( "<STR_LIT>" % SDB_NAMESPACE ) . text ) <EOL> self . box_usage += box_usage <EOL> LOGGER . debug ( """<STR_LIT>""" % ( <EOL> item_name , <EOL> domain , <EOL> box_usage ) ) <EOL> xml_response = etree_to_dict ( xml , namespace = SDB_NAMESPACE ) <EOL> attributes = { } <EOL> if xml_response [ "<STR_LIT>" ] [ <NUM_LIT:0> ] is None : <EOL> raise Exception ( "<STR_LIT>" ) <EOL> for attribute in xml_response [ "<STR_LIT>" ] [ <NUM_LIT:0> ] [ '<STR_LIT>' ] : <EOL> if attribute [ "<STR_LIT:Name>" ] [ <NUM_LIT:0> ] not in attributes : <EOL> attributes [ attribute [ "<STR_LIT:Name>" ] [ <NUM_LIT:0> ] ] = [ ] <EOL> attributes [ attribute [ "<STR_LIT:Name>" ] [ <NUM_LIT:0> ] ] . extend ( attribute [ "<STR_LIT>" ] ) <EOL> return attributes <EOL> def delete ( self , domain , item_name ) : <EOL> """<STR_LIT>""" <EOL> return self . deleteAttributes ( domain , item_name ) <EOL> def deleteAttributes ( self , domain , item_name , attributes = None ) : <EOL> """<STR_LIT>""" <EOL> if attributes is None : <EOL> attributes = { } <EOL> if not isinstance ( attributes , dict ) and not isinstance ( attributes , list ) : <EOL> message = "<STR_LIT>" <EOL> raise Exception ( message ) <EOL> parameters = { } <EOL> parameters [ "<STR_LIT>" ] = "<STR_LIT>" <EOL> parameters [ "<STR_LIT>" ] = domain <EOL> parameters [ "<STR_LIT>" ] = item_name <EOL> if isinstance ( attributes , dict ) : <EOL> attr_count = <NUM_LIT:1> <EOL> for key in attributes : <EOL> parameters [ "<STR_LIT>" % attr_count ] = key <EOL> parameters [ "<STR_LIT>" % attr_count ] = attributes [ key ] <EOL> attr_count += <NUM_LIT:1> <EOL> if isinstance ( attributes , list ) : <EOL> attr_count = <NUM_LIT:0> <EOL> for key in attributes : <EOL> parameters [ "<STR_LIT>" % attr_count ] = key <EOL> attr_count += <NUM_LIT:1> <EOL> d = self . _request ( parameters ) <EOL> d . addCallback ( self . _deleteAttributesCallback , domain , item_name ) <EOL> d . addErrback ( self . _genericErrback ) <EOL> return d <EOL> def _deleteAttributesCallback ( self , data , domain , item_name ) : <EOL> xml = ET . fromstring ( data [ "<STR_LIT>" ] ) <EOL> box_usage = float ( xml . find ( "<STR_LIT>" % SDB_NAMESPACE ) . text ) <EOL> self . box_usage += box_usage <EOL> LOGGER . debug ( """<STR_LIT>""" % ( <EOL> item_name , <EOL> domain , <EOL> box_usage ) ) <EOL> return True <EOL> def select ( self , select_expression , max_results = <NUM_LIT:0> ) : <EOL> """<STR_LIT>""" <EOL> if "<STR_LIT>" in select_expression . lower ( ) : <EOL> return self . _selectCount ( select_expression ) <EOL> return self . _select ( select_expression , max_results = max_results ) <EOL> def _selectCount ( self , select_expression , next_token = None , <EOL> previous_count = <NUM_LIT:0> , <EOL> total_box_usage = <NUM_LIT:0> ) : <EOL> parameters = { } <EOL> parameters [ "<STR_LIT>" ] = "<STR_LIT>" <EOL> parameters [ "<STR_LIT>" ] = select_expression <EOL> if next_token is not None : <EOL> parameters [ "<STR_LIT>" ] = next_token <EOL> d = self . _request ( parameters ) <EOL> d . addCallback ( self . _selectCountCallback , <EOL> select_expression = select_expression , <EOL> previous_count = previous_count , <EOL> total_box_usage = total_box_usage ) <EOL> d . addErrback ( self . _genericErrback ) <EOL> return d <EOL> def _selectCountCallback ( self , data , select_expression = None , <EOL> previous_count = <NUM_LIT:0> , <EOL> total_box_usage = <NUM_LIT:0> ) : <EOL> xml = ET . fromstring ( data [ "<STR_LIT>" ] ) <EOL> box_usage = float ( xml . find ( "<STR_LIT>" % SDB_NAMESPACE ) . text ) <EOL> self . box_usage += box_usage <EOL> total_box_usage += box_usage <EOL> next_token_element = xml . find ( "<STR_LIT>" % SDB_NAMESPACE ) <EOL> if next_token_element is not None : <EOL> next_token = next_token_element . text <EOL> else : <EOL> next_token = None <EOL> count = previous_count + int ( xml . find ( "<STR_LIT>" % SDB_NAMESPACE ) . text ) <EOL> if next_token is not None : <EOL> return self . _selectCount ( select_expression , next_token = next_token , <EOL> previous_count = count , <EOL> total_box_usage = total_box_usage ) <EOL> LOGGER . debug ( """<STR_LIT>""" % ( <EOL> select_expression , <EOL> total_box_usage ) ) <EOL> return count <EOL> def _select ( self , select_expression , next_token = None , <EOL> previous_results = None , <EOL> total_box_usage = <NUM_LIT:0> , <EOL> max_results = <NUM_LIT:0> ) : <EOL> parameters = { } <EOL> parameters [ "<STR_LIT>" ] = "<STR_LIT>" <EOL> parameters [ "<STR_LIT>" ] = select_expression <EOL> if next_token is not None : <EOL> parameters [ "<STR_LIT>" ] = next_token <EOL> d = self . _request ( parameters ) <EOL> d . addCallback ( self . _selectCallback , <EOL> select_expression = select_expression , <EOL> previous_results = previous_results , <EOL> total_box_usage = total_box_usage , <EOL> max_results = max_results ) <EOL> d . addErrback ( self . _genericErrback ) <EOL> return d <EOL> def _selectCallback ( self , data , select_expression = None , <EOL> previous_results = None , <EOL> total_box_usage = <NUM_LIT:0> , <EOL> max_results = <NUM_LIT:0> ) : <EOL> if previous_results is not None : <EOL> results = previous_results <EOL> else : <EOL> results = { } <EOL> xml = ET . fromstring ( data [ "<STR_LIT>" ] ) <EOL> box_usage = float ( xml . find ( "<STR_LIT>" % SDB_NAMESPACE ) . text ) <EOL> self . box_usage += box_usage <EOL> total_box_usage += box_usage <EOL> next_token_element = xml . find ( "<STR_LIT>" % SDB_NAMESPACE ) <EOL> if next_token_element is not None : <EOL> next_token = next_token_element . text <EOL> else : <EOL> next_token = None <EOL> items = xml . findall ( "<STR_LIT>" % SDB_NAMESPACE ) <EOL> for item in items : <EOL> key = item . find ( "<STR_LIT>" % SDB_NAMESPACE ) . text <EOL> attributes = item . findall ( "<STR_LIT>" % SDB_NAMESPACE ) <EOL> attribute_dict = { } <EOL> for attribute in attributes : <EOL> attr_name = attribute . find ( "<STR_LIT>" % SDB_NAMESPACE ) . text <EOL> attr_value = attribute . find ( "<STR_LIT>" % SDB_NAMESPACE ) . text <EOL> if attr_name in attribute_dict : <EOL> attribute_dict [ attr_name ] . append ( attr_value ) <EOL> else : <EOL> attribute_dict [ attr_name ] = [ attr_value ] <EOL> results [ key ] = attribute_dict <EOL> if next_token is not None : <EOL> if max_results == <NUM_LIT:0> or len ( results ) < max_results : <EOL> return self . _select ( select_expression , next_token = next_token , <EOL> previous_results = results , <EOL> total_box_usage = total_box_usage , <EOL> max_results = max_results ) <EOL> LOGGER . debug ( """<STR_LIT>""" % ( <EOL> select_expression , <EOL> total_box_usage ) ) <EOL> return results <EOL> def _request ( self , parameters ) : <EOL> """<STR_LIT>""" <EOL> parameters = self . _getAuthorization ( "<STR_LIT:GET>" , parameters ) <EOL> query_string = urllib . urlencode ( parameters ) <EOL> url = "<STR_LIT>" % ( self . host , query_string ) <EOL> if len ( url ) > <NUM_LIT> : <EOL> del parameters [ '<STR_LIT>' ] <EOL> parameters = self . _getAuthorization ( "<STR_LIT:POST>" , parameters ) <EOL> query_string = urllib . urlencode ( parameters ) <EOL> url = "<STR_LIT>" % ( self . host ) <EOL> d = self . rq . getPage ( url , method = "<STR_LIT:POST>" , postdata = query_string ) <EOL> return d <EOL> else : <EOL> d = self . rq . getPage ( url , method = "<STR_LIT:GET>" ) <EOL> return d <EOL> def _canonicalize ( self , parameters ) : <EOL> """<STR_LIT>""" <EOL> parameters = parameters . items ( ) <EOL> parameters . sort ( lambda x , y : cmp ( x [ <NUM_LIT:0> ] , y [ <NUM_LIT:0> ] ) ) <EOL> return "<STR_LIT:&>" . join ( [ safe_quote_tuple ( x ) for x in parameters ] ) <EOL> def _getAuthorization ( self , method , parameters ) : <EOL> """<STR_LIT>""" <EOL> signature_parameters = { <EOL> "<STR_LIT>" : self . aws_access_key_id , <EOL> "<STR_LIT>" : "<STR_LIT:2>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> '<STR_LIT>' : datetime . utcnow ( ) . isoformat ( ) [ <NUM_LIT:0> : <NUM_LIT> ] + "<STR_LIT>" , <EOL> "<STR_LIT>" : self . aws_access_key_id , <EOL> "<STR_LIT>" : "<STR_LIT>" <EOL> } <EOL> signature_parameters . update ( parameters ) <EOL> query_string = self . _canonicalize ( signature_parameters ) <EOL> string_to_sign = "<STR_LIT>" % { <EOL> "<STR_LIT>" : method , <EOL> "<STR_LIT:host>" : self . host . lower ( ) , <EOL> "<STR_LIT>" : "<STR_LIT:/>" , <EOL> "<STR_LIT>" : query_string , <EOL> } <EOL> args = [ self . aws_secret_access_key , string_to_sign , hashlib . sha256 ] <EOL> signature = base64 . encodestring ( hmac . new ( * args ) . digest ( ) ) . strip ( ) <EOL> signature_parameters . update ( { '<STR_LIT>' : signature } ) <EOL> return signature_parameters <EOL> def _genericErrback ( self , error ) : <EOL> if hasattr ( error , "<STR_LIT:value>" ) : <EOL> if hasattr ( error . value , "<STR_LIT>" ) : <EOL> xml = ET . XML ( error . value . response ) <EOL> try : <EOL> LOGGER . debug ( xml . find ( "<STR_LIT>" ) . text ) <EOL> except Exception , e : <EOL> pass <EOL> return error </s>
