This commit is contained in:
billypom on debian 2025-04-24 19:20:38 -04:00
parent 491e134d94
commit 18d248386c
6 changed files with 127 additions and 64 deletions

View File

@ -4,15 +4,31 @@ from appdirs import user_config_dir
from dataclasses import dataclass, asdict from dataclasses import dataclass, asdict
from typing import Optional from typing import Optional
@dataclass @dataclass
class SQLiteMap(): class SQLiteMap:
title: str title: Optional[str] = None
artist: str artist: Optional[str] = None
album: Optional[str] = None album: Optional[str] = None
album_artist: Optional[str] = None
track_number: Optional[str] = None
genre: Optional[str] = None
length_seconds: Optional[str] = None
album_date: Optional[str] = None
codec: Optional[str] = None
filepath: Optional[str] = None
"""
db names are called FIELDS (e.g., title, track_number, length_seconds)
gui names are called HEADERS (e.g., title, track, length, year)
id3 names are called TAGS (e.g., TIT2, TPE1, TALB)
class HeaderTags(): is dataclasses rly worth it?
"""
class HeaderTags:
""" """
Utility class to converting between different "standards" for tags (headers, id3, etc) Utility class to converting between different "standards" for tags (headers, id3, etc)
@ -20,8 +36,8 @@ class HeaderTags():
`gui`: dict = "db name": "gui string" `gui`: dict = "db name": "gui string"
`id3`: dict = "db name": "id3 tag string" `id3`: dict = "db name": "id3 tag string"
`id3_keys`: dict = "id3 tag string": "db name" `id3_keys`: dict = "id3 tag string": "db name"
`editable_db_tags`: list = "list of db names that are user editable" `editable_fields`: list = "list of db names that are user editable"
`user_headers`: list = "list of db headers that the user has chosen to see in gui" `user_fields`: list = "list of db headers that the user has chosen to see in gui"
""" """
def __init__(self): def __init__(self):
@ -31,7 +47,43 @@ class HeaderTags():
) )
self.config = ConfigParser() self.config = ConfigParser()
self.config.read(cfg_file) self.config.read(cfg_file)
self.user_headers: list = str(self.config["table"]["columns"]).split(",") print("header tag config")
print(self.config)
self.user_fields: list = str(self.config["table"]["columns"]).split(",")
self.editable_fields: list = [
"title",
"artist",
"album_artist",
"album",
"track_number",
"genre",
"album_date",
]
self.fields = SQLiteMap()
self.headers = SQLiteMap(
title="title",
artist="artist",
album="album",
album_artist="alb artist",
track_number="track",
genre="genre",
codec="codec",
length_seconds="length",
album_date="year",
filepath="path",
)
# self.id3 = SQLiteMap(
# title = "TIT2",
# artist = "TPE1",
# album = "TALB",
# album_artist = "TPE2",
# track_number = "TRCK",
# genre = "TCON",
# length_seconds = "TLEN",
# album_date = "TDRC",
# codec = None,
# filepath = None
# )
self.db: dict = { self.db: dict = {
"title": "title", "title": "title",
"artist": "artist", "artist": "artist",
@ -74,20 +126,10 @@ class HeaderTags():
if v is not None: if v is not None:
self.id3_keys[v] = k self.id3_keys[v] = k
self.editable_db_tags: list = [
"title",
"artist",
"album_artist",
"album",
"track_number",
"genre",
"album_date",
]
def get_user_gui_headers(self) -> list: def get_user_gui_headers(self) -> list:
"""Returns a list of headers for the GUI""" """Returns a list of headers for the GUI"""
gui_headers = [] gui_headers = []
for db, gui in self.gui.items(): for db, gui in asdict(self.headers).items():
if db in self.user_headers: if db in self.user_fields:
gui_headers.append(gui) gui_headers.append(gui)
return gui_headers return gui_headers

View File

@ -56,9 +56,10 @@ class MetadataWindow(QDialog):
layout.addWidget(category_label) layout.addWidget(category_label)
layout.addWidget(h_separator) layout.addWidget(h_separator)
tag_sets: dict = {} tag_sets: dict[str, list] = {}
# Get a dict of all tags for all songs # Get a dict of all tags for all songs
# e.g., { "TIT2": ["song_title1", "song_title2"], ... } # e.g., { "TIT2": ["song_title1", "song_title2"], ... }
# e.g., { "title": ["song_title1", "song_title2"], ... }
for song in self.songs: for song in self.songs:
song_data = get_tags(song[0])[0] song_data = get_tags(song[0])[0]
if not song_data: if not song_data:
@ -71,7 +72,7 @@ class MetadataWindow(QDialog):
) )
return return
for key, tag in self.headers.id3.items(): for key, tag in self.headers.id3.items():
if key not in self.headers.editable_db_tags: if key not in self.headers.editable_fields:
continue continue
if tag is not None: if tag is not None:
try: try:

