<s> from __future__ import print_function <EOL> """<STR_LIT>""" <EOL> from boto . sdb . queryresultset import SelectResultSet <EOL> from boto . compat import six <EOL> class Domain ( object ) : <EOL> def __init__ ( self , connection = None , name = None ) : <EOL> self . connection = connection <EOL> self . name = name <EOL> self . _metadata = None <EOL> def __repr__ ( self ) : <EOL> return '<STR_LIT>' % self . name <EOL> def __iter__ ( self ) : <EOL> return iter ( self . select ( "<STR_LIT>" % self . name ) ) <EOL> def startElement ( self , name , attrs , connection ) : <EOL> return None <EOL> def endElement ( self , name , value , connection ) : <EOL> if name == '<STR_LIT>' : <EOL> self . name = value <EOL> else : <EOL> setattr ( self , name , value ) <EOL> def get_metadata ( self ) : <EOL> if not self . _metadata : <EOL> self . _metadata = self . connection . domain_metadata ( self ) <EOL> return self . _metadata <EOL> def put_attributes ( self , item_name , attributes , <EOL> replace = True , expected_value = None ) : <EOL> """<STR_LIT>""" <EOL> return self . connection . put_attributes ( self , item_name , attributes , <EOL> replace , expected_value ) <EOL> def batch_put_attributes ( self , items , replace = True ) : <EOL> """<STR_LIT>""" <EOL> return self . connection . batch_put_attributes ( self , items , replace ) <EOL> def get_attributes ( self , item_name , attribute_name = None , <EOL> consistent_read = False , item = None ) : <EOL> """<STR_LIT>""" <EOL> return self . connection . get_attributes ( self , item_name , attribute_name , <EOL> consistent_read , item ) <EOL> def delete_attributes ( self , item_name , attributes = None , <EOL> expected_values = None ) : <EOL> """<STR_LIT>""" <EOL> return self . connection . delete_attributes ( self , item_name , attributes , <EOL> expected_values ) <EOL> def batch_delete_attributes ( self , items ) : <EOL> """<STR_LIT>""" <EOL> return self . connection . batch_delete_attributes ( self , items ) <EOL> def select ( self , query = '<STR_LIT>' , next_token = None , consistent_read = False , max_items = None ) : <EOL> """<STR_LIT>""" <EOL> return SelectResultSet ( self , query , max_items = max_items , next_token = next_token , <EOL> consistent_read = consistent_read ) <EOL> def get_item ( self , item_name , consistent_read = False ) : <EOL> """<STR_LIT>""" <EOL> item = self . get_attributes ( item_name , consistent_read = consistent_read ) <EOL> if item : <EOL> item . domain = self <EOL> return item <EOL> else : <EOL> return None <EOL> def new_item ( self , item_name ) : <EOL> return self . connection . item_cls ( self , item_name ) <EOL> def delete_item ( self , item ) : <EOL> self . delete_attributes ( item . name ) <EOL> def to_xml ( self , f = None ) : <EOL> """<STR_LIT>""" <EOL> if not f : <EOL> from tempfile import TemporaryFile <EOL> f = TemporaryFile ( ) <EOL> print ( '<STR_LIT>' , file = f ) <EOL> print ( '<STR_LIT>' % self . name , file = f ) <EOL> for item in self : <EOL> print ( '<STR_LIT>' % item . name , file = f ) <EOL> for k in item : <EOL> print ( '<STR_LIT>' % k , file = f ) <EOL> values = item [ k ] <EOL> if not isinstance ( values , list ) : <EOL> values = [ values ] <EOL> for value in values : <EOL> print ( '<STR_LIT>' , end = '<STR_LIT:U+0020>' , file = f ) <EOL> if isinstance ( value , six . text_type ) : <EOL> value = value . encode ( '<STR_LIT:utf-8>' , '<STR_LIT:replace>' ) <EOL> else : <EOL> value = six . text_type ( value , errors = '<STR_LIT:replace>' ) . encode ( '<STR_LIT:utf-8>' , '<STR_LIT:replace>' ) <EOL> f . write ( value ) <EOL> print ( '<STR_LIT>' , file = f ) <EOL> print ( '<STR_LIT>' , file = f ) <EOL> print ( '<STR_LIT>' , file = f ) <EOL> print ( '<STR_LIT>' , file = f ) <EOL> f . flush ( ) <EOL> f . seek ( <NUM_LIT:0> ) <EOL> return f <EOL> def from_xml ( self , doc ) : <EOL> """<STR_LIT>""" <EOL> import xml . sax <EOL> handler = DomainDumpParser ( self ) <EOL> xml . sax . parse ( doc , handler ) <EOL> return handler <EOL> def delete ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . connection . delete_domain ( self ) <EOL> class DomainMetaData ( object ) : <EOL> def __init__ ( self , domain = None ) : <EOL> self . domain = domain <EOL> self . item_count = None <EOL> self . item_names_size = None <EOL> self . attr_name_count = None <EOL> self . attr_names_size = None <EOL> self . attr_value_count = None <EOL> self . attr_values_size = None <EOL> def startElement ( self , name , attrs , connection ) : <EOL> return None <EOL> def endElement ( self , name , value , connection ) : <EOL> if name == '<STR_LIT>' : <EOL> self . item_count = int ( value ) <EOL> elif name == '<STR_LIT>' : <EOL> self . item_names_size = int ( value ) <EOL> elif name == '<STR_LIT>' : <EOL> self . attr_name_count = int ( value ) <EOL> elif name == '<STR_LIT>' : <EOL> self . attr_names_size = int ( value ) <EOL> elif name == '<STR_LIT>' : <EOL> self . attr_value_count = int ( value ) <EOL> elif name == '<STR_LIT>' : <EOL> self . attr_values_size = int ( value ) <EOL> elif name == '<STR_LIT>' : <EOL> self . timestamp = value <EOL> else : <EOL> setattr ( self , name , value ) <EOL> import sys <EOL> from xml . sax . handler import ContentHandler <EOL> class DomainDumpParser ( ContentHandler ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , domain ) : <EOL> self . uploader = UploaderThread ( domain ) <EOL> self . item_id = None <EOL> self . attrs = { } <EOL> self . attribute = None <EOL> self . value = "<STR_LIT>" <EOL> self . domain = domain <EOL> def startElement ( self , name , attrs ) : <EOL> if name == "<STR_LIT>" : <EOL> self . item_id = attrs [ '<STR_LIT:id>' ] <EOL> self . attrs = { } <EOL> elif name == "<STR_LIT>" : <EOL> self . attribute = attrs [ '<STR_LIT:id>' ] <EOL> elif name == "<STR_LIT:value>" : <EOL> self . value = "<STR_LIT>" <EOL> def characters ( self , ch ) : <EOL> self . value += ch <EOL> def endElement ( self , name ) : <EOL> if name == "<STR_LIT:value>" : <EOL> if self . value and self . attribute : <EOL> value = self . value . strip ( ) <EOL> attr_name = self . attribute . strip ( ) <EOL> if attr_name in self . attrs : <EOL> self . attrs [ attr_name ] . append ( value ) <EOL> else : <EOL> self . attrs [ attr_name ] = [ value ] <EOL> elif name == "<STR_LIT>" : <EOL> self . uploader . items [ self . item_id ] = self . attrs <EOL> if len ( self . uploader . items ) >= <NUM_LIT:20> : <EOL> self . uploader . start ( ) <EOL> self . uploader = UploaderThread ( self . domain ) <EOL> elif name == "<STR_LIT>" : <EOL> self . uploader . start ( ) <EOL> from threading import Thread <EOL> class UploaderThread ( Thread ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , domain ) : <EOL> self . db = domain <EOL> self . items = { } <EOL> super ( UploaderThread , self ) . __init__ ( ) <EOL> def run ( self ) : <EOL> try : <EOL> self . db . batch_put_attributes ( self . items ) <EOL> except : <EOL> print ( "<STR_LIT>" ) <EOL> for item_name in self . items : <EOL> self . db . put_attributes ( item_name , self . items [ item_name ] ) <EOL> print ( "<STR_LIT:.>" , end = '<STR_LIT:U+0020>' ) <EOL> sys . stdout . flush ( ) </s>
