<s> """<STR_LIT>""" <EOL> from six import string_types , PY2 <EOL> import mimetypes <EOL> from riak . util import lazy_property <EOL> from riak . datatypes import TYPES <EOL> def bucket_property ( name , doc = None ) : <EOL> def _prop_getter ( self ) : <EOL> return self . get_property ( name ) <EOL> def _prop_setter ( self , value ) : <EOL> return self . set_property ( name , value ) <EOL> return property ( _prop_getter , _prop_setter , doc = doc ) <EOL> class RiakBucket ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , client , name , bucket_type ) : <EOL> """<STR_LIT>""" <EOL> if not isinstance ( name , string_types ) : <EOL> raise TypeError ( '<STR_LIT>' ) <EOL> if PY2 : <EOL> try : <EOL> name = name . encode ( '<STR_LIT:ascii>' ) <EOL> except UnicodeError : <EOL> raise TypeError ( '<STR_LIT>' ) <EOL> if not isinstance ( bucket_type , BucketType ) : <EOL> raise TypeError ( '<STR_LIT>' ) <EOL> self . _client = client <EOL> self . name = name <EOL> self . bucket_type = bucket_type <EOL> self . _encoders = { } <EOL> self . _decoders = { } <EOL> self . _resolver = None <EOL> def __hash__ ( self ) : <EOL> return hash ( ( self . bucket_type . name , self . name , self . _client ) ) <EOL> def __eq__ ( self , other ) : <EOL> if isinstance ( other , self . __class__ ) : <EOL> return hash ( self ) == hash ( other ) <EOL> else : <EOL> return False <EOL> def __ne__ ( self , other ) : <EOL> if isinstance ( other , self . __class__ ) : <EOL> return hash ( self ) != hash ( other ) <EOL> else : <EOL> return True <EOL> def get_encoder ( self , content_type ) : <EOL> """<STR_LIT>""" <EOL> if content_type in self . _encoders : <EOL> return self . _encoders [ content_type ] <EOL> else : <EOL> return self . _client . get_encoder ( content_type ) <EOL> def set_encoder ( self , content_type , encoder ) : <EOL> """<STR_LIT>""" <EOL> self . _encoders [ content_type ] = encoder <EOL> return self <EOL> def get_decoder ( self , content_type ) : <EOL> """<STR_LIT>""" <EOL> if content_type in self . _decoders : <EOL> return self . _decoders [ content_type ] <EOL> else : <EOL> return self . _client . get_decoder ( content_type ) <EOL> def set_decoder ( self , content_type , decoder ) : <EOL> """<STR_LIT>""" <EOL> self . _decoders [ content_type ] = decoder <EOL> return self <EOL> def new ( self , key = None , data = None , content_type = '<STR_LIT:application/json>' , <EOL> encoded_data = None ) : <EOL> """<STR_LIT>""" <EOL> from riak import RiakObject <EOL> if self . bucket_type . datatype : <EOL> return TYPES [ self . bucket_type . datatype ] ( bucket = self , key = key ) <EOL> if PY2 : <EOL> try : <EOL> if isinstance ( data , string_types ) : <EOL> data = data . encode ( '<STR_LIT:ascii>' ) <EOL> except UnicodeError : <EOL> raise TypeError ( '<STR_LIT>' ) <EOL> obj = RiakObject ( self . _client , self , key ) <EOL> obj . content_type = content_type <EOL> if data is not None : <EOL> obj . data = data <EOL> if encoded_data is not None : <EOL> obj . encoded_data = encoded_data <EOL> return obj <EOL> def get ( self , key , r = None , pr = None , timeout = None , include_context = None , <EOL> basic_quorum = None , notfound_ok = None ) : <EOL> """<STR_LIT>""" <EOL> from riak import RiakObject <EOL> if self . bucket_type . datatype : <EOL> return self . _client . fetch_datatype ( self , key , r = r , pr = pr , <EOL> timeout = timeout , <EOL> include_context = include_context , <EOL> basic_quorum = basic_quorum , <EOL> notfound_ok = notfound_ok ) <EOL> else : <EOL> obj = RiakObject ( self . _client , self , key ) <EOL> return obj . reload ( r = r , pr = pr , timeout = timeout , <EOL> basic_quorum = basic_quorum , <EOL> notfound_ok = notfound_ok ) <EOL> def multiget ( self , keys , r = None , pr = None , timeout = None , <EOL> basic_quorum = None , notfound_ok = None ) : <EOL> """<STR_LIT>""" <EOL> bkeys = [ ( self . bucket_type . name , self . name , key ) for key in keys ] <EOL> return self . _client . multiget ( bkeys , r = r , pr = pr , timeout = timeout , <EOL> basic_quorum = basic_quorum , <EOL> notfound_ok = notfound_ok ) <EOL> def _get_resolver ( self ) : <EOL> if callable ( self . _resolver ) : <EOL> return self . _resolver <EOL> elif self . _resolver is None : <EOL> return self . _client . resolver <EOL> else : <EOL> raise TypeError ( "<STR_LIT>" ) <EOL> def _set_resolver ( self , value ) : <EOL> if value is None or callable ( value ) : <EOL> self . _resolver = value <EOL> else : <EOL> raise TypeError ( "<STR_LIT>" ) <EOL> resolver = property ( _get_resolver , _set_resolver , <EOL> doc = """<STR_LIT>""" ) <EOL> n_val = bucket_property ( '<STR_LIT>' , doc = """<STR_LIT>""" ) <EOL> allow_mult = bucket_property ( '<STR_LIT>' , doc = """<STR_LIT>""" ) <EOL> r = bucket_property ( '<STR_LIT:r>' , doc = """<STR_LIT>""" ) <EOL> pr = bucket_property ( '<STR_LIT>' , doc = """<STR_LIT>""" ) <EOL> rw = bucket_property ( '<STR_LIT>' , doc = """<STR_LIT>""" ) <EOL> w = bucket_property ( '<STR_LIT:w>' , doc = """<STR_LIT>""" ) <EOL> dw = bucket_property ( '<STR_LIT>' , doc = """<STR_LIT>""" ) <EOL> pw = bucket_property ( '<STR_LIT>' , doc = """<STR_LIT>""" ) <EOL> def set_property ( self , key , value ) : <EOL> """<STR_LIT>""" <EOL> return self . set_properties ( { key : value } ) <EOL> def get_property ( self , key ) : <EOL> """<STR_LIT>""" <EOL> return self . get_properties ( ) [ key ] <EOL> def set_properties ( self , props ) : <EOL> """<STR_LIT>""" <EOL> self . _client . set_bucket_props ( self , props ) <EOL> def get_properties ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _client . get_bucket_props ( self ) <EOL> def clear_properties ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _client . clear_bucket_props ( self ) <EOL> def get_keys ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _client . get_keys ( self ) <EOL> def stream_keys ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _client . stream_keys ( self ) <EOL> def new_from_file ( self , key , filename ) : <EOL> """<STR_LIT>""" <EOL> binary_data = None <EOL> with open ( filename , '<STR_LIT:rb>' ) as f : <EOL> binary_data = f . read ( ) <EOL> mimetype , encoding = mimetypes . guess_type ( filename ) <EOL> if encoding : <EOL> binary_data = bytearray ( binary_data , encoding ) <EOL> else : <EOL> binary_data = bytearray ( binary_data ) <EOL> if not mimetype : <EOL> mimetype = '<STR_LIT>' <EOL> if PY2 : <EOL> return self . new ( key , encoded_data = binary_data , <EOL> content_type = mimetype ) <EOL> else : <EOL> return self . new ( key , encoded_data = bytes ( binary_data ) , <EOL> content_type = mimetype ) <EOL> def search_enabled ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . get_properties ( ) . get ( '<STR_LIT>' , False ) <EOL> def enable_search ( self ) : <EOL> """<STR_LIT>""" <EOL> if not self . search_enabled ( ) : <EOL> self . set_property ( '<STR_LIT>' , True ) <EOL> return True <EOL> def disable_search ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . search_enabled ( ) : <EOL> self . set_property ( '<STR_LIT>' , False ) <EOL> return True <EOL> def search ( self , query , index = None , ** params ) : <EOL> """<STR_LIT>""" <EOL> search_index = index or self . name <EOL> return self . _client . fulltext_search ( search_index , query , ** params ) <EOL> def get_index ( self , index , startkey , endkey = None , return_terms = None , <EOL> max_results = None , continuation = None , timeout = None , <EOL> term_regex = None ) : <EOL> """<STR_LIT>""" <EOL> return self . _client . get_index ( self , index , startkey , endkey , <EOL> return_terms = return_terms , <EOL> max_results = max_results , <EOL> continuation = continuation , <EOL> timeout = timeout , term_regex = term_regex ) <EOL> def paginate_index ( self , index , startkey , endkey = None , <EOL> return_terms = None , max_results = <NUM_LIT:1000> , <EOL> continuation = None , timeout = None , term_regex = None ) : <EOL> """<STR_LIT>""" <EOL> return self . _client . paginate_index ( self , index , startkey , endkey , <EOL> return_terms = return_terms , <EOL> max_results = max_results , <EOL> continuation = continuation , <EOL> timeout = timeout , <EOL> term_regex = term_regex ) <EOL> def stream_index ( self , index , startkey , endkey = None , return_terms = None , <EOL> max_results = None , continuation = None , timeout = None , <EOL> term_regex = None ) : <EOL> """<STR_LIT>""" <EOL> return self . _client . stream_index ( self , index , startkey , endkey , <EOL> return_terms = return_terms , <EOL> max_results = max_results , <EOL> continuation = continuation , <EOL> timeout = timeout , <EOL> term_regex = term_regex ) <EOL> def paginate_stream_index ( self , index , startkey , endkey = None , <EOL> return_terms = None , max_results = <NUM_LIT:1000> , <EOL> continuation = None , timeout = None , <EOL> term_regex = None ) : <EOL> """<STR_LIT>""" <EOL> return self . _client . paginate_stream_index ( self , index , startkey , <EOL> endkey , <EOL> return_terms = return_terms , <EOL> max_results = max_results , <EOL> continuation = continuation , <EOL> timeout = timeout , <EOL> term_regex = term_regex ) <EOL> def delete ( self , key , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> return self . new ( key ) . delete ( ** kwargs ) <EOL> def get_counter ( self , key , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> return self . _client . get_counter ( self , key , ** kwargs ) <EOL> def update_counter ( self , key , value , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> return self . _client . update_counter ( self , key , value , ** kwargs ) <EOL> increment_counter = update_counter <EOL> def get_preflist ( self , key ) : <EOL> """<STR_LIT>""" <EOL> return self . _client . get_preflist ( self , key ) <EOL> def __str__ ( self ) : <EOL> if self . bucket_type . is_default ( ) : <EOL> return '<STR_LIT>' . format ( self . name ) <EOL> else : <EOL> return '<STR_LIT>' . format ( self . bucket_type . name , <EOL> self . name ) <EOL> __repr__ = __str__ <EOL> class BucketType ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , client , name ) : <EOL> """<STR_LIT>""" <EOL> self . _client = client <EOL> self . name = name <EOL> def is_default ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . name == '<STR_LIT:default>' <EOL> def get_property ( self , key ) : <EOL> """<STR_LIT>""" <EOL> return self . get_properties ( ) [ key ] <EOL> def set_property ( self , key , value ) : <EOL> """<STR_LIT>""" <EOL> return self . set_properties ( { key : value } ) <EOL> def get_properties ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _client . get_bucket_type_props ( self ) <EOL> def set_properties ( self , props ) : <EOL> """<STR_LIT>""" <EOL> self . _client . set_bucket_type_props ( self , props ) <EOL> def bucket ( self , name ) : <EOL> """<STR_LIT>""" <EOL> return self . _client . bucket ( name , self ) <EOL> def get_buckets ( self , timeout = None ) : <EOL> """<STR_LIT>""" <EOL> return self . _client . get_buckets ( bucket_type = self , timeout = timeout ) <EOL> def stream_buckets ( self , timeout = None ) : <EOL> """<STR_LIT>""" <EOL> return self . _client . stream_buckets ( bucket_type = self , timeout = timeout ) <EOL> @ lazy_property <EOL> def datatype ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . is_default ( ) : <EOL> return None <EOL> else : <EOL> return self . get_properties ( ) . get ( '<STR_LIT>' ) <EOL> def __str__ ( self ) : <EOL> return "<STR_LIT>" . format ( self . name ) <EOL> __repr__ = __str__ <EOL> def __hash__ ( self ) : <EOL> return hash ( ( self . name , self . _client ) ) <EOL> def __eq__ ( self , other ) : <EOL> if isinstance ( other , self . __class__ ) : <EOL> return hash ( self ) == hash ( other ) <EOL> else : <EOL> return False <EOL> def __ne__ ( self , other ) : <EOL> if isinstance ( other , self . __class__ ) : <EOL> return hash ( self ) != hash ( other ) <EOL> else : <EOL> return True </s>
