From 11fdae1553997e73f6e6a0d411b033af05aebfb8 Mon Sep 17 00:00:00 2001 From: WorldTeacher <41587052+WorldTeacher@users.noreply.github.com> Date: Wed, 7 Feb 2024 11:37:31 +0100 Subject: [PATCH] create and delete files --- src/backend/create_file.py | 33 +++++++++++++++++++++++++++++ src/backend/delete_temp_contents.py | 13 ++++++++++++ 2 files changed, 46 insertions(+) create mode 100644 src/backend/create_file.py create mode 100644 src/backend/delete_temp_contents.py diff --git a/src/backend/create_file.py b/src/backend/create_file.py new file mode 100644 index 0000000..3954475 --- /dev/null +++ b/src/backend/create_file.py @@ -0,0 +1,33 @@ +import os +from pathlib import Path +from omegaconf import OmegaConf +from src.backend.database import Database + +db = Database() +config = OmegaConf.load("config.yaml") + +def recreateFile(name, app_id, filetype, open=True)->Path: + """ + recreateFile creates a file from the database and opens it in the respective program, if the open parameter is set to True. + + Args: + ---- + - name (str): The filename selected by the user. + - app_id (str): the id of the apparatus. + - filetype (str): the extension of the file to be created. + - open (bool, optional): Determines if the file should be opened. Defaults to True. + + Returns: + ------- + - Path: Absolute path to the file. + """ + path = db.recreateFile(name, app_id, filetype=filetype) + path = Path(path) + if open: + if os.getenv("OS") == "Windows_NT": + path = path.resolve() + os.startfile(path) + else: + path = path.resolve() + os.system(f"open {path}") + return path \ No newline at end of file diff --git a/src/backend/delete_temp_contents.py b/src/backend/delete_temp_contents.py new file mode 100644 index 0000000..5eabca7 --- /dev/null +++ b/src/backend/delete_temp_contents.py @@ -0,0 +1,13 @@ +import os +from omegaconf import OmegaConf +config = OmegaConf.load("config.yaml") + +def delete_temp_contents(): + """ + delete_temp_contents deletes the contents of the temp directory. + """ + for root, dirs, files in os.walk(config.database.tempdir): + for file in files: + os.remove(os.path.join(root, file)) + for dir in dirs: + os.rmdir(os.path.join(root, dir)) \ No newline at end of file