<s> import os <EOL> import errno <EOL> import urlparse <EOL> import itertools <EOL> from django . conf import settings <EOL> from django . core . exceptions import ImproperlyConfigured , SuspiciousOperation <EOL> from django . core . files import locks , File <EOL> from django . core . files . move import file_move_safe <EOL> from django . utils . encoding import force_unicode , filepath_to_uri <EOL> from django . utils . functional import LazyObject <EOL> from django . utils . importlib import import_module <EOL> from django . utils . text import get_valid_filename <EOL> from django . utils . _os import safe_join <EOL> __all__ = ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> class Storage ( object ) : <EOL> """<STR_LIT>""" <EOL> def open ( self , name , mode = '<STR_LIT:rb>' , mixin = None ) : <EOL> """<STR_LIT>""" <EOL> file = self . _open ( name , mode ) <EOL> if mixin : <EOL> file . __class__ = type ( mixin . __name__ , ( mixin , file . __class__ ) , { } ) <EOL> return file <EOL> def save ( self , name , content ) : <EOL> """<STR_LIT>""" <EOL> if name is None : <EOL> name = content . name <EOL> name = self . get_available_name ( name ) <EOL> name = self . _save ( name , content ) <EOL> return force_unicode ( name . replace ( '<STR_LIT:\\>' , '<STR_LIT:/>' ) ) <EOL> def get_valid_name ( self , name ) : <EOL> """<STR_LIT>""" <EOL> return get_valid_filename ( name ) <EOL> def get_available_name ( self , name ) : <EOL> """<STR_LIT>""" <EOL> dir_name , file_name = os . path . split ( name ) <EOL> file_root , file_ext = os . path . splitext ( file_name ) <EOL> count = itertools . count ( <NUM_LIT:1> ) <EOL> while self . exists ( name ) : <EOL> name = os . path . join ( dir_name , "<STR_LIT>" % ( file_root , count . next ( ) , file_ext ) ) <EOL> return name <EOL> def path ( self , name ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError ( "<STR_LIT>" ) <EOL> def delete ( self , name ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError ( ) <EOL> def exists ( self , name ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError ( ) <EOL> def listdir ( self , path ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError ( ) <EOL> def size ( self , name ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError ( ) <EOL> def url ( self , name ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError ( ) <EOL> class FileSystemStorage ( Storage ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , location = None , base_url = None ) : <EOL> if location is None : <EOL> location = settings . MEDIA_ROOT <EOL> if base_url is None : <EOL> base_url = settings . MEDIA_URL <EOL> self . location = os . path . abspath ( location ) <EOL> self . base_url = base_url <EOL> def _open ( self , name , mode = '<STR_LIT:rb>' ) : <EOL> return File ( open ( self . path ( name ) , mode ) ) <EOL> def _save ( self , name , content ) : <EOL> full_path = self . path ( name ) <EOL> directory = os . path . dirname ( full_path ) <EOL> if not os . path . exists ( directory ) : <EOL> os . makedirs ( directory ) <EOL> elif not os . path . isdir ( directory ) : <EOL> raise IOError ( "<STR_LIT>" % directory ) <EOL> while True : <EOL> try : <EOL> if hasattr ( content , '<STR_LIT>' ) : <EOL> file_move_safe ( content . temporary_file_path ( ) , full_path ) <EOL> content . close ( ) <EOL> else : <EOL> fd = os . open ( full_path , os . O_WRONLY | os . O_CREAT | os . O_EXCL | getattr ( os , '<STR_LIT>' , <NUM_LIT:0> ) ) <EOL> try : <EOL> locks . lock ( fd , locks . LOCK_EX ) <EOL> for chunk in content . chunks ( ) : <EOL> os . write ( fd , chunk ) <EOL> finally : <EOL> locks . unlock ( fd ) <EOL> os . close ( fd ) <EOL> except OSError , e : <EOL> if e . errno == errno . EEXIST : <EOL> name = self . get_available_name ( name ) <EOL> full_path = self . path ( name ) <EOL> else : <EOL> raise <EOL> else : <EOL> break <EOL> if settings . FILE_UPLOAD_PERMISSIONS is not None : <EOL> os . chmod ( full_path , settings . FILE_UPLOAD_PERMISSIONS ) <EOL> return name <EOL> def delete ( self , name ) : <EOL> name = self . path ( name ) <EOL> if os . path . exists ( name ) : <EOL> os . remove ( name ) <EOL> def exists ( self , name ) : <EOL> return os . path . exists ( self . path ( name ) ) <EOL> def listdir ( self , path ) : <EOL> path = self . path ( path ) <EOL> directories , files = [ ] , [ ] <EOL> for entry in os . listdir ( path ) : <EOL> if os . path . isdir ( os . path . join ( path , entry ) ) : <EOL> directories . append ( entry ) <EOL> else : <EOL> files . append ( entry ) <EOL> return directories , files <EOL> def path ( self , name ) : <EOL> try : <EOL> path = safe_join ( self . location , name ) <EOL> except ValueError : <EOL> raise SuspiciousOperation ( "<STR_LIT>" % name ) <EOL> return os . path . normpath ( path ) <EOL> def size ( self , name ) : <EOL> return os . path . getsize ( self . path ( name ) ) <EOL> def url ( self , name ) : <EOL> if self . base_url is None : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> return urlparse . urljoin ( self . base_url , filepath_to_uri ( name ) ) <EOL> def get_storage_class ( import_path = None ) : <EOL> if import_path is None : <EOL> import_path = settings . DEFAULT_FILE_STORAGE <EOL> try : <EOL> dot = import_path . rindex ( '<STR_LIT:.>' ) <EOL> except ValueError : <EOL> raise ImproperlyConfigured ( "<STR_LIT>" % import_path ) <EOL> module , classname = import_path [ : dot ] , import_path [ dot + <NUM_LIT:1> : ] <EOL> try : <EOL> mod = import_module ( module ) <EOL> except ImportError , e : <EOL> raise ImproperlyConfigured ( '<STR_LIT>' % ( module , e ) ) <EOL> try : <EOL> return getattr ( mod , classname ) <EOL> except AttributeError : <EOL> raise ImproperlyConfigured ( '<STR_LIT>' % ( module , classname ) ) <EOL> class DefaultStorage ( LazyObject ) : <EOL> def _setup ( self ) : <EOL> self . _wrapped = get_storage_class ( ) ( ) <EOL> default_storage = DefaultStorage ( ) </s>