View File

@ -1,7 +1,5 @@
from mutagen.id3 import ID3 from mutagen.id3 import ID3
from json import load as jsonload
import DBA import DBA
from pprint import pprint
from PyQt5.QtGui import ( from PyQt5.QtGui import (
QColor, QColor,
QDragMoveEvent, QDragMoveEvent,
@ -18,12 +16,10 @@ from PyQt5.QtWidgets import (
QAction, QAction,
QHeaderView, QHeaderView,
QMenu, QMenu,
QPlainTextEdit,
QTableView, QTableView,
QShortcut, QShortcut,
QMessageBox, QMessageBox,
QAbstractItemView, QAbstractItemView,
QVBoxLayout,
) )
from PyQt5.QtCore import ( from PyQt5.QtCore import (
QItemSelectionModel, QItemSelectionModel,
@ -32,7 +28,6 @@ from PyQt5.QtCore import (
QModelIndex, QModelIndex,
QThreadPool, QThreadPool,
pyqtSignal, pyqtSignal,
QTimer,
) )
from components.DebugWindow import DebugWindow from components.DebugWindow import DebugWindow
from components.ErrorDialog import ErrorDialog from components.ErrorDialog import ErrorDialog
@ -104,6 +99,8 @@ class MusicTable(QTableView):
) )
self.config = ConfigParser() self.config = ConfigParser()
self.config.read(cfg_file) self.config.read(cfg_file)
print("music table config:")
print(self.config)
# Threads # Threads
self.threadpool = QThreadPool self.threadpool = QThreadPool
@ -127,8 +124,6 @@ class MusicTable(QTableView):
self.setSelectionMode(QAbstractItemView.ExtendedSelection) self.setSelectionMode(QAbstractItemView.ExtendedSelection)
self.setSelectionBehavior(QAbstractItemView.SelectRows) self.setSelectionBehavior(QAbstractItemView.SelectRows)
# header # header
# FIXME: table headers being resized and going out window bounds
# causing some recursion errors...
self.horizontal_header: QHeaderView = self.horizontalHeader() self.horizontal_header: QHeaderView = self.horizontalHeader()
assert self.horizontal_header is not None # i hate look at linting errors assert self.horizontal_header is not None # i hate look at linting errors
self.horizontal_header.setStretchLastSection(True) self.horizontal_header.setStretchLastSection(True)
@ -342,7 +337,7 @@ class MusicTable(QTableView):
def on_sort(self): def on_sort(self):
debug("on_sort") debug("on_sort")
search_col_num = self.headers.user_headers.index("filepath") search_col_num = self.headers.user_fields.index("filepath")
selected_qmodel_index = self.find_qmodel_index_by_value( selected_qmodel_index = self.find_qmodel_index_by_value(
self.proxymodel, search_col_num, self.selected_song_filepath self.proxymodel, search_col_num, self.selected_song_filepath
) )
@ -397,18 +392,14 @@ class MusicTable(QTableView):
if isinstance(self.model2, QStandardItemModel): if isinstance(self.model2, QStandardItemModel):
debug("on_cell_data_changed") debug("on_cell_data_changed")
# get the ID of the row that was edited # get the ID of the row that was edited
id_index = self.model2.index(topLeft.row(), 0) # ID is column 0, always id_index = self.model2.index(topLeft.row(), 0)
# get the db song_id from the row # get the db song_id from the row
song_id = self.model2.data(id_index, Qt.ItemDataRole.UserRole) song_id = self.model2.data(id_index, Qt.ItemDataRole.UserRole)
# get the filepath through a series of steps... user_index = self.headers.user_fields.index("filepath")
# NOTE: filepath is always the last column filepath = self.currentIndex().siblingAtColumn(user_index).data()
filepath_column_idx = self.model2.columnCount() - 1
filepath_index = self.model2.index(topLeft.row(), filepath_column_idx)
filepath = self.model2.data(filepath_index)
# update the ID3 information # update the ID3 information
user_input_data = topLeft.data() user_input_data = topLeft.data()
# edited_column_name = self.database_columns[topLeft.column()] edited_column_name = self.headers.user_fields[topLeft.column()]
edited_column_name = self.headers.user_headers[topLeft.column()]
debug(f"on_cell_data_changed | edited column name: {edited_column_name}") debug(f"on_cell_data_changed | edited column name: {edited_column_name}")
response = set_tag(filepath, edited_column_name, user_input_data) response = set_tag(filepath, edited_column_name, user_input_data)
if response: if response:
@ -677,7 +668,7 @@ class MusicTable(QTableView):
self.vertical_scroll_position = self.verticalScrollBar().value() # type: ignore self.vertical_scroll_position = self.verticalScrollBar().value() # type: ignore
self.model2.clear() self.model2.clear()
self.model2.setHorizontalHeaderLabels(self.headers.get_user_gui_headers()) self.model2.setHorizontalHeaderLabels(self.headers.get_user_gui_headers())
fields = ", ".join(self.headers.user_headers) fields = ", ".join(self.headers.user_fields)
if playlist_id: # Load a playlist if playlist_id: # Load a playlist
# Fetch playlist data # Fetch playlist data
selected_playlist_id = playlist_id[0] selected_playlist_id = playlist_id[0]
@ -730,13 +721,15 @@ class MusicTable(QTableView):
# reloading the model destroys and makes new indexes # reloading the model destroys and makes new indexes
# so we look for the new index of the current song on load # so we look for the new index of the current song on load
current_song_filepath = self.get_current_song_filepath() current_song_filepath = self.get_current_song_filepath()
print(f'load music table current filepath: {current_song_filepath}') print(f"load music table current filepath: {current_song_filepath}")
for row in range(self.model2.rowCount()): for row in range(self.model2.rowCount()):
real_index = self.model2.index(row, self.headers.user_headers.index("filepath")) real_index = self.model2.index(
row, self.headers.user_fields.index("filepath")
)
if real_index.data() == current_song_filepath: if real_index.data() == current_song_filepath:
print('is it true?') print("is it true?")
print(f'{real_index.data()} == {current_song_filepath}') print(f"{real_index.data()} == {current_song_filepath}")
print('load music table real index:') print("load music table real index:")
print(real_index) print(real_index)
self.current_song_qmodel_index = real_index self.current_song_qmodel_index = real_index
self.model2.layoutChanged.emit() # emits a signal that the view should be updated self.model2.layoutChanged.emit() # emits a signal that the view should be updated
@ -833,9 +826,7 @@ class MusicTable(QTableView):
selected_rows = self.get_selected_rows() selected_rows = self.get_selected_rows()
filepaths = [] filepaths = []
for row in selected_rows: for row in selected_rows:
idx = self.proxymodel.index( idx = self.proxymodel.index(row, self.headers.user_fields.index("filepath"))
row, self.headers.user_headers.index("filepath")
)
filepaths.append(idx.data()) filepaths.append(idx.data())
return filepaths return filepaths
@ -850,7 +841,9 @@ class MusicTable(QTableView):
return [] return []
selected_rows = set(index.row() for index in indexes) selected_rows = set(index.row() for index in indexes)
id_list = [ id_list = [
self.proxymodel.data(self.proxymodel.index(row, 0), Qt.ItemDataRole.UserRole) self.proxymodel.data(
self.proxymodel.index(row, 0), Qt.ItemDataRole.UserRole
)
for row in selected_rows for row in selected_rows
] ]
return id_list return id_list
@ -870,14 +863,18 @@ class MusicTable(QTableView):
def set_selected_song_filepath(self) -> None: def set_selected_song_filepath(self) -> None:
"""Sets the filepath of the currently selected song""" """Sets the filepath of the currently selected song"""
try: try:
table_index = self.headers.user_headers.index("filepath") user_index = self.headers.user_fields.index("filepath")
filepath = self.currentIndex().siblingAtColumn(table_index).data() filepath = self.currentIndex().siblingAtColumn(user_index).data()
except ValueError: except ValueError:
# if the user doesnt have filepath selected as a header, retrieve the file from db # if the user doesnt have filepath selected as a header, retrieve the file from db
row = self.currentIndex().row() row = self.currentIndex().row()
id = self.proxymodel.data(self.proxymodel.index(row, 0), Qt.ItemDataRole.UserRole) id = self.proxymodel.data(
self.proxymodel.index(row, 0), Qt.ItemDataRole.UserRole
)
with DBA.DBAccess() as db: with DBA.DBAccess() as db:
filepath = db.query('SELECT filepath FROM song WHERE id = ?', (id,))[0][0] filepath = db.query("SELECT filepath FROM song WHERE id = ?", (id,))[0][
0
]
self.selected_song_filepath = filepath self.selected_song_filepath = filepath
def set_current_song_filepath(self, filepath=None) -> None: def set_current_song_filepath(self, filepath=None) -> None:
@ -887,7 +884,9 @@ class MusicTable(QTableView):
""" """
# update the filepath # update the filepath
if not filepath: if not filepath:
path = self.current_song_qmodel_index.siblingAtColumn(self.headers.user_headers.index("filepath")).data() path = self.current_song_qmodel_index.siblingAtColumn(
self.headers.user_fields.index("filepath")
).data()
self.current_song_filepath: str = path self.current_song_filepath: str = path
else: else:
self.current_song_filepath = filepath self.current_song_filepath = filepath

