<s> import mimetypes <EOL> import pyrax <EOL> import re <EOL> import warnings <EOL> from gzip import GzipFile <EOL> try : <EOL> from cStringIO import StringIO <EOL> except ImportError : <EOL> try : <EOL> from StringIO import StringIO <EOL> except ImportError : <EOL> from io import StringIO <EOL> from django . core . files . storage import Storage <EOL> from django . core . files . base import File , ContentFile <EOL> try : <EOL> from django . utils . deconstruct import deconstructible <EOL> except ImportError : <EOL> def deconstructible ( * args , ** kwargs ) : <EOL> def decorator ( klass ) : <EOL> return klass <EOL> if not args : <EOL> return decorator <EOL> return decorator ( * args , ** kwargs ) <EOL> from cumulus . authentication import Auth <EOL> from cumulus . settings import CUMULUS <EOL> HEADER_PATTERNS = tuple ( ( re . compile ( p ) , h ) for p , h in CUMULUS . get ( "<STR_LIT>" , { } ) ) <EOL> def get_content_type ( name , content ) : <EOL> """<STR_LIT>""" <EOL> if hasattr ( content , "<STR_LIT>" ) : <EOL> content_type = content . content_type <EOL> else : <EOL> mime_type , encoding = mimetypes . guess_type ( name ) <EOL> content_type = mime_type <EOL> return content_type <EOL> def get_headers ( name , content_type ) : <EOL> headers = { "<STR_LIT:Content-Type>" : content_type } <EOL> if content_type in CUMULUS . get ( "<STR_LIT>" , [ ] ) : <EOL> headers [ "<STR_LIT>" ] = "<STR_LIT>" <EOL> if CUMULUS [ "<STR_LIT>" ] : <EOL> for pattern , pattern_headers in HEADER_PATTERNS : <EOL> if pattern . match ( name ) : <EOL> headers . update ( pattern_headers . copy ( ) ) <EOL> return headers <EOL> def sync_headers ( cloud_obj , headers = None , header_patterns = HEADER_PATTERNS ) : <EOL> """<STR_LIT>""" <EOL> if headers is None : <EOL> headers = { } <EOL> content_type = getattr ( cloud_obj , "<STR_LIT>" , None ) <EOL> if content_type == "<STR_LIT>" : <EOL> return <EOL> matched_headers = { } <EOL> for pattern , pattern_headers in header_patterns : <EOL> if pattern . match ( cloud_obj . name ) : <EOL> matched_headers . update ( pattern_headers . copy ( ) ) <EOL> matched_headers . update ( cloud_obj . headers ) <EOL> matched_headers . update ( headers ) <EOL> if matched_headers != cloud_obj . headers : <EOL> cloud_obj . headers = matched_headers <EOL> cloud_obj . sync_metadata ( ) <EOL> def get_gzipped_contents ( input_file ) : <EOL> """<STR_LIT>""" <EOL> zbuf = StringIO ( ) <EOL> zfile = GzipFile ( mode = "<STR_LIT:wb>" , compresslevel = <NUM_LIT:6> , fileobj = zbuf ) <EOL> zfile . write ( input_file . read ( ) ) <EOL> zfile . close ( ) <EOL> return ContentFile ( zbuf . getvalue ( ) ) <EOL> @ deconstructible <EOL> class CumulusStorage ( Auth , Storage ) : <EOL> """<STR_LIT>""" <EOL> default_quick_listdir = True <EOL> container_name = CUMULUS [ "<STR_LIT>" ] <EOL> container_uri = CUMULUS [ "<STR_LIT>" ] <EOL> container_ssl_uri = CUMULUS [ "<STR_LIT>" ] <EOL> ttl = CUMULUS [ "<STR_LIT>" ] <EOL> file_ttl = CUMULUS [ "<STR_LIT>" ] <EOL> use_ssl = CUMULUS [ "<STR_LIT>" ] <EOL> def _open ( self , name , mode = "<STR_LIT:rb>" ) : <EOL> """<STR_LIT>""" <EOL> return ContentFile ( self . _get_object ( name ) . get ( ) ) <EOL> def _save ( self , name , content ) : <EOL> """<STR_LIT>""" <EOL> content_type = get_content_type ( name , content . file ) <EOL> headers = get_headers ( name , content_type ) <EOL> if self . use_pyrax : <EOL> if headers . get ( "<STR_LIT>" ) == "<STR_LIT>" : <EOL> content = get_gzipped_contents ( content ) <EOL> self . connection . store_object ( container = self . container_name , <EOL> obj_name = name , <EOL> data = content . read ( ) , <EOL> content_type = content_type , <EOL> content_encoding = headers . get ( "<STR_LIT>" , None ) , <EOL> ttl = self . file_ttl , <EOL> etag = None ) <EOL> self . connection . set_object_metadata ( container = self . container_name , <EOL> obj = name , <EOL> metadata = headers , <EOL> prefix = '<STR_LIT>' , <EOL> clear = True ) <EOL> else : <EOL> self . connection . put_object ( self . container_name , name , <EOL> content , headers = headers ) <EOL> return name <EOL> def delete ( self , name ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> self . connection . delete_object ( self . container_name , name ) <EOL> except pyrax . exceptions . ClientException as exc : <EOL> if exc . http_status == <NUM_LIT> : <EOL> pass <EOL> else : <EOL> raise <EOL> except pyrax . exceptions . NoSuchObject : <EOL> pass <EOL> def exists ( self , name ) : <EOL> """<STR_LIT>""" <EOL> return bool ( self . _get_object ( name ) ) <EOL> def size ( self , name ) : <EOL> """<STR_LIT>""" <EOL> file_object = self . _get_object ( name ) <EOL> if file_object : <EOL> return file_object . total_bytes <EOL> else : <EOL> return <NUM_LIT:0> <EOL> def url ( self , name ) : <EOL> """<STR_LIT>""" <EOL> return u"<STR_LIT>" . format ( self . container_url , name ) <EOL> def listdir ( self , path ) : <EOL> """<STR_LIT>""" <EOL> files = [ ] <EOL> if path and not path . endswith ( "<STR_LIT:/>" ) : <EOL> path = u"<STR_LIT>" . format ( path ) <EOL> path_len = len ( path ) <EOL> for name in [ x [ "<STR_LIT:name>" ] for x in <EOL> self . connection . get_container ( self . container_name , full_listing = True ) [ <NUM_LIT:1> ] ] : <EOL> files . append ( name [ path_len : ] ) <EOL> return ( [ ] , files ) <EOL> def full_listdir ( self , path ) : <EOL> """<STR_LIT>""" <EOL> dirs = set ( ) <EOL> files = [ ] <EOL> if path and not path . endswith ( "<STR_LIT:/>" ) : <EOL> path = u"<STR_LIT>" . format ( path ) <EOL> path_len = len ( path ) <EOL> for name in [ x [ "<STR_LIT:name>" ] for x in <EOL> self . connection . get_container ( self . container_name , full_listing = True ) [ <NUM_LIT:1> ] ] : <EOL> name = name [ path_len : ] <EOL> slash = name [ <NUM_LIT:1> : - <NUM_LIT:1> ] . find ( "<STR_LIT:/>" ) + <NUM_LIT:1> <EOL> if slash : <EOL> dirs . add ( name [ : slash ] ) <EOL> elif name : <EOL> files . append ( name ) <EOL> dirs = list ( dirs ) <EOL> dirs . sort ( ) <EOL> return ( dirs , files ) <EOL> class CumulusStaticStorage ( CumulusStorage ) : <EOL> """<STR_LIT>""" <EOL> container_name = CUMULUS [ "<STR_LIT>" ] <EOL> container_uri = CUMULUS [ "<STR_LIT>" ] <EOL> container_ssl_uri = CUMULUS [ "<STR_LIT>" ] <EOL> class ThreadSafeCumulusStorage ( CumulusStorage ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> super ( ThreadSafeCumulusStorage , self ) . __init__ ( * args , ** kwargs ) <EOL> import threading <EOL> self . local_cache = threading . local ( ) <EOL> def _get_connection ( self ) : <EOL> if not hasattr ( self . local_cache , "<STR_LIT>" ) : <EOL> super ( ThreadSafeSwiftclientStorage , self ) . _get_connection ( ) <EOL> self . local_cache . connection = connection <EOL> return self . local_cache . connection <EOL> connection = property ( _get_connection , CumulusStorage . _set_connection ) <EOL> def _get_container ( self ) : <EOL> if not hasattr ( self . local_cache , "<STR_LIT>" ) : <EOL> container = self . connection . create_container ( self . container_name ) <EOL> self . local_cache . container = container <EOL> return self . local_cache . container <EOL> container = property ( _get_container , CumulusStorage . _set_container ) <EOL> class SwiftclientStorage ( CumulusStorage ) : <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> warnings . warn ( "<STR_LIT>" , DeprecationWarning ) <EOL> super ( SwiftclientStorage , self ) . __init__ ( ) <EOL> class SwiftclientStaticStorage ( CumulusStaticStorage ) : <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> warnings . warn ( "<STR_LIT>" , DeprecationWarning ) <EOL> super ( SwiftclientStaticStorage , self ) . __init__ ( ) <EOL> class ThreadSafeSwiftclientStorage ( ThreadSafeCumulusStorage ) : <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> warnings . warn ( "<STR_LIT>" , DeprecationWarning ) <EOL> super ( ThreadSafeSwiftclientStorage , self ) . __init__ ( ) </s>
