def __enter__(self) -> Database: logger.info(f'Entering database context.') self.context = True if not os.path.exists(DatabasePath.get()): self.create_db = True self.connection = sqlite3.connect(DatabasePath.get()) self.cursor = self.connection.cursor() if self.create_db: self.create_database() return self
def remove_db_file() -> None: """ Function to remove a database file if it exists. This function uses the DatabasePath class from `S3_File_Uploader.__init__.py` to determine what database file to remove. """ # Have to create an instance variable of DatabasePath since the tests create different databases # depending on the test that is running. # I tested by just entering `DatabasePath.get()` in the os commands bellow and the tests fail. db_file_path = DatabasePath.get() if os.path.exists(db_file_path): # Since the tests run faster than what Python can remove the file, # the function is now recursive to wait until the program actually ends # before removing the database file. try: os.remove(db_file_path) except PermissionError: time.sleep(1) remove_db_file()