chore: restructured project, updated readme
This commit is contained in:
100
src/utils/files.py
Normal file
100
src/utils/files.py
Normal file
@@ -0,0 +1,100 @@
|
||||
"""File operations and management utilities."""
|
||||
|
||||
import os
|
||||
from pathlib import Path
|
||||
|
||||
from src import LOG_DIR, settings
|
||||
from src.database import Database
|
||||
from src.shared.logging import log
|
||||
|
||||
|
||||
def recreate_file(name: str, app_id: int, filetype: str, open_file: bool = True) -> Path:
|
||||
"""
|
||||
Recreate a file from the database and optionally open it.
|
||||
|
||||
Args:
|
||||
name: The filename selected by the user.
|
||||
app_id: The ID of the apparatus.
|
||||
filetype: The extension of the file to be created.
|
||||
open_file: Determines if the file should be opened. Defaults to True.
|
||||
|
||||
Returns:
|
||||
Absolute path to the file.
|
||||
"""
|
||||
db = Database()
|
||||
path = db.recreateFile(name, app_id, filetype=filetype)
|
||||
path = Path(path)
|
||||
log.info(f"File created: {path}")
|
||||
|
||||
if open_file:
|
||||
path = path.resolve()
|
||||
if os.getenv("OS") == "Windows_NT":
|
||||
os.startfile(path)
|
||||
else:
|
||||
os.system(f"open {path}")
|
||||
|
||||
return path
|
||||
|
||||
|
||||
# Legacy name for backwards compatibility
|
||||
def recreateFile(name: str, app_id: int, filetype: str, open: bool = True) -> Path:
|
||||
"""Legacy function name - use recreate_file instead."""
|
||||
return recreate_file(name, app_id, filetype, open)
|
||||
|
||||
|
||||
def recreate_elsa_file(filename: str, filetype: str, open_file: bool = True) -> Path:
|
||||
"""
|
||||
Recreate an ELSA file from the database and optionally open it.
|
||||
|
||||
Args:
|
||||
filename: The filename selected by the user.
|
||||
filetype: The file extension.
|
||||
open_file: Determines if the file should be opened. Defaults to True.
|
||||
|
||||
Returns:
|
||||
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")
|
||||
|
||||
db = Database()
|
||||
path = db.recreateElsaFile(filename, filetype)
|
||||
path = Path(path)
|
||||
|
||||
if open_file:
|
||||
path = path.resolve()
|
||||
if os.getenv("OS") == "Windows_NT":
|
||||
os.startfile(path)
|
||||
else:
|
||||
os.system(f"open {path}")
|
||||
|
||||
return path
|
||||
|
||||
|
||||
# Legacy name for backwards compatibility
|
||||
def recreateElsaFile(filename: str, filetype: str, open: bool = True) -> Path:
|
||||
"""Legacy function name - use recreate_elsa_file instead."""
|
||||
return recreate_elsa_file(filename, filetype, open)
|
||||
|
||||
|
||||
def delete_temp_contents() -> None:
|
||||
"""Delete the contents of the temp directory."""
|
||||
database = settings.database
|
||||
path = database.temp.expanduser()
|
||||
|
||||
for root, dirs, files in os.walk(path, topdown=False):
|
||||
for file in files:
|
||||
try:
|
||||
os.remove(os.path.join(root, file))
|
||||
except Exception as e:
|
||||
log.warning(f"Could not remove file {file}: {e}")
|
||||
for dir in dirs:
|
||||
try:
|
||||
os.rmdir(os.path.join(root, dir))
|
||||
except Exception as e:
|
||||
log.warning(f"Could not remove directory {dir}: {e}")
|
||||
|
||||
log.info(f"Temp directory cleared: {path}")
|
||||
Reference in New Issue
Block a user