<s> __version__ = '<STR_LIT>' <EOL> __author__ = '<STR_LIT>' <EOL> __doc__ = '''<STR_LIT>''' <EOL> import os <EOL> import datastore . core <EOL> def ensure_directory_exists ( directory ) : <EOL> '''<STR_LIT>''' <EOL> if not os . path . exists ( directory ) : <EOL> os . makedirs ( directory ) <EOL> elif os . path . isfile ( directory ) : <EOL> raise RuntimeError ( '<STR_LIT>' % directory ) <EOL> class FileSystemDatastore ( datastore . Datastore ) : <EOL> '''<STR_LIT>''' <EOL> object_extension = '<STR_LIT>' <EOL> ignore_list = list ( ) <EOL> def __init__ ( self , root , case_sensitive = True ) : <EOL> '''<STR_LIT>''' <EOL> root = os . path . normpath ( root ) <EOL> if not root : <EOL> errstr = '<STR_LIT>' <EOL> raise ValueError ( errstr ) <EOL> ensure_directory_exists ( root ) <EOL> self . root_path = root <EOL> self . case_sensitive = bool ( case_sensitive ) <EOL> def relative_path ( self , key ) : <EOL> '''<STR_LIT>''' <EOL> key = str ( key ) <EOL> key = key . replace ( '<STR_LIT::>' , '<STR_LIT:/>' ) <EOL> key = key [ <NUM_LIT:1> : ] <EOL> if not self . case_sensitive : <EOL> key = key . lower ( ) <EOL> return os . path . normpath ( key ) <EOL> def path ( self , key ) : <EOL> '''<STR_LIT>''' <EOL> return os . path . join ( self . root_path , self . relative_path ( key ) ) <EOL> def relative_object_path ( self , key ) : <EOL> '''<STR_LIT>''' <EOL> return self . relative_path ( key ) + self . object_extension <EOL> def object_path ( self , key ) : <EOL> '''<STR_LIT>''' <EOL> return os . path . join ( self . root_path , self . relative_object_path ( key ) ) <EOL> def _write_object ( self , path , value ) : <EOL> '''<STR_LIT>''' <EOL> ensure_directory_exists ( os . path . dirname ( path ) ) <EOL> with open ( path , '<STR_LIT:w>' ) as f : <EOL> f . write ( value ) <EOL> def _read_object ( self , path ) : <EOL> '''<STR_LIT>''' <EOL> if not os . path . exists ( path ) : <EOL> return None <EOL> if os . path . isdir ( path ) : <EOL> raise RuntimeError ( '<STR_LIT>' % path ) <EOL> with open ( path ) as f : <EOL> file_contents = f . read ( ) <EOL> return file_contents <EOL> def _read_object_gen ( self , iterable ) : <EOL> '''<STR_LIT>''' <EOL> for filename in iterable : <EOL> yield self . _read_object ( filename ) <EOL> def get ( self , key ) : <EOL> '''<STR_LIT>''' <EOL> path = self . object_path ( key ) <EOL> return self . _read_object ( path ) <EOL> def put ( self , key , value ) : <EOL> '''<STR_LIT>''' <EOL> path = self . object_path ( key ) <EOL> self . _write_object ( path , value ) <EOL> def delete ( self , key ) : <EOL> '''<STR_LIT>''' <EOL> path = self . object_path ( key ) <EOL> if os . path . exists ( path ) : <EOL> os . remove ( path ) <EOL> def query ( self , query ) : <EOL> '''<STR_LIT>''' <EOL> path = self . path ( query . key ) <EOL> if os . path . exists ( path ) : <EOL> filenames = os . listdir ( path ) <EOL> filenames = list ( set ( filenames ) - set ( self . ignore_list ) ) <EOL> filenames = map ( lambda f : os . path . join ( path , f ) , filenames ) <EOL> iterable = self . _read_object_gen ( filenames ) <EOL> else : <EOL> iterable = list ( ) <EOL> return query ( iterable ) <EOL> def contains ( self , key ) : <EOL> '''<STR_LIT>''' <EOL> path = self . object_path ( key ) <EOL> return os . path . exists ( path ) and os . path . isfile ( path ) </s>
