<s> from . . _error import ( <EOL> _validate_not_none , <EOL> _validate_type_bytes , <EOL> _ERROR_VALUE_NEGATIVE , <EOL> ) <EOL> from . . _common_conversion import ( <EOL> _int_to_str , <EOL> _to_str , <EOL> _datetime_to_utc_string , <EOL> ) <EOL> from . . _serialization import ( <EOL> _get_request_body_bytes_only , <EOL> ) <EOL> from . . _http import HTTPRequest <EOL> from . _error import ( <EOL> _ERROR_PAGE_BLOB_SIZE_ALIGNMENT , <EOL> ) <EOL> from . _chunking import ( <EOL> _PageBlobChunkUploader , <EOL> _upload_blob_chunks , <EOL> ) <EOL> from . models import ( <EOL> _BlobTypes , <EOL> PageBlobProperties , <EOL> ) <EOL> from . . _constants import ( <EOL> SERVICE_HOST_BASE , <EOL> DEFAULT_PROTOCOL , <EOL> ) <EOL> from . _serialization import ( <EOL> _get_path , <EOL> _validate_and_format_range_headers , <EOL> ) <EOL> from . _deserialization import ( <EOL> _convert_xml_to_page_ranges , <EOL> _parse_page_properties , <EOL> _parse_base_properties , <EOL> ) <EOL> from . baseblobservice import BaseBlobService <EOL> from os import path <EOL> import sys <EOL> if sys . version_info >= ( <NUM_LIT:3> , ) : <EOL> from io import BytesIO <EOL> else : <EOL> from cStringIO import StringIO as BytesIO <EOL> _PAGE_ALIGNMENT = <NUM_LIT> <EOL> class PageBlobService ( BaseBlobService ) : <EOL> '''<STR_LIT>''' <EOL> MAX_PAGE_SIZE = <NUM_LIT:4> * <NUM_LIT> * <NUM_LIT> <EOL> def __init__ ( self , account_name = None , account_key = None , sas_token = None , <EOL> is_emulated = False , protocol = DEFAULT_PROTOCOL , endpoint_suffix = SERVICE_HOST_BASE , <EOL> custom_domain = None , request_session = None , connection_string = None ) : <EOL> '''<STR_LIT>''' <EOL> self . blob_type = _BlobTypes . PageBlob <EOL> super ( PageBlobService , self ) . __init__ ( <EOL> account_name , account_key , sas_token , is_emulated , protocol , endpoint_suffix , <EOL> custom_domain , request_session , connection_string ) <EOL> def create_blob ( <EOL> self , container_name , blob_name , content_length , content_settings = None , <EOL> sequence_number = None , metadata = None , lease_id = None , if_modified_since = None , <EOL> if_unmodified_since = None , if_match = None , if_none_match = None , timeout = None ) : <EOL> '''<STR_LIT>''' <EOL> _validate_not_none ( '<STR_LIT>' , container_name ) <EOL> _validate_not_none ( '<STR_LIT>' , blob_name ) <EOL> _validate_not_none ( '<STR_LIT>' , content_length ) <EOL> request = HTTPRequest ( ) <EOL> request . method = '<STR_LIT>' <EOL> request . host = self . _get_host ( ) <EOL> request . path = _get_path ( container_name , blob_name ) <EOL> request . query = [ ( '<STR_LIT>' , _int_to_str ( timeout ) ) ] <EOL> request . headers = [ <EOL> ( '<STR_LIT>' , _to_str ( self . blob_type ) ) , <EOL> ( '<STR_LIT>' , metadata ) , <EOL> ( '<STR_LIT>' , _to_str ( content_length ) ) , <EOL> ( '<STR_LIT>' , _to_str ( lease_id ) ) , <EOL> ( '<STR_LIT>' , _to_str ( sequence_number ) ) , <EOL> ( '<STR_LIT>' , _datetime_to_utc_string ( if_modified_since ) ) , <EOL> ( '<STR_LIT>' , _datetime_to_utc_string ( if_unmodified_since ) ) , <EOL> ( '<STR_LIT>' , _to_str ( if_match ) ) , <EOL> ( '<STR_LIT>' , _to_str ( if_none_match ) ) <EOL> ] <EOL> if content_settings is not None : <EOL> request . headers += content_settings . _to_headers ( ) <EOL> response = self . _perform_request ( request ) <EOL> return _parse_base_properties ( response ) <EOL> def update_page ( <EOL> self , container_name , blob_name , page , start_range , end_range , <EOL> content_md5 = None , lease_id = None , if_sequence_number_lte = None , <EOL> if_sequence_number_lt = None , if_sequence_number_eq = None , <EOL> if_modified_since = None , if_unmodified_since = None , <EOL> if_match = None , if_none_match = None , timeout = None ) : <EOL> '''<STR_LIT>''' <EOL> _validate_not_none ( '<STR_LIT>' , container_name ) <EOL> _validate_not_none ( '<STR_LIT>' , blob_name ) <EOL> _validate_not_none ( '<STR_LIT>' , page ) <EOL> request = HTTPRequest ( ) <EOL> request . method = '<STR_LIT>' <EOL> request . host = self . _get_host ( ) <EOL> request . path = _get_path ( container_name , blob_name ) <EOL> request . query = [ <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , _int_to_str ( timeout ) ) , <EOL> ] <EOL> request . headers = [ <EOL> ( '<STR_LIT>' , _to_str ( content_md5 ) ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , _to_str ( lease_id ) ) , <EOL> ( '<STR_LIT>' , <EOL> _to_str ( if_sequence_number_lte ) ) , <EOL> ( '<STR_LIT>' , <EOL> _to_str ( if_sequence_number_lt ) ) , <EOL> ( '<STR_LIT>' , <EOL> _to_str ( if_sequence_number_eq ) ) , <EOL> ( '<STR_LIT>' , _datetime_to_utc_string ( if_modified_since ) ) , <EOL> ( '<STR_LIT>' , _datetime_to_utc_string ( if_unmodified_since ) ) , <EOL> ( '<STR_LIT>' , _to_str ( if_match ) ) , <EOL> ( '<STR_LIT>' , _to_str ( if_none_match ) ) <EOL> ] <EOL> _validate_and_format_range_headers ( <EOL> request , <EOL> start_range , <EOL> end_range , <EOL> align_to_page = True ) <EOL> request . body = _get_request_body_bytes_only ( '<STR_LIT>' , page ) <EOL> response = self . _perform_request ( request ) <EOL> return _parse_page_properties ( response ) <EOL> def clear_page ( <EOL> self , container_name , blob_name , start_range , end_range , <EOL> lease_id = None , if_sequence_number_lte = None , <EOL> if_sequence_number_lt = None , if_sequence_number_eq = None , <EOL> if_modified_since = None , if_unmodified_since = None , <EOL> if_match = None , if_none_match = None , timeout = None ) : <EOL> '''<STR_LIT>''' <EOL> _validate_not_none ( '<STR_LIT>' , container_name ) <EOL> _validate_not_none ( '<STR_LIT>' , blob_name ) <EOL> request = HTTPRequest ( ) <EOL> request . method = '<STR_LIT>' <EOL> request . host = self . _get_host ( ) <EOL> request . path = _get_path ( container_name , blob_name ) <EOL> request . query = [ <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , _int_to_str ( timeout ) ) , <EOL> ] <EOL> request . headers = [ <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , _to_str ( lease_id ) ) , <EOL> ( '<STR_LIT>' , <EOL> _to_str ( if_sequence_number_lte ) ) , <EOL> ( '<STR_LIT>' , <EOL> _to_str ( if_sequence_number_lt ) ) , <EOL> ( '<STR_LIT>' , <EOL> _to_str ( if_sequence_number_eq ) ) , <EOL> ( '<STR_LIT>' , _datetime_to_utc_string ( if_modified_since ) ) , <EOL> ( '<STR_LIT>' , _datetime_to_utc_string ( if_unmodified_since ) ) , <EOL> ( '<STR_LIT>' , _to_str ( if_match ) ) , <EOL> ( '<STR_LIT>' , _to_str ( if_none_match ) ) <EOL> ] <EOL> _validate_and_format_range_headers ( <EOL> request , <EOL> start_range , <EOL> end_range , <EOL> align_to_page = True ) <EOL> response = self . _perform_request ( request ) <EOL> return _parse_page_properties ( response ) <EOL> def get_page_ranges ( <EOL> self , container_name , blob_name , snapshot = None , start_range = None , <EOL> end_range = None , lease_id = None , if_modified_since = None , <EOL> if_unmodified_since = None , if_match = None , if_none_match = None , timeout = None ) : <EOL> '''<STR_LIT>''' <EOL> _validate_not_none ( '<STR_LIT>' , container_name ) <EOL> _validate_not_none ( '<STR_LIT>' , blob_name ) <EOL> request = HTTPRequest ( ) <EOL> request . method = '<STR_LIT:GET>' <EOL> request . host = self . _get_host ( ) <EOL> request . path = _get_path ( container_name , blob_name ) <EOL> request . query = [ <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , _to_str ( snapshot ) ) , <EOL> ( '<STR_LIT>' , _int_to_str ( timeout ) ) , <EOL> ] <EOL> request . headers = [ <EOL> ( '<STR_LIT>' , _to_str ( lease_id ) ) , <EOL> ( '<STR_LIT>' , _datetime_to_utc_string ( if_modified_since ) ) , <EOL> ( '<STR_LIT>' , _datetime_to_utc_string ( if_unmodified_since ) ) , <EOL> ( '<STR_LIT>' , _to_str ( if_match ) ) , <EOL> ( '<STR_LIT>' , _to_str ( if_none_match ) ) , <EOL> ] <EOL> if start_range is not None : <EOL> _validate_and_format_range_headers ( <EOL> request , <EOL> start_range , <EOL> end_range , <EOL> start_range_required = False , <EOL> end_range_required = False , <EOL> align_to_page = True ) <EOL> response = self . _perform_request ( request ) <EOL> return _convert_xml_to_page_ranges ( response ) <EOL> def get_page_ranges_diff ( <EOL> self , container_name , blob_name , previous_snapshot , snapshot = None , <EOL> start_range = None , end_range = None , lease_id = None , if_modified_since = None , <EOL> if_unmodified_since = None , if_match = None , if_none_match = None , timeout = None ) : <EOL> '''<STR_LIT>''' <EOL> _validate_not_none ( '<STR_LIT>' , container_name ) <EOL> _validate_not_none ( '<STR_LIT>' , blob_name ) <EOL> _validate_not_none ( '<STR_LIT>' , previous_snapshot ) <EOL> request = HTTPRequest ( ) <EOL> request . method = '<STR_LIT:GET>' <EOL> request . host = self . _get_host ( ) <EOL> request . path = _get_path ( container_name , blob_name ) <EOL> request . query = [ <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , _to_str ( snapshot ) ) , <EOL> ( '<STR_LIT>' , _to_str ( previous_snapshot ) ) , <EOL> ( '<STR_LIT>' , _int_to_str ( timeout ) ) , <EOL> ] <EOL> request . headers = [ <EOL> ( '<STR_LIT>' , _to_str ( lease_id ) ) , <EOL> ( '<STR_LIT>' , _datetime_to_utc_string ( if_modified_since ) ) , <EOL> ( '<STR_LIT>' , _datetime_to_utc_string ( if_unmodified_since ) ) , <EOL> ( '<STR_LIT>' , _to_str ( if_match ) ) , <EOL> ( '<STR_LIT>' , _to_str ( if_none_match ) ) , <EOL> ] <EOL> if start_range is not None : <EOL> _validate_and_format_range_headers ( <EOL> request , <EOL> start_range , <EOL> end_range , <EOL> start_range_required = False , <EOL> end_range_required = False , <EOL> align_to_page = True ) <EOL> response = self . _perform_request ( request ) <EOL> return _convert_xml_to_page_ranges ( response ) <EOL> def set_sequence_number ( <EOL> self , container_name , blob_name , sequence_number_action , sequence_number = None , <EOL> lease_id = None , if_modified_since = None , if_unmodified_since = None , <EOL> if_match = None , if_none_match = None , timeout = None ) : <EOL> '''<STR_LIT>''' <EOL> _validate_not_none ( '<STR_LIT>' , container_name ) <EOL> _validate_not_none ( '<STR_LIT>' , blob_name ) <EOL> _validate_not_none ( '<STR_LIT>' , sequence_number_action ) <EOL> request = HTTPRequest ( ) <EOL> request . method = '<STR_LIT>' <EOL> request . host = self . _get_host ( ) <EOL> request . path = _get_path ( container_name , blob_name ) <EOL> request . query = [ <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , _int_to_str ( timeout ) ) , <EOL> ] <EOL> request . headers = [ <EOL> ( '<STR_LIT>' , _to_str ( sequence_number ) ) , <EOL> ( '<STR_LIT>' , _to_str ( sequence_number_action ) ) , <EOL> ( '<STR_LIT>' , _to_str ( lease_id ) ) , <EOL> ( '<STR_LIT>' , _datetime_to_utc_string ( if_modified_since ) ) , <EOL> ( '<STR_LIT>' , _datetime_to_utc_string ( if_unmodified_since ) ) , <EOL> ( '<STR_LIT>' , _to_str ( if_match ) ) , <EOL> ( '<STR_LIT>' , _to_str ( if_none_match ) ) , <EOL> ] <EOL> response = self . _perform_request ( request ) <EOL> return _parse_page_properties ( response ) <EOL> def resize_blob ( <EOL> self , container_name , blob_name , content_length , <EOL> lease_id = None , if_modified_since = None , if_unmodified_since = None , <EOL> if_match = None , if_none_match = None , timeout = None ) : <EOL> '''<STR_LIT>''' <EOL> _validate_not_none ( '<STR_LIT>' , container_name ) <EOL> _validate_not_none ( '<STR_LIT>' , blob_name ) <EOL> _validate_not_none ( '<STR_LIT>' , content_length ) <EOL> request = HTTPRequest ( ) <EOL> request . method = '<STR_LIT>' <EOL> request . host = self . _get_host ( ) <EOL> request . path = _get_path ( container_name , blob_name ) <EOL> request . query = [ <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , _int_to_str ( timeout ) ) , <EOL> ] <EOL> request . headers = [ <EOL> ( '<STR_LIT>' , _to_str ( content_length ) ) , <EOL> ( '<STR_LIT>' , _to_str ( lease_id ) ) , <EOL> ( '<STR_LIT>' , _datetime_to_utc_string ( if_modified_since ) ) , <EOL> ( '<STR_LIT>' , _datetime_to_utc_string ( if_unmodified_since ) ) , <EOL> ( '<STR_LIT>' , _to_str ( if_match ) ) , <EOL> ( '<STR_LIT>' , _to_str ( if_none_match ) ) , <EOL> ] <EOL> response = self . _perform_request ( request ) <EOL> return _parse_page_properties ( response ) <EOL> def create_blob_from_path ( <EOL> self , container_name , blob_name , file_path , content_settings = None , <EOL> metadata = None , progress_callback = None , max_connections = <NUM_LIT:1> , <EOL> max_retries = <NUM_LIT:5> , retry_wait = <NUM_LIT:1.0> , lease_id = None , if_modified_since = None , <EOL> if_unmodified_since = None , if_match = None , if_none_match = None , timeout = None ) : <EOL> '''<STR_LIT>''' <EOL> _validate_not_none ( '<STR_LIT>' , container_name ) <EOL> _validate_not_none ( '<STR_LIT>' , blob_name ) <EOL> _validate_not_none ( '<STR_LIT>' , file_path ) <EOL> count = path . getsize ( file_path ) <EOL> with open ( file_path , '<STR_LIT:rb>' ) as stream : <EOL> self . create_blob_from_stream ( <EOL> container_name = container_name , <EOL> blob_name = blob_name , <EOL> stream = stream , <EOL> count = count , <EOL> content_settings = content_settings , <EOL> metadata = metadata , <EOL> progress_callback = progress_callback , <EOL> max_connections = max_connections , <EOL> max_retries = max_retries , <EOL> retry_wait = retry_wait , <EOL> lease_id = lease_id , <EOL> if_modified_since = if_modified_since , <EOL> if_unmodified_since = if_unmodified_since , <EOL> if_match = if_match , <EOL> if_none_match = if_none_match , <EOL> timeout = timeout ) <EOL> def create_blob_from_stream ( <EOL> self , container_name , blob_name , stream , count , content_settings = None , <EOL> metadata = None , progress_callback = None , max_connections = <NUM_LIT:1> , <EOL> max_retries = <NUM_LIT:5> , retry_wait = <NUM_LIT:1.0> , lease_id = None , if_modified_since = None , <EOL> if_unmodified_since = None , if_match = None , if_none_match = None , timeout = None ) : <EOL> '''<STR_LIT>''' <EOL> _validate_not_none ( '<STR_LIT>' , container_name ) <EOL> _validate_not_none ( '<STR_LIT>' , blob_name ) <EOL> _validate_not_none ( '<STR_LIT>' , stream ) <EOL> _validate_not_none ( '<STR_LIT:count>' , count ) <EOL> if count < <NUM_LIT:0> : <EOL> raise ValueError ( _ERROR_VALUE_NEGATIVE . format ( '<STR_LIT:count>' ) ) <EOL> if count % _PAGE_ALIGNMENT != <NUM_LIT:0> : <EOL> raise ValueError ( _ERROR_PAGE_BLOB_SIZE_ALIGNMENT . format ( count ) ) <EOL> response = self . create_blob ( <EOL> container_name = container_name , <EOL> blob_name = blob_name , <EOL> content_length = count , <EOL> content_settings = content_settings , <EOL> metadata = metadata , <EOL> lease_id = lease_id , <EOL> if_modified_since = if_modified_since , <EOL> if_unmodified_since = if_unmodified_since , <EOL> if_match = if_match , <EOL> if_none_match = if_none_match , <EOL> timeout = timeout <EOL> ) <EOL> _upload_blob_chunks ( <EOL> blob_service = self , <EOL> container_name = container_name , <EOL> blob_name = blob_name , <EOL> blob_size = count , <EOL> block_size = self . MAX_PAGE_SIZE , <EOL> stream = stream , <EOL> max_connections = max_connections , <EOL> max_retries = max_retries , <EOL> retry_wait = retry_wait , <EOL> progress_callback = progress_callback , <EOL> lease_id = lease_id , <EOL> uploader_class = _PageBlobChunkUploader , <EOL> if_match = response . etag , <EOL> timeout = timeout <EOL> ) <EOL> def create_blob_from_bytes ( <EOL> self , container_name , blob_name , blob , index = <NUM_LIT:0> , count = None , <EOL> content_settings = None , metadata = None , progress_callback = None , <EOL> max_connections = <NUM_LIT:1> , max_retries = <NUM_LIT:5> , retry_wait = <NUM_LIT:1.0> , <EOL> lease_id = None , if_modified_since = None , if_unmodified_since = None , <EOL> if_match = None , if_none_match = None , timeout = None ) : <EOL> '''<STR_LIT>''' <EOL> _validate_not_none ( '<STR_LIT>' , container_name ) <EOL> _validate_not_none ( '<STR_LIT>' , blob_name ) <EOL> _validate_not_none ( '<STR_LIT>' , blob ) <EOL> _validate_type_bytes ( '<STR_LIT>' , blob ) <EOL> if index < <NUM_LIT:0> : <EOL> raise IndexError ( _ERROR_VALUE_NEGATIVE . format ( '<STR_LIT:index>' ) ) <EOL> if count is None or count < <NUM_LIT:0> : <EOL> count = len ( blob ) - index <EOL> stream = BytesIO ( blob ) <EOL> stream . seek ( index ) <EOL> self . create_blob_from_stream ( <EOL> container_name = container_name , <EOL> blob_name = blob_name , <EOL> stream = stream , <EOL> count = count , <EOL> content_settings = content_settings , <EOL> metadata = metadata , <EOL> lease_id = lease_id , <EOL> progress_callback = progress_callback , <EOL> max_connections = max_connections , <EOL> max_retries = max_retries , <EOL> retry_wait = retry_wait , <EOL> if_modified_since = if_modified_since , <EOL> if_unmodified_since = if_unmodified_since , <EOL> if_match = if_match , <EOL> if_none_match = if_none_match , <EOL> timeout = timeout ) </s>
