def setUp(self): self.filepath = 'delete_test_1.txt' # delete the file to start with clean slate deleteFile(self.filepath) # create new file in its place with open(self.filepath, 'w') as file: file.write('test') try: fileExists(self.filepath, throw_error=True) except: self.fail("deleteFile() raised an error unexpectedly!")
def testAyxWriteData(self): # loop through all valid connections for connection_num in self.output_connections: # get filepath filepath = os.path.abspath(outputFilename(connection_num)) # it shouldn't exist (after setup executed) if fileExists(filepath): raise FileExistsError("File already exists: {}".format(filepath)) # write data write(self.data, connection_num) print(filepath) result = fileExists(filepath) self.assertTrue(result)
def __returnConnection(self): error_msg = "Unable to connect to input data" try: # if file exists, and not creating a new db, then throw error if self.create_new: if os.path.isfile(self.filepath): try: deleteFile(self.filepath, debug=self.debug) except: raise PermissionError( "unable to delete file: {}".format(self.filepath)) if not os.access(os.path.dirname(self.filepath), os.W_OK): raise PermissionError( "unable to write to filepath: {}".format( self.filepath)) return None elif fileExists(self.filepath, throw_error=not (self.create_new), msg=error_msg): # open connection and attempt to read one row # to confirm that it is a valid file if self.fileformat.filetype == "sqlite": connection = sqlite3.connect(self.filepath) connection.execute("select * from sqlite_master limit 1") elif self.fileformat.filetype == "yxdb": connection = pyxdb.AlteryxYXDB() connection.open(self.filepath) # test that its a real yxdb file connection.get_num_records() else: self.__formatNotSupportedYet() return connection except: try: connection.close() except: pass raise ConnectionError(fileErrorMsg(error_msg, self.filepath))
def __getConfigJSON(self): if self.debug: print("Attempting to read in config file ({})".format( self.absolute_path)) try: # check that config exists if fileExists( self.absolute_path, True, "Cached data unavailable -- run the workflow to make the input data available in Jupyter notebook", ): if self.debug: print("Config file found -- {}".format(self.absolute_path)) # read in config file with open(self.absolute_path) as f: config = json.load(f) # check if config is a dict if not isinstance(config, dict): raise TypeError("Input config must be a python dict") return config except: print("Config file error -- {}".format(self.absolute_path)) raise
def testWriteDeleteWriteDelete(self): # delete the file to start with clean slate deleteFile(self.filepath) # does file exist? result = fileExists(self.filepath) self.assertFalse(result)