<s> import re <EOL> import urllib <EOL> import xml . sax <EOL> import boto <EOL> from boto import handler <EOL> from boto . resultset import ResultSet <EOL> from boto . exception import GSResponseError <EOL> from boto . exception import InvalidAclError <EOL> from boto . gs . acl import ACL , CannedACLStrings <EOL> from boto . gs . acl import SupportedPermissions as GSPermissions <EOL> from boto . gs . bucketlistresultset import VersionedBucketListResultSet <EOL> from boto . gs . cors import Cors <EOL> from boto . gs . lifecycle import LifecycleConfig <EOL> from boto . gs . key import Key as GSKey <EOL> from boto . s3 . acl import Policy <EOL> from boto . s3 . bucket import Bucket as S3Bucket <EOL> from boto . utils import get_utf8_value <EOL> from boto . compat import six <EOL> DEF_OBJ_ACL = '<STR_LIT>' <EOL> STANDARD_ACL = '<STR_LIT>' <EOL> CORS_ARG = '<STR_LIT>' <EOL> LIFECYCLE_ARG = '<STR_LIT>' <EOL> ERROR_DETAILS_REGEX = re . compile ( r'<STR_LIT>' ) <EOL> class Bucket ( S3Bucket ) : <EOL> """<STR_LIT>""" <EOL> VersioningBody = ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> WebsiteBody = ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> WebsiteMainPageFragment = '<STR_LIT>' <EOL> WebsiteErrorFragment = '<STR_LIT>' <EOL> def __init__ ( self , connection = None , name = None , key_class = GSKey ) : <EOL> super ( Bucket , self ) . __init__ ( connection , name , key_class ) <EOL> def startElement ( self , name , attrs , connection ) : <EOL> return None <EOL> def endElement ( self , name , value , connection ) : <EOL> if name == '<STR_LIT:Name>' : <EOL> self . name = value <EOL> elif name == '<STR_LIT>' : <EOL> self . creation_date = value <EOL> else : <EOL> setattr ( self , name , value ) <EOL> def get_key ( self , key_name , headers = None , version_id = None , <EOL> response_headers = None , generation = None ) : <EOL> """<STR_LIT>""" <EOL> query_args_l = [ ] <EOL> if generation : <EOL> query_args_l . append ( '<STR_LIT>' % generation ) <EOL> if response_headers : <EOL> for rk , rv in six . iteritems ( response_headers ) : <EOL> query_args_l . append ( '<STR_LIT>' % ( rk , urllib . quote ( rv ) ) ) <EOL> try : <EOL> key , resp = self . _get_key_internal ( key_name , headers , <EOL> query_args_l = query_args_l ) <EOL> except GSResponseError as e : <EOL> if e . status == <NUM_LIT> and '<STR_LIT>' in e . reason : <EOL> e . reason = ( "<STR_LIT>" % <EOL> ( self . name , key_name ) ) <EOL> raise <EOL> return key <EOL> def copy_key ( self , new_key_name , src_bucket_name , src_key_name , <EOL> metadata = None , src_version_id = None , storage_class = '<STR_LIT>' , <EOL> preserve_acl = False , encrypt_key = False , headers = None , <EOL> query_args = None , src_generation = None ) : <EOL> """<STR_LIT>""" <EOL> if src_generation : <EOL> headers = headers or { } <EOL> headers [ '<STR_LIT>' ] = str ( src_generation ) <EOL> return super ( Bucket , self ) . copy_key ( <EOL> new_key_name , src_bucket_name , src_key_name , metadata = metadata , <EOL> storage_class = storage_class , preserve_acl = preserve_acl , <EOL> encrypt_key = encrypt_key , headers = headers , query_args = query_args ) <EOL> def list_versions ( self , prefix = '<STR_LIT>' , delimiter = '<STR_LIT>' , marker = '<STR_LIT>' , <EOL> generation_marker = '<STR_LIT>' , headers = None ) : <EOL> """<STR_LIT>""" <EOL> return VersionedBucketListResultSet ( self , prefix , delimiter , <EOL> marker , generation_marker , <EOL> headers ) <EOL> def validate_get_all_versions_params ( self , params ) : <EOL> """<STR_LIT>""" <EOL> self . validate_kwarg_names ( params , <EOL> [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> def delete_key ( self , key_name , headers = None , version_id = None , <EOL> mfa_token = None , generation = None ) : <EOL> """<STR_LIT>""" <EOL> query_args_l = [ ] <EOL> if generation : <EOL> query_args_l . append ( '<STR_LIT>' % generation ) <EOL> self . _delete_key_internal ( key_name , headers = headers , <EOL> version_id = version_id , mfa_token = mfa_token , <EOL> query_args_l = query_args_l ) <EOL> def set_acl ( self , acl_or_str , key_name = '<STR_LIT>' , headers = None , version_id = None , <EOL> generation = None , if_generation = None , if_metageneration = None ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( acl_or_str , Policy ) : <EOL> raise InvalidAclError ( '<STR_LIT>' ) <EOL> elif isinstance ( acl_or_str , ACL ) : <EOL> self . set_xml_acl ( acl_or_str . to_xml ( ) , key_name , headers = headers , <EOL> generation = generation , <EOL> if_generation = if_generation , <EOL> if_metageneration = if_metageneration ) <EOL> else : <EOL> self . set_canned_acl ( acl_or_str , key_name , headers = headers , <EOL> generation = generation , <EOL> if_generation = if_generation , <EOL> if_metageneration = if_metageneration ) <EOL> def set_def_acl ( self , acl_or_str , headers = None ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( acl_or_str , Policy ) : <EOL> raise InvalidAclError ( '<STR_LIT>' ) <EOL> elif isinstance ( acl_or_str , ACL ) : <EOL> self . set_def_xml_acl ( acl_or_str . to_xml ( ) , headers = headers ) <EOL> else : <EOL> self . set_def_canned_acl ( acl_or_str , headers = headers ) <EOL> def _get_xml_acl_helper ( self , key_name , headers , query_args ) : <EOL> """<STR_LIT>""" <EOL> response = self . connection . make_request ( '<STR_LIT:GET>' , self . name , key_name , <EOL> query_args = query_args , <EOL> headers = headers ) <EOL> body = response . read ( ) <EOL> if response . status != <NUM_LIT:200> : <EOL> if response . status == <NUM_LIT> : <EOL> match = ERROR_DETAILS_REGEX . search ( body ) <EOL> details = match . group ( '<STR_LIT>' ) if match else None <EOL> if details : <EOL> details = ( ( '<STR_LIT>' <EOL> '<STR_LIT>' ) % <EOL> details ) <EOL> body = re . sub ( ERROR_DETAILS_REGEX , details , body ) <EOL> raise self . connection . provider . storage_response_error ( <EOL> response . status , response . reason , body ) <EOL> return body <EOL> def _get_acl_helper ( self , key_name , headers , query_args ) : <EOL> """<STR_LIT>""" <EOL> body = self . _get_xml_acl_helper ( key_name , headers , query_args ) <EOL> acl = ACL ( self ) <EOL> h = handler . XmlHandler ( acl , self ) <EOL> xml . sax . parseString ( body , h ) <EOL> return acl <EOL> def get_acl ( self , key_name = '<STR_LIT>' , headers = None , version_id = None , <EOL> generation = None ) : <EOL> """<STR_LIT>""" <EOL> query_args = STANDARD_ACL <EOL> if generation : <EOL> query_args += '<STR_LIT>' % generation <EOL> return self . _get_acl_helper ( key_name , headers , query_args ) <EOL> def get_xml_acl ( self , key_name = '<STR_LIT>' , headers = None , version_id = None , <EOL> generation = None ) : <EOL> """<STR_LIT>""" <EOL> query_args = STANDARD_ACL <EOL> if generation : <EOL> query_args += '<STR_LIT>' % generation <EOL> return self . _get_xml_acl_helper ( key_name , headers , query_args ) <EOL> def get_def_acl ( self , headers = None ) : <EOL> """<STR_LIT>""" <EOL> return self . _get_acl_helper ( '<STR_LIT>' , headers , DEF_OBJ_ACL ) <EOL> def _set_acl_helper ( self , acl_or_str , key_name , headers , query_args , <EOL> generation , if_generation , if_metageneration , <EOL> canned = False ) : <EOL> """<STR_LIT>""" <EOL> headers = headers or { } <EOL> data = '<STR_LIT>' <EOL> if canned : <EOL> headers [ self . connection . provider . acl_header ] = acl_or_str <EOL> else : <EOL> data = acl_or_str <EOL> if generation : <EOL> query_args += '<STR_LIT>' % generation <EOL> if if_metageneration is not None and if_generation is None : <EOL> raise ValueError ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> if not key_name and ( if_generation or if_metageneration ) : <EOL> raise ValueError ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> if if_generation is not None : <EOL> headers [ '<STR_LIT>' ] = str ( if_generation ) <EOL> if if_metageneration is not None : <EOL> headers [ '<STR_LIT>' ] = str ( if_metageneration ) <EOL> response = self . connection . make_request ( <EOL> '<STR_LIT>' , get_utf8_value ( self . name ) , get_utf8_value ( key_name ) , <EOL> data = get_utf8_value ( data ) , headers = headers , query_args = query_args ) <EOL> body = response . read ( ) <EOL> if response . status != <NUM_LIT:200> : <EOL> raise self . connection . provider . storage_response_error ( <EOL> response . status , response . reason , body ) <EOL> def set_xml_acl ( self , acl_str , key_name = '<STR_LIT>' , headers = None , version_id = None , <EOL> query_args = '<STR_LIT>' , generation = None , if_generation = None , <EOL> if_metageneration = None ) : <EOL> """<STR_LIT>""" <EOL> return self . _set_acl_helper ( acl_str , key_name = key_name , headers = headers , <EOL> query_args = query_args , <EOL> generation = generation , <EOL> if_generation = if_generation , <EOL> if_metageneration = if_metageneration ) <EOL> def set_canned_acl ( self , acl_str , key_name = '<STR_LIT>' , headers = None , <EOL> version_id = None , generation = None , if_generation = None , <EOL> if_metageneration = None ) : <EOL> """<STR_LIT>""" <EOL> if acl_str not in CannedACLStrings : <EOL> raise ValueError ( "<STR_LIT>" <EOL> % acl_str ) <EOL> query_args = STANDARD_ACL <EOL> return self . _set_acl_helper ( acl_str , key_name , headers , query_args , <EOL> generation , if_generation , <EOL> if_metageneration , canned = True ) <EOL> def set_def_canned_acl ( self , acl_str , headers = None ) : <EOL> """<STR_LIT>""" <EOL> if acl_str not in CannedACLStrings : <EOL> raise ValueError ( "<STR_LIT>" <EOL> % acl_str ) <EOL> query_args = DEF_OBJ_ACL <EOL> return self . _set_acl_helper ( acl_str , '<STR_LIT>' , headers , query_args , <EOL> generation = None , if_generation = None , <EOL> if_metageneration = None , canned = True ) <EOL> def set_def_xml_acl ( self , acl_str , headers = None ) : <EOL> """<STR_LIT>""" <EOL> return self . set_xml_acl ( acl_str , '<STR_LIT>' , headers , <EOL> query_args = DEF_OBJ_ACL ) <EOL> def get_cors ( self , headers = None ) : <EOL> """<STR_LIT>""" <EOL> response = self . connection . make_request ( '<STR_LIT:GET>' , self . name , <EOL> query_args = CORS_ARG , <EOL> headers = headers ) <EOL> body = response . read ( ) <EOL> if response . status == <NUM_LIT:200> : <EOL> cors = Cors ( ) <EOL> h = handler . XmlHandler ( cors , self ) <EOL> xml . sax . parseString ( body , h ) <EOL> return cors <EOL> else : <EOL> raise self . connection . provider . storage_response_error ( <EOL> response . status , response . reason , body ) <EOL> def set_cors ( self , cors , headers = None ) : <EOL> """<STR_LIT>""" <EOL> response = self . connection . make_request ( <EOL> '<STR_LIT>' , get_utf8_value ( self . name ) , data = get_utf8_value ( cors ) , <EOL> query_args = CORS_ARG , headers = headers ) <EOL> body = response . read ( ) <EOL> if response . status != <NUM_LIT:200> : <EOL> raise self . connection . provider . storage_response_error ( <EOL> response . status , response . reason , body ) <EOL> def get_storage_class ( self ) : <EOL> """<STR_LIT>""" <EOL> response = self . connection . make_request ( '<STR_LIT:GET>' , self . name , <EOL> query_args = '<STR_LIT>' ) <EOL> body = response . read ( ) <EOL> if response . status == <NUM_LIT:200> : <EOL> rs = ResultSet ( self ) <EOL> h = handler . XmlHandler ( rs , self ) <EOL> xml . sax . parseString ( body , h ) <EOL> return rs . StorageClass <EOL> else : <EOL> raise self . connection . provider . storage_response_error ( <EOL> response . status , response . reason , body ) <EOL> def add_email_grant ( self , permission , email_address , <EOL> recursive = False , headers = None ) : <EOL> """<STR_LIT>""" <EOL> if permission not in GSPermissions : <EOL> raise self . connection . provider . storage_permissions_error ( <EOL> '<STR_LIT>' % permission ) <EOL> acl = self . get_acl ( headers = headers ) <EOL> acl . add_email_grant ( permission , email_address ) <EOL> self . set_acl ( acl , headers = headers ) <EOL> if recursive : <EOL> for key in self : <EOL> key . add_email_grant ( permission , email_address , headers = headers ) <EOL> def add_user_grant ( self , permission , user_id , recursive = False , <EOL> headers = None ) : <EOL> """<STR_LIT>""" <EOL> if permission not in GSPermissions : <EOL> raise self . connection . provider . storage_permissions_error ( <EOL> '<STR_LIT>' % permission ) <EOL> acl = self . get_acl ( headers = headers ) <EOL> acl . add_user_grant ( permission , user_id ) <EOL> self . set_acl ( acl , headers = headers ) <EOL> if recursive : <EOL> for key in self : <EOL> key . add_user_grant ( permission , user_id , headers = headers ) <EOL> def add_group_email_grant ( self , permission , email_address , recursive = False , <EOL> headers = None ) : <EOL> """<STR_LIT>""" <EOL> if permission not in GSPermissions : <EOL> raise self . connection . provider . storage_permissions_error ( <EOL> '<STR_LIT>' % permission ) <EOL> acl = self . get_acl ( headers = headers ) <EOL> acl . add_group_email_grant ( permission , email_address ) <EOL> self . set_acl ( acl , headers = headers ) <EOL> if recursive : <EOL> for key in self : <EOL> key . add_group_email_grant ( permission , email_address , <EOL> headers = headers ) <EOL> def list_grants ( self , headers = None ) : <EOL> """<STR_LIT>""" <EOL> acl = self . get_acl ( headers = headers ) <EOL> return acl . entries <EOL> def disable_logging ( self , headers = None ) : <EOL> """<STR_LIT>""" <EOL> xml_str = '<STR_LIT>' <EOL> self . set_subresource ( '<STR_LIT>' , xml_str , headers = headers ) <EOL> def enable_logging ( self , target_bucket , target_prefix = None , headers = None ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( target_bucket , Bucket ) : <EOL> target_bucket = target_bucket . name <EOL> xml_str = '<STR_LIT>' <EOL> xml_str = ( xml_str + '<STR_LIT>' % target_bucket ) <EOL> if target_prefix : <EOL> xml_str = ( xml_str + <EOL> '<STR_LIT>' % target_prefix ) <EOL> xml_str = xml_str + '<STR_LIT>' <EOL> self . set_subresource ( '<STR_LIT>' , xml_str , headers = headers ) <EOL> def get_logging_config_with_xml ( self , headers = None ) : <EOL> """<STR_LIT>""" <EOL> response = self . connection . make_request ( '<STR_LIT:GET>' , self . name , <EOL> query_args = '<STR_LIT>' , <EOL> headers = headers ) <EOL> body = response . read ( ) <EOL> boto . log . debug ( body ) <EOL> if response . status != <NUM_LIT:200> : <EOL> raise self . connection . provider . storage_response_error ( <EOL> response . status , response . reason , body ) <EOL> e = boto . jsonresponse . Element ( ) <EOL> h = boto . jsonresponse . XmlHandler ( e , None ) <EOL> h . parse ( body ) <EOL> return e , body <EOL> def get_logging_config ( self , headers = None ) : <EOL> """<STR_LIT>""" <EOL> return self . get_logging_config_with_xml ( headers ) [ <NUM_LIT:0> ] <EOL> def configure_website ( self , main_page_suffix = None , error_key = None , <EOL> headers = None ) : <EOL> """<STR_LIT>""" <EOL> if main_page_suffix : <EOL> main_page_frag = self . WebsiteMainPageFragment % main_page_suffix <EOL> else : <EOL> main_page_frag = '<STR_LIT>' <EOL> if error_key : <EOL> error_frag = self . WebsiteErrorFragment % error_key <EOL> else : <EOL> error_frag = '<STR_LIT>' <EOL> body = self . WebsiteBody % ( main_page_frag , error_frag ) <EOL> response = self . connection . make_request ( <EOL> '<STR_LIT>' , get_utf8_value ( self . name ) , data = get_utf8_value ( body ) , <EOL> query_args = '<STR_LIT>' , headers = headers ) <EOL> body = response . read ( ) <EOL> if response . status == <NUM_LIT:200> : <EOL> return True <EOL> else : <EOL> raise self . connection . provider . storage_response_error ( <EOL> response . status , response . reason , body ) <EOL> def get_website_configuration ( self , headers = None ) : <EOL> """<STR_LIT>""" <EOL> return self . get_website_configuration_with_xml ( headers ) [ <NUM_LIT:0> ] <EOL> def get_website_configuration_with_xml ( self , headers = None ) : <EOL> """<STR_LIT>""" <EOL> response = self . connection . make_request ( '<STR_LIT:GET>' , self . name , <EOL> query_args = '<STR_LIT>' , headers = headers ) <EOL> body = response . read ( ) <EOL> boto . log . debug ( body ) <EOL> if response . status != <NUM_LIT:200> : <EOL> raise self . connection . provider . storage_response_error ( <EOL> response . status , response . reason , body ) <EOL> e = boto . jsonresponse . Element ( ) <EOL> h = boto . jsonresponse . XmlHandler ( e , None ) <EOL> h . parse ( body ) <EOL> return e , body <EOL> def delete_website_configuration ( self , headers = None ) : <EOL> """<STR_LIT>""" <EOL> self . configure_website ( headers = headers ) <EOL> def get_versioning_status ( self , headers = None ) : <EOL> """<STR_LIT>""" <EOL> response = self . connection . make_request ( '<STR_LIT:GET>' , self . name , <EOL> query_args = '<STR_LIT>' , <EOL> headers = headers ) <EOL> body = response . read ( ) <EOL> boto . log . debug ( body ) <EOL> if response . status != <NUM_LIT:200> : <EOL> raise self . connection . provider . storage_response_error ( <EOL> response . status , response . reason , body ) <EOL> resp_json = boto . jsonresponse . Element ( ) <EOL> boto . jsonresponse . XmlHandler ( resp_json , None ) . parse ( body ) <EOL> resp_json = resp_json [ '<STR_LIT>' ] <EOL> return ( '<STR_LIT>' in resp_json ) and ( resp_json [ '<STR_LIT>' ] == '<STR_LIT>' ) <EOL> def configure_versioning ( self , enabled , headers = None ) : <EOL> """<STR_LIT>""" <EOL> if enabled == True : <EOL> req_body = self . VersioningBody % ( '<STR_LIT>' ) <EOL> else : <EOL> req_body = self . VersioningBody % ( '<STR_LIT>' ) <EOL> self . set_subresource ( '<STR_LIT>' , req_body , headers = headers ) <EOL> def get_lifecycle_config ( self , headers = None ) : <EOL> """<STR_LIT>""" <EOL> response = self . connection . make_request ( '<STR_LIT:GET>' , self . name , <EOL> query_args = LIFECYCLE_ARG , headers = headers ) <EOL> body = response . read ( ) <EOL> boto . log . debug ( body ) <EOL> if response . status == <NUM_LIT:200> : <EOL> lifecycle_config = LifecycleConfig ( ) <EOL> h = handler . XmlHandler ( lifecycle_config , self ) <EOL> xml . sax . parseString ( body , h ) <EOL> return lifecycle_config <EOL> else : <EOL> raise self . connection . provider . storage_response_error ( <EOL> response . status , response . reason , body ) <EOL> def configure_lifecycle ( self , lifecycle_config , headers = None ) : <EOL> """<STR_LIT>""" <EOL> xml = lifecycle_config . to_xml ( ) <EOL> response = self . connection . make_request ( <EOL> '<STR_LIT>' , get_utf8_value ( self . name ) , data = get_utf8_value ( xml ) , <EOL> query_args = LIFECYCLE_ARG , headers = headers ) <EOL> body = response . read ( ) <EOL> if response . status == <NUM_LIT:200> : <EOL> return True <EOL> else : <EOL> raise self . connection . provider . storage_response_error ( <EOL> response . status , response . reason , body ) </s>
