From 874c8719230e41cdb829d62a697230b807957607 Mon Sep 17 00:00:00 2001 From: WorldTeacher <41587052+WorldTeacher@users.noreply.github.com> Date: Fri, 24 May 2024 10:08:37 +0200 Subject: [PATCH] add elsa function to recreate the file --- src/backend/create_file.py | 102 ++++++++++++++++++++++++------------- 1 file changed, 66 insertions(+), 36 deletions(-) diff --git a/src/backend/create_file.py b/src/backend/create_file.py index 8f86f64..07a4a0a 100644 --- a/src/backend/create_file.py +++ b/src/backend/create_file.py @@ -1,36 +1,66 @@ -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 +import os +from pathlib import Path + +from icecream import ic +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 + + +def recreateElsaFile(filename: str, filetype: str, open=True) -> Path: + """ + recreateElsaFile creates a file from the database and opens it in the respective program, if the open parameter is set to True. + + Args: + ---- + - filename (str): The filename selected by the user. + - open (bool, optional): Determines if the file should be opened. Defaults to True. + + Returns: + ------- + - Path: Absolute path to the file. + """ + if filename.startswith("(") and filename.endswith(")"): + filename = str(filename[1:-1].replace("'", "")) + if not isinstance(filename, str): + raise ValueError("filename must be a string") + path = db.recreateElsaFile(filename, 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