#NEWLINE# def check_file_access( filename , mode = 'r' ) : #NEWLINE# #INDENT# 'Check for file access in the specified `mode`.\n\n    `mode` is one of the modes supported by `File` objects.  If the file\n    indicated by `filename` can be accessed using that `mode`, the\n    function ends successfully.  Else, an ``IOError`` is raised\n    explaining the reason of the failure.\n\n    All this paraphernalia is used to avoid the lengthy and scaring HDF5\n    messages produced when there are problems opening a file.  No\n    changes are ever made to the file system.\n\n    ' #NEWLINE# if ( mode == 'r' ) : #NEWLINE# #INDENT# if ( not os . access ( filename , os . F_OK ) ) : #NEWLINE# #INDENT# raise IOError ( ( '``%s`` does not exist' % ( filename , ) ) ) #NEWLINE# #UNINDENT# if ( not os . path . isfile ( filename ) ) : #NEWLINE# #INDENT# raise IOError ( ( '``%s`` is not a regular file' % ( filename , ) ) ) #NEWLINE# #UNINDENT# if ( not os . access ( filename , os . R_OK ) ) : #NEWLINE# #INDENT# raise IOError ( ( 'file ``%s`` exists but it can not be read' % ( filename , ) ) ) #NEWLINE# #UNINDENT# #UNINDENT# else : #NEWLINE# #INDENT# if ( mode == 'w' ) : #NEWLINE# #INDENT# if os . access ( filename , os . F_OK ) : #NEWLINE# #INDENT# check_file_access ( filename , 'r+' ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# parentname = os . path . dirname ( parentname ) #NEWLINE# if ( not parentname ) : #NEWLINE# #INDENT# parentname = '.' #NEWLINE# #UNINDENT# if ( not os . access ( parentname , os . F_OK ) ) : #NEWLINE# #INDENT# raise IOError ( ( '``%s`` does not exist' % ( parentname , ) ) ) #NEWLINE# #UNINDENT# if ( not os . path . isdir ( parentname ) ) : #NEWLINE# #INDENT# raise IOError ( ( '``%s`` is not a directory' % ( parentname , ) ) ) #NEWLINE# #UNINDENT# if ( not os . access ( parentname , os . W_OK ) ) : #NEWLINE# #INDENT# raise IOError ( ( 'directory ``%s`` exists but it can not be written' % ( parentname , ) ) ) #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# else : #NEWLINE# #INDENT# if ( mode == 'a' ) : #NEWLINE# #INDENT# if os . access ( filename , os . F_OK ) : #NEWLINE# #INDENT# check_file_access ( filename , 'r+' ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# check_file_access ( filename , 'w' ) #NEWLINE# #UNINDENT# #UNINDENT# else : #NEWLINE# #INDENT# if ( mode == 'r+' ) : #NEWLINE# #INDENT# check_file_access ( filename , 'r' ) #NEWLINE# if ( not os . access ( filename , os . W_OK ) ) : #NEWLINE# #INDENT# raise IOError ( ( 'file ``%s`` exists but it can not be written' % ( filename , ) ) ) #NEWLINE# #UNINDENT# #UNINDENT# else : #NEWLINE# #INDENT# raise ValueError ( ( 'invalid mode: %r' % ( mode , ) ) )