<s> import os <EOL> from azure . storage . blob . pageblobservice import PageBlobService <EOL> from . . utils . xz import XZ <EOL> from . . azurectl_exceptions import ( <EOL> AzureStorageFileNotFound , <EOL> AzureStorageStreamError , <EOL> AzureStorageUploadError , <EOL> AzureStorageDeleteError <EOL> ) <EOL> from . . utils . filetype import FileType <EOL> from . page_blob import PageBlob <EOL> from . . logger import log <EOL> class Storage ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , account , container ) : <EOL> self . account = account <EOL> self . account_name = account . storage_name ( ) <EOL> self . account_key = account . storage_key ( ) <EOL> self . blob_service_host_base = self . account . get_blob_service_host_base ( ) <EOL> self . container = container <EOL> self . upload_status = { '<STR_LIT>' : <NUM_LIT:0> , '<STR_LIT>' : <NUM_LIT:0> } <EOL> def upload ( self , image , name = None , max_chunk_size = None , max_attempts = <NUM_LIT:5> ) : <EOL> if not os . path . exists ( image ) : <EOL> raise AzureStorageFileNotFound ( '<STR_LIT>' % image ) <EOL> blob_service = PageBlobService ( <EOL> self . account_name , <EOL> self . account_key , <EOL> endpoint_suffix = self . blob_service_host_base <EOL> ) <EOL> image_type = FileType ( image ) <EOL> blob_name = ( name or image_type . basename ( ) ) <EOL> if not name : <EOL> log . info ( '<STR_LIT>' , blob_name ) <EOL> image_size = self . __upload_byte_size ( image , image_type ) <EOL> try : <EOL> stream = self . __open_upload_stream ( image , image_type ) <EOL> except Exception as e : <EOL> raise AzureStorageStreamError ( <EOL> '<STR_LIT>' % ( type ( e ) . __name__ , format ( e ) ) <EOL> ) <EOL> try : <EOL> page_blob = PageBlob ( <EOL> blob_service , blob_name , self . container , image_size <EOL> ) <EOL> self . __upload_status ( <NUM_LIT:0> , image_size ) <EOL> while True : <EOL> bytes_transfered = page_blob . next ( <EOL> stream , max_chunk_size , max_attempts <EOL> ) <EOL> self . __upload_status ( bytes_transfered , image_size ) <EOL> except StopIteration : <EOL> stream . close ( ) <EOL> self . __upload_status ( image_size , image_size ) <EOL> except Exception as e : <EOL> stream . close ( ) <EOL> raise AzureStorageUploadError ( <EOL> '<STR_LIT>' % ( type ( e ) . __name__ , format ( e ) ) <EOL> ) <EOL> def delete ( self , image ) : <EOL> blob_service = PageBlobService ( <EOL> self . account_name , <EOL> self . account_key , <EOL> endpoint_suffix = self . blob_service_host_base <EOL> ) <EOL> try : <EOL> blob_service . delete_blob ( self . container , image ) <EOL> except Exception as e : <EOL> raise AzureStorageDeleteError ( <EOL> '<STR_LIT>' % ( type ( e ) . __name__ , format ( e ) ) <EOL> ) <EOL> def print_upload_status ( self ) : <EOL> log . progress ( <EOL> self . upload_status [ '<STR_LIT>' ] , <EOL> self . upload_status [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' <EOL> ) <EOL> def __upload_status ( self , current , total ) : <EOL> self . upload_status [ '<STR_LIT>' ] = current <EOL> self . upload_status [ '<STR_LIT>' ] = total <EOL> def __open_upload_stream ( self , image , image_type ) : <EOL> if image_type . is_xz ( ) : <EOL> return XZ . open ( image ) <EOL> return open ( image ) <EOL> def __upload_byte_size ( self , image , image_type ) : <EOL> if image_type . is_xz ( ) : <EOL> return XZ . uncompressed_size ( image ) <EOL> return os . path . getsize ( image ) </s>
