From 4e117fe84f32567334c6f98e0a3ae871109efa79 Mon Sep 17 00:00:00 2001 From: WorldTeacher <41587052+WorldTeacher@users.noreply.github.com> Date: Wed, 11 Sep 2024 15:58:17 +0200 Subject: [PATCH] filehandler --- filehandle.py | 103 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 103 insertions(+) create mode 100644 filehandle.py diff --git a/filehandle.py b/filehandle.py new file mode 100644 index 0000000..43c8bdb --- /dev/null +++ b/filehandle.py @@ -0,0 +1,103 @@ +import os +import shutil +from pathlib import Path +ORIGIN = "C:/testing/source_2" +BACKUP = "C:/Databasebackup" +FILE = "database.db" +def handle_file(): + """ + Handles file creation, movement, and backup based on path and backup existence. + + Args: + path (str): The desired path for the file. + filename (str): The name of the file. + backup_location (str): The location for backups. + + Returns: + str: The final path of the file. + """ + full = Path(ORIGIN) / FILE + full_path = str(full) + backup = Path(BACKUP) / FILE + backup_path = str(backup) + origin_reachable = os.path.exists(full_path) + backup_reachable = os.path.exists(backup_path) + print(origin_reachable, backup_reachable) + if not origin_reachable and not backup_reachable: + make_dirs(ORIGIN, BACKUP) + + # if not os.path.exists(full_path): + # print("File does not exist, creating file") + # create_file(full_path) + if os.path.exists(backup_path) and ".backup" in os.listdir(BACKUP): + print("Used backup previously, overwriting file") + #overwrite file at source with backup + shutil.copy(backup_path, full_path) + os.remove(BACKUP + "/.backup") + + +def make_dirs(full_path, backup_path): + """ + Creates directories if they do not exist and moves the file to the desired location. + + Args: + full_path (str): The full path of the file. + backup_path (str): The backup path of the file. + """ + if not os.path.exists(full_path): + os.makedirs(full_path) + if not os.path.exists(backup_path): + os.makedirs(backup_path) + +def create_file(full_path): + """ + Creates a file at the desired location. + + Args: + full_path (str): The full path of the file. + """ + with open(full_path, "w") as f: + f.write("") + +def handle_folder_reachability(original_path, backup_path): + """ + Checks if the original folder is reachable. If not, creates a backup. + If the original folder becomes reachable again, restores the backup. + + Args: + original_path (str): Path to the original folder. + backup_path (str): Path to the backup folder. + + Returns: + str: Path to the current accessible folder. + """ + + backup_file = os.path.join(backup_path, ".backup") + try: + os.makedirs(original_path) + except FileExistsError: + pass + + if not os.path.exists(original_path): + #original folder not reachable, use backup path and create .backup file + if not os.path.exists(backup_path): + os.makedirs(backup_path) + with open(backup_file, "w") as f: + f.write("") + + # Create an empty backup file as a marker + return backup_path +"/" + FILE + + else: + # Original folder is reachable, check for backup + if os.path.exists(backup_file): + # Restore backup + shutil.rmtree(original_path) # Remove original folder to avoid conflicts + shutil.move(backup_path, original_path) + #os.remove(backup_file) + #remove backup file from original path + os.remove(original_path + "/.backup") + os.makedirs(backup_path) + return original_path +"/" + FILE +if __name__=="__main__": + print(handle_folder_reachability(ORIGIN, BACKUP)) # should create a new file at C:/newdatabase/database.db) \ No newline at end of file