Files
SemesterapparatsManager/src/backend/create_file.py

73 lines
2.2 KiB
Python

import os
from pathlib import Path
from src.backend.database import Database
import loguru
import sys
from src import LOG_DIR
log = loguru.logger
log.remove()
log.add(sys.stdout, level="INFO")
log.add(f"{LOG_DIR}/application.log", rotation="1 MB", retention="10 days")
db = Database()
def recreateFile(name: str, app_id: int, filetype: str, open: bool = 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)
log.info(f"File created: {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