<s> from __future__ import with_statement <EOL> import errno <EOL> import os <EOL> import shutil <EOL> import StringIO <EOL> from flask import current_app , url_for <EOL> from . base import Storage , StorageFile , StorageException , reraise as _reraise <EOL> def reraise ( exception ) : <EOL> if exception . errno == errno . EEXIST : <EOL> exception . status = <NUM_LIT> <EOL> elif exception . errno == errno . ENOENT : <EOL> exception . status = <NUM_LIT> <EOL> exception . message = exception . strerror <EOL> _reraise ( exception ) <EOL> class FileSystemStorage ( Storage ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , folder_name = None , file_view = None ) : <EOL> if folder_name is None : <EOL> folder_name = current_app . config . get ( <EOL> '<STR_LIT>' , <EOL> os . path . dirname ( __file__ ) <EOL> ) <EOL> if file_view is None : <EOL> file_view = current_app . config . get ( <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' <EOL> ) <EOL> self . _folder_name = folder_name <EOL> self . _file_view = file_view <EOL> self . _absolute_path = os . path . abspath ( folder_name ) <EOL> @ property <EOL> def folder_name ( self ) : <EOL> return self . _folder_name <EOL> def list_folders ( self ) : <EOL> if not self . _absolute_path : <EOL> raise StorageException ( '<STR_LIT>' ) <EOL> return filter ( <EOL> lambda a : os . path . isdir ( os . path . join ( self . _absolute_path , a ) ) , <EOL> os . listdir ( self . _absolute_path ) <EOL> ) <EOL> def list_files ( self ) : <EOL> if not self . _absolute_path : <EOL> raise StorageException ( '<STR_LIT>' ) <EOL> return filter ( <EOL> lambda a : not os . path . isdir ( os . path . join ( self . _absolute_path , a ) ) , <EOL> os . listdir ( self . _absolute_path ) <EOL> ) <EOL> def _save ( self , name , content ) : <EOL> full_path = self . path ( name ) <EOL> directory = os . path . dirname ( full_path ) <EOL> try : <EOL> self . create_folder ( directory ) <EOL> except StorageException , e : <EOL> if e . status_code != <NUM_LIT> : <EOL> raise e <EOL> with open ( full_path , '<STR_LIT:wb>' ) as destination : <EOL> buffer_size = <NUM_LIT> <EOL> if isinstance ( content , basestring ) : <EOL> io = StringIO . StringIO ( ) <EOL> io . write ( content ) <EOL> content = io <EOL> content . seek ( <NUM_LIT:0> ) <EOL> try : <EOL> shutil . copyfileobj ( content , destination , buffer_size ) <EOL> except OSError , e : <EOL> reraise ( e ) <EOL> return self . file_class ( self , name ) <EOL> def open ( self , name , mode = '<STR_LIT:rb>' ) : <EOL> try : <EOL> file_ = self . file_class ( self , name ) <EOL> file_ . file <EOL> return file_ <EOL> except IOError , e : <EOL> reraise ( e ) <EOL> def delete_folder ( self , name ) : <EOL> path = self . path ( name ) <EOL> try : <EOL> return shutil . rmtree ( path ) <EOL> except OSError , e : <EOL> reraise ( e ) <EOL> def create_folder ( self , path ) : <EOL> try : <EOL> return os . makedirs ( path ) <EOL> except OSError , e : <EOL> reraise ( e ) <EOL> def delete ( self , name ) : <EOL> name = self . path ( name ) <EOL> try : <EOL> return os . remove ( name ) <EOL> except OSError , e : <EOL> reraise ( e ) <EOL> def exists ( self , name ) : <EOL> return os . path . exists ( self . path ( name ) ) <EOL> def path ( self , name ) : <EOL> return os . path . normpath ( os . path . join ( self . _absolute_path , name ) ) <EOL> def url ( self , name ) : <EOL> return url_for ( self . _file_view , filename = name ) <EOL> @ property <EOL> def file_class ( self ) : <EOL> return FileSystemStorageFile <EOL> class FileSystemStorageFile ( StorageFile ) : <EOL> _file = None <EOL> def __init__ ( self , storage , name = None , prefix = '<STR_LIT>' ) : <EOL> self . _storage = storage <EOL> if name is not None : <EOL> self . name = name <EOL> self . prefix = prefix <EOL> @ property <EOL> def file ( self ) : <EOL> if not self . _file : <EOL> self . _file = open ( self . path , '<STR_LIT:rb>' ) <EOL> return self . _file <EOL> @ property <EOL> def path ( self ) : <EOL> return self . _storage . path ( self . name ) <EOL> @ property <EOL> def last_modified ( self ) : <EOL> return os . path . getmtime ( self . path ) <EOL> @ property <EOL> def size ( self ) : <EOL> return os . path . getsize ( self . path ) <EOL> @ property <EOL> def url ( self ) : <EOL> return self . _storage . url ( self . name ) <EOL> def tell ( self ) : <EOL> return self . file . tell ( ) <EOL> def read ( self , size = - <NUM_LIT:1> ) : <EOL> return self . file . read ( size ) <EOL> def seek ( self , offset , whence = os . SEEK_SET ) : <EOL> self . file . seek ( offset , whence ) </s>