38
main.py
View File

@ -41,7 +41,13 @@ from PyQt5.QtCore import (
QThreadPool, QThreadPool,
QRunnable, QRunnable,
) )
from PyQt5.QtMultimedia import QMediaPlayer, QMediaContent, QAudioProbe, QMediaPlaylist, QMultimedia from PyQt5.QtMultimedia import (
QMediaPlayer,
QMediaContent,
QAudioProbe,
QMediaPlaylist,
QMultimedia,
)
from PyQt5.QtGui import QClipboard, QCloseEvent, QFont, QPixmap, QResizeEvent from PyQt5.QtGui import QClipboard, QCloseEvent, QFont, QPixmap, QResizeEvent
from utils import ( from utils import (
delete_album_art, delete_album_art,
@ -50,7 +56,7 @@ from utils import (
initialize_db, initialize_db,
add_files_to_database, add_files_to_database,
set_album_art, set_album_art,
id3_remap id3_remap,
) )
from components import ( from components import (
HeaderTags, HeaderTags,
@ -155,6 +161,8 @@ class ApplicationWindow(QMainWindow, Ui_MainWindow):
/ "config.ini" / "config.ini"
) )
self.config.read(self.cfg_file) self.config.read(self.cfg_file)
print("main config:")
print(self.config)
self.threadpool: QThreadPool = QThreadPool() self.threadpool: QThreadPool = QThreadPool()
# UI # UI
self.setupUi(self) self.setupUi(self)
@ -328,7 +336,9 @@ class ApplicationWindow(QMainWindow, Ui_MainWindow):
file_url = media.canonicalUrl().toLocalFile() file_url = media.canonicalUrl().toLocalFile()
metadata = id3_remap(get_tags(file_url)[0]) metadata = id3_remap(get_tags(file_url)[0])
if metadata is not None: if metadata is not None:
self.set_ui_metadata(metadata["title"], metadata["artist"], metadata["album"], file_url) self.set_ui_metadata(
metadata["title"], metadata["artist"], metadata["album"], file_url
)
def on_volume_changed(self) -> None: def on_volume_changed(self) -> None:
"""Handles volume changes""" """Handles volume changes"""
@ -374,8 +384,12 @@ class ApplicationWindow(QMainWindow, Ui_MainWindow):
return return
row: int = index.row() row: int = index.row()
prev_row: int = row - 1 prev_row: int = row - 1
prev_index: QModelIndex = self.tableView.proxymodel.index(prev_row, index.column()) prev_index: QModelIndex = self.tableView.proxymodel.index(
prev_filepath = prev_index.siblingAtColumn(self.headers.user_headers.index("filepath")).data() prev_row, index.column()
)
prev_filepath = prev_index.siblingAtColumn(
self.headers.user_fields.index("filepath")
).data()
if prev_filepath is None: if prev_filepath is None:
return return
@ -394,8 +408,12 @@ class ApplicationWindow(QMainWindow, Ui_MainWindow):
return return
row: int = index.row() row: int = index.row()
next_row: int = row + 1 next_row: int = row + 1
next_index: QModelIndex = self.tableView.proxymodel.index(next_row, index.column()) next_index: QModelIndex = self.tableView.proxymodel.index(
next_filepath = next_index.siblingAtColumn(self.headers.user_headers.index("filepath")).data() next_row, index.column()
)
next_filepath = next_index.siblingAtColumn(
self.headers.user_fields.index("filepath")
).data()
if next_filepath is None: if next_filepath is None:
return return
self.play_audio_file(next_filepath) self.play_audio_file(next_filepath)
@ -471,7 +489,7 @@ class ApplicationWindow(QMainWindow, Ui_MainWindow):
filepath default value = `tableView.current_song_filepath` filepath default value = `tableView.current_song_filepath`
""" """
print('play audio file') print("play audio file")
if not filepath: if not filepath:
filepath = self.tableView.get_selected_song_filepath() filepath = self.tableView.get_selected_song_filepath()
metadata = id3_remap(get_tags(filepath)[0]) metadata = id3_remap(get_tags(filepath)[0])
@ -487,7 +505,9 @@ class ApplicationWindow(QMainWindow, Ui_MainWindow):
# assign "now playing" labels & album artwork # assign "now playing" labels & album artwork
if metadata is not None: if metadata is not None:
self.set_ui_metadata(metadata["title"], metadata["artist"], metadata["album"], filepath) self.set_ui_metadata(
metadata["title"], metadata["artist"], metadata["album"], filepath
)
def set_ui_metadata(self, title, artist, album, filepath): def set_ui_metadata(self, title, artist, album, filepath):
""" """

6
ui.ui
View File

@ -6,8 +6,8 @@
<rect> <rect>
<x>0</x> <x>0</x>
<y>0</y> <y>0</y>
<width>1152</width> <width>953</width>
<height>894</height> <height>739</height>
</rect> </rect>
</property> </property>
<property name="windowTitle"> <property name="windowTitle">
@ -310,7 +310,7 @@
<rect> <rect>
<x>0</x> <x>0</x>
<y>0</y> <y>0</y>
<width>1152</width> <width>953</width>
<height>24</height> <height>24</height>
</rect> </rect>
</property> </property>

View File

@ -107,6 +107,7 @@ def set_tag(filepath: str, tag_name: str, value: str):
# if tdat_tag: # if tdat_tag:
# # update TDAT if we have it # # update TDAT if we have it
# audio_file.add(tdat_tag) # audio_file.add(tdat_tag)
# Lyrics # Lyrics
if tag_name == "lyrics" or tag_name == "USLT": if tag_name == "lyrics" or tag_name == "USLT":
try: try: