<s> import os <EOL> import shutil <EOL> from lockfile import LockFile <EOL> from archivekit . store . common import Store , StoreObject , MANIFEST <EOL> from archivekit . util import safe_id , fullpath <EOL> LENGTH = <NUM_LIT:2> <EOL> class FileStore ( Store ) : <EOL> def __init__ ( self , path = None , ** kwargs ) : <EOL> self . path = fullpath ( path ) <EOL> if os . path . exists ( path ) and not os . path . isdir ( path ) : <EOL> raise ValueError ( '<STR_LIT>' % path ) <EOL> def get_object ( self , collection , package_id , path ) : <EOL> return FileStoreObject ( self , collection , package_id , path ) <EOL> def list_collections ( self ) : <EOL> if self . path is None : <EOL> return <EOL> for collection in os . listdir ( self . path ) : <EOL> if os . path . isdir ( os . path . join ( self . path , collection ) ) : <EOL> yield collection <EOL> def list_packages ( self , collection ) : <EOL> if self . path is None : <EOL> return <EOL> coll_path = os . path . join ( self . path , collection ) <EOL> if not os . path . exists ( coll_path ) : <EOL> return <EOL> for ( dirpath , dirnames , filenames ) in os . walk ( coll_path ) : <EOL> if MANIFEST not in filenames : <EOL> continue <EOL> _ , id = os . path . split ( dirpath ) <EOL> if self . _make_path ( collection , id ) == dirpath : <EOL> yield id <EOL> def _make_path ( self , collection , package_id ) : <EOL> id = safe_id ( package_id , len = LENGTH ) <EOL> path = os . path . join ( self . path , collection , * id [ : LENGTH ] ) <EOL> return os . path . join ( path , id ) <EOL> def list_resources ( self , collection , package_id ) : <EOL> prefix = self . _make_path ( collection , package_id ) <EOL> if not os . path . exists ( prefix ) : <EOL> return <EOL> skip = os . path . join ( prefix , MANIFEST ) <EOL> for ( dirpath , dirnames , filenames ) in os . walk ( self . path ) : <EOL> for filename in filenames : <EOL> path = os . path . join ( dirpath , filename ) <EOL> if path == skip : <EOL> continue <EOL> yield os . path . relpath ( path , start = prefix ) <EOL> def __repr__ ( self ) : <EOL> return '<STR_LIT>' % self . path <EOL> def __unicode__ ( self ) : <EOL> return self . path <EOL> class FileStoreObject ( StoreObject ) : <EOL> def __init__ ( self , store , collection , package_id , path ) : <EOL> self . store = store <EOL> self . package_id = package_id <EOL> self . path = path <EOL> pkg_path = self . store . _make_path ( collection , package_id ) <EOL> self . _abs_path = os . path . join ( pkg_path , path ) <EOL> self . _abs_dir = os . path . dirname ( self . _abs_path ) <EOL> self . _lock = LockFile ( self . _abs_path ) <EOL> def exists ( self ) : <EOL> return os . path . exists ( self . _abs_path ) <EOL> def _prepare ( self ) : <EOL> try : <EOL> os . makedirs ( self . _abs_dir ) <EOL> except : <EOL> pass <EOL> def save_fileobj ( self , fileobj ) : <EOL> self . _prepare ( ) <EOL> with self . _lock : <EOL> with open ( self . _abs_path , '<STR_LIT:wb>' ) as fh : <EOL> shutil . copyfileobj ( fileobj , fh ) <EOL> def save_file ( self , file_name , destructive = False ) : <EOL> self . _prepare ( ) <EOL> with self . _lock : <EOL> if destructive : <EOL> shutil . move ( file_name , self . _abs_path ) <EOL> else : <EOL> shutil . copy ( file_name , self . _abs_path ) <EOL> def save_data ( self , data ) : <EOL> self . _prepare ( ) <EOL> with self . _lock : <EOL> with open ( self . _abs_path , '<STR_LIT:wb>' ) as fh : <EOL> fh . write ( data ) <EOL> def load_fileobj ( self ) : <EOL> if not self . exists ( ) : <EOL> return <EOL> with self . _lock : <EOL> return open ( self . _abs_path , '<STR_LIT:rb>' ) <EOL> def load_data ( self ) : <EOL> if not self . exists ( ) : <EOL> return <EOL> with self . _lock : <EOL> with open ( self . _abs_path , '<STR_LIT:rb>' ) as fh : <EOL> return fh . read ( ) <EOL> def local_path ( self ) : <EOL> return self . _abs_path <EOL> def __repr__ ( self ) : <EOL> return '<STR_LIT>' % self . _abs_path <EOL> def __unicode__ ( self ) : <EOL> return self . _abs_path </s>
