Skip to content

Commit cf49e51

Browse files
committed
refactored all functions in content_database.py
1 parent 693cdab commit cf49e51

File tree

2 files changed

+54
-39
lines changed

2 files changed

+54
-39
lines changed

.github/workflows/build.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ jobs:
2727
2828
- name: Build with PyInstaller
2929
run: |
30-
pyinstaller --add-binary "7z/7z.exe;7z" --onefile main.pyw --name DazContentInstaller
30+
pyinstaller --add-binary "7z/7z.exe;7z" --onefile main.py --name DazContentInstaller
3131
3232
- name: Create package directory
3333
run: mkdir -p dist/DazContentInstaller

content_database.py

Lines changed: 53 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import logging
2-
import pathlib
32
import sqlite3
43
import threading
5-
from typing import Tuple
4+
from pathlib import Path
65

76
from helper.config_operations import get_library_path
87

@@ -31,22 +30,34 @@ def connect_database(db_path: str = "database/archives.db") -> sqlite3.Connectio
3130
''')
3231
return conn
3332

34-
# Add a new archive and its files
3533
def add_archive(archive_name: str, files: list[str]) -> None:
36-
"""Add a new archive and its associated files."""
34+
"""
35+
Add a new archive and its associated files to the database.
36+
37+
Args:
38+
archive_name (str): The name of the archive.
39+
files (List[str]): A list of file names to associate with the archive.
40+
"""
3741
with connect_database() as conn:
3842
cursor = conn.cursor()
39-
cursor.execute("INSERT INTO archives (archive_name) VALUES (?)", (archive_name,))
40-
archive_id = cursor.lastrowid
41-
cursor.executemany(
42-
"INSERT INTO files (archive_id, file_name) VALUES (?, ?)",
43-
[(archive_id, file_name) for file_name in files],
44-
)
45-
logging.info(f"Archive '{archive_name}' added with {len(files)} files.")
43+
try:
44+
cursor.execute("INSERT INTO archives (archive_name) VALUES (?)", (archive_name,))
45+
archive_id = cursor.lastrowid
46+
cursor.executemany(
47+
"INSERT INTO files (archive_id, file_name) VALUES (?, ?)",
48+
[(archive_id, file_name) for file_name in files],
49+
)
50+
logging.info(f"Archive '{archive_name}' added with {len(files)} files.")
51+
except sqlite3.IntegrityError:
52+
logging.error(f"Archive '{archive_name}' already exists. Skipping.")
4653

47-
# Retrieve all archives and their file counts
4854
def get_archives() -> list[tuple[str, str]]:
49-
"""Retrieve a list of all archives and their file counts."""
55+
"""
56+
Retrieve a list of all archives and their file counts.
57+
58+
Returns:
59+
list[tuple[str, str]]: A list of tuples containing the archive name and file count.
60+
"""
5061
with connect_database() as conn:
5162
cursor = conn.cursor()
5263
cursor.execute("SELECT id, archive_name FROM archives")
@@ -63,36 +74,40 @@ def get_archives() -> list[tuple[str, str]]:
6374
return archive_list
6475

6576

66-
# Function to delete an archive and its associated files
67-
def delete_archive(archive_name):
68-
with lock:
69-
logger = logging.getLogger(__name__)
70-
conn = connect_database()
71-
cursor = conn.cursor()
72-
# Retrieve the archive ID and associated files
73-
cursor.execute("SELECT id FROM archives WHERE archive_name = ?", (archive_name,))
74-
result = cursor.fetchone()
77+
def delete_archive(archive_name: str) -> None:
78+
"""
79+
Delete an archive and its associated files from the database and filesystem.
7580
76-
if result:
77-
archive_id = result[0]
81+
Args:
82+
archive_name (str): The name of the archive to delete.
83+
"""
84+
with lock: # Ensure thread safety
85+
with connect_database() as conn:
86+
cursor = conn.cursor()
87+
cursor.execute("SELECT id FROM archives WHERE archive_name = ?", (archive_name,))
88+
result = cursor.fetchone()
89+
90+
if not result:
91+
logging.info(f"Archive '{archive_name}' not found.")
92+
return
7893

94+
archive_id = result[0]
7995
cursor.execute("SELECT file_name FROM files WHERE archive_id = ?", (archive_id,))
8096
files = cursor.fetchall()
81-
# Loop through and print the files to be deleted
82-
if files:
83-
for file in files:
84-
file_path = pathlib.Path(get_library_path()).joinpath(file[0])
85-
if pathlib.Path(file_path).exists():
86-
pathlib.Path(file_path).unlink()
87-
88-
# Delete the archive (this will also delete associated files due to ON DELETE CASCADE)
89-
cursor.execute("DELETE FROM archives WHERE id = ?", (archive_id,))
90-
conn.commit()
9197

92-
logger.info(f"Archive '{archive_name}' and its files have been deleted.")
93-
else:
94-
logger.info(f"Archive '{archive_name}' not found.")
95-
conn.close()
98+
# Delete associated files from the filesystem
99+
for (file_name,) in files:
100+
file_path = Path(get_library_path()) / file_name
101+
try:
102+
if file_path.exists():
103+
file_path.unlink()
104+
logging.info(f"Deleted file: {file_path}")
105+
except Exception as e:
106+
logging.error(f"Error deleting file {file_path}: {e}")
107+
108+
# Delete the archive and its entries in the database
109+
cursor.execute("DELETE FROM archives WHERE id = ?", (archive_id,))
110+
logging.info(f"Archive '{archive_name}' and its files have been deleted.")
96111

97112

98113
def does_archive_exist(archive_name: str, file_list: list[str]) -> bool:

0 commit comments

Comments
 (0)