XDG_CONFIG_HOME config file and put the database there i guess

This commit is contained in:
tsi-billypom 2025-03-24 11:29:31 -04:00
parent 199932d3f9
commit a41f909abe
8 changed files with 111 additions and 42 deletions

8
DBA.py
View File

@ -1,13 +1,19 @@
import sqlite3 import sqlite3
import logging import logging
from configparser import ConfigParser from configparser import ConfigParser
from pathlib import Path
from appdirs import user_config_dir
class DBAccess: class DBAccess:
def __init__(self, db_name=None): def __init__(self, db_name=None):
logging.info("Instantiating DBAccess") logging.info("Instantiating DBAccess")
config = ConfigParser() config = ConfigParser()
config.read("config.ini") cfg_file = (
Path(user_config_dir(appname="musicpom", appauthor="billypom"))
/ "config.ini"
)
config.read(cfg_file)
if db_name is None: if db_name is None:
db_name = config.get("db", "database") db_name = config.get("db", "database")
self._conn: sqlite3.Connection = sqlite3.connect(db_name) self._conn: sqlite3.Connection = sqlite3.connect(db_name)

View File

@ -1,4 +1,6 @@
import logging import logging
import DBA
import os
from PyQt5.QtWidgets import ( from PyQt5.QtWidgets import (
QCheckBox, QCheckBox,
QDialog, QDialog,
@ -11,11 +13,11 @@ from PyQt5.QtWidgets import (
QListWidgetItem, QListWidgetItem,
) )
from PyQt5.QtGui import QFont from PyQt5.QtGui import QFont
from configparser import ConfigParser
from utils import get_reorganize_vars from utils import get_reorganize_vars
from components import ErrorDialog from components import ErrorDialog
import DBA from configparser import ConfigParser
import os from pathlib import Path
from appdirs import user_config_dir
class ExportPlaylistWindow(QDialog): class ExportPlaylistWindow(QDialog):
@ -23,10 +25,16 @@ class ExportPlaylistWindow(QDialog):
super(ExportPlaylistWindow, self).__init__() super(ExportPlaylistWindow, self).__init__()
self.setWindowTitle("Export playlist") self.setWindowTitle("Export playlist")
self.setMinimumSize(600, 400) self.setMinimumSize(600, 400)
config = ConfigParser() self.cfg_file = (
config.read("config.ini") Path(user_config_dir(appname="musicpom", appauthor="billypom"))
self.relative_path: str = config.get("directories", "playlist_relative_path") / "config.ini"
self.export_path: str = config.get("directories", "playlist_export_path") )
self.config = ConfigParser()
self.config.read(self.cfg_file)
self.relative_path: str = self.config.get(
"directories", "playlist_relative_path"
)
self.export_path: str = self.config.get("directories", "playlist_export_path")
self.selected_playlist_name: str = "my-playlist.m3u" self.selected_playlist_name: str = "my-playlist.m3u"
self.current_m3u_path: str = self.export_path self.current_m3u_path: str = self.export_path
self.current_relative_path: str = self.relative_path self.current_relative_path: str = self.relative_path

View File

@ -44,10 +44,12 @@ from utils.get_album_art import get_album_art
from utils import set_id3_tag from utils import set_id3_tag
from subprocess import Popen from subprocess import Popen
from logging import debug, error from logging import debug, error
import configparser
import os import os
import shutil import shutil
import typing import typing
from pathlib import Path
from appdirs import user_config_dir
from configparser import ConfigParser
class MusicTable(QTableView): class MusicTable(QTableView):
@ -65,8 +67,14 @@ class MusicTable(QTableView):
self.setModel(self.model2) self.setModel(self.model2)
# Config # Config
self.config = configparser.ConfigParser() cfg_file = (
self.config.read("config.ini") Path(user_config_dir(appname="musicpom", appauthor="billypom"))
/ "config.ini"
)
self.config = ConfigParser()
self.config.read(cfg_file)
# Threads
self.threadpool = QThreadPool self.threadpool = QThreadPool
# gui names of headers # gui names of headers
self.table_headers = [ self.table_headers = [
@ -494,7 +502,7 @@ class MusicTable(QTableView):
Reorganizes files into Artist/Album/Song, Reorganizes files into Artist/Album/Song,
based on self.config['directories'][reorganize_destination'] based on self.config['directories'][reorganize_destination']
""" """
debug('reorganizing files') debug("reorganizing files")
# Get target directory # Get target directory
target_dir = str(self.config["directories"]["reorganize_destination"]) target_dir = str(self.config["directories"]["reorganize_destination"])
for filepath in filepaths: for filepath in filepaths:
@ -511,10 +519,10 @@ class MusicTable(QTableView):
if progress_callback: if progress_callback:
progress_callback.emit(f"Organizing: {filepath}") progress_callback.emit(f"Organizing: {filepath}")
# Create the directories if they dont exist # Create the directories if they dont exist
debug('make dirs') debug("make dirs")
os.makedirs(os.path.dirname(new_path), exist_ok=True) os.makedirs(os.path.dirname(new_path), exist_ok=True)
# Move the file to the new directory # Move the file to the new directory
debug(f'{filepath} > {new_path}') debug(f"{filepath} > {new_path}")
shutil.move(filepath, new_path) shutil.move(filepath, new_path)
# Update the db # Update the db
with DBA.DBAccess() as db: with DBA.DBAccess() as db:
@ -524,9 +532,7 @@ class MusicTable(QTableView):
) )
debug(f"reorganize_files() | Moved: {filepath} -> {new_path}") debug(f"reorganize_files() | Moved: {filepath} -> {new_path}")
except Exception as e: except Exception as e:
error( error(f"reorganize_files() | Error moving file: {filepath} | {e}")
f"reorganize_files() | Error moving file: {filepath} | {e}"
)
# Draw the rest of the owl # Draw the rest of the owl
# QMessageBox.information( # QMessageBox.information(
# self, "Reorganization complete", "Files successfully reorganized" # self, "Reorganization complete", "Files successfully reorganized"

View File

@ -10,16 +10,26 @@ from PyQt5.QtWidgets import (
QWidget, QWidget,
QDial, QDial,
) )
from logging import info
from PyQt5.QtGui import QFont from PyQt5.QtGui import QFont
from configparser import ConfigParser
from pathlib import Path
from appdirs import user_config_dir
class PreferencesWindow(QDialog): class PreferencesWindow(QDialog):
def __init__(self, config): def __init__(self, reloadConfigSignal):
super(PreferencesWindow, self).__init__() super(PreferencesWindow, self).__init__()
# Config # Config
self.reloadConfigSignal = reloadConfigSignal
self.setWindowTitle("Preferences") self.setWindowTitle("Preferences")
self.setMinimumSize(800, 800) self.setMinimumSize(800, 800)
self.config = config self.cfg_file = (
Path(user_config_dir(appname="musicpom", appauthor="billypom"))
/ "config.ini"
)
self.config = ConfigParser()
self.config.read(self.cfg_file)
self.current_category = "" self.current_category = ""
# Widgets # Widgets
@ -83,7 +93,7 @@ class PreferencesWindow(QDialog):
child.widget().deleteLater() child.widget().deleteLater()
def save_preferences(self): def save_preferences(self):
print('im saving') info("im saving")
# Upcate the config fields # Upcate the config fields
for key in self.input_fields: for key in self.input_fields:
for category in self.config.sections(): for category in self.config.sections():
@ -93,6 +103,7 @@ class PreferencesWindow(QDialog):
].text() ].text()
# Write the config file # Write the config file
with open("config.ini", "w") as configfile: with open(self.cfg_file, "w") as configfile:
self.config.write(configfile) self.config.write(configfile)
# self.close()
self.reloadConfigSignal.emit()

56
main.py
View File

@ -1,15 +1,17 @@
import os import os
import sys import sys
import logging import logging
from subprocess import run
import qdarktheme import qdarktheme
import typing import typing
import traceback
import DBA
from subprocess import run
from pyqtgraph import mkBrush from pyqtgraph import mkBrush
from mutagen.id3 import ID3 from mutagen.id3 import ID3
from mutagen.id3._frames import APIC from mutagen.id3._frames import APIC
from configparser import ConfigParser from configparser import ConfigParser
import traceback from pathlib import Path
import DBA from appdirs import user_config_dir
from logging import debug, error, warning, basicConfig, INFO, DEBUG from logging import debug, error, warning, basicConfig, INFO, DEBUG
from ui import Ui_MainWindow from ui import Ui_MainWindow
from PyQt5.QtWidgets import ( from PyQt5.QtWidgets import (
@ -132,6 +134,7 @@ class Worker(QRunnable):
class ApplicationWindow(QMainWindow, Ui_MainWindow): class ApplicationWindow(QMainWindow, Ui_MainWindow):
playlistCreatedSignal = pyqtSignal() playlistCreatedSignal = pyqtSignal()
reloadConfigSignal = pyqtSignal()
def __init__(self, clipboard): def __init__(self, clipboard):
super(ApplicationWindow, self).__init__() super(ApplicationWindow, self).__init__()
@ -141,7 +144,7 @@ class ApplicationWindow(QMainWindow, Ui_MainWindow):
self.threadpool = QThreadPool() self.threadpool = QThreadPool()
# UI # UI
self.setupUi(self) self.setupUi(self)
self.setWindowTitle("MusicPom") self.setWindowTitle("musicpom")
self.status_bar = QStatusBar() self.status_bar = QStatusBar()
self.permanent_status_label = QLabel("Status...") self.permanent_status_label = QLabel("Status...")
self.status_bar.addPermanentWidget(self.permanent_status_label) self.status_bar.addPermanentWidget(self.permanent_status_label)
@ -152,6 +155,11 @@ class ApplicationWindow(QMainWindow, Ui_MainWindow):
self.current_song_album_art: bytes | None = None self.current_song_album_art: bytes | None = None
self.album_art_scene: QGraphicsScene = QGraphicsScene() self.album_art_scene: QGraphicsScene = QGraphicsScene()
self.config: ConfigParser = ConfigParser() self.config: ConfigParser = ConfigParser()
self.cfg_file = (
Path(user_config_dir(appname="musicpom", appauthor="billypom"))
/ "config.ini"
)
self.config.read(self.cfg_file)
self.player: QMediaPlayer = QMediaPlayer() # Audio player object self.player: QMediaPlayer = QMediaPlayer() # Audio player object
self.probe: QAudioProbe = QAudioProbe() # Gets audio data self.probe: QAudioProbe = QAudioProbe() # Gets audio data
self.audio_visualizer: AudioVisualizer = AudioVisualizer(self.player) self.audio_visualizer: AudioVisualizer = AudioVisualizer(self.player)
@ -159,7 +167,6 @@ class ApplicationWindow(QMainWindow, Ui_MainWindow):
self.clipboard = clipboard self.clipboard = clipboard
self.tableView.load_qapp(self) self.tableView.load_qapp(self)
self.albumGraphicsView.load_qapp(self) self.albumGraphicsView.load_qapp(self)
self.config.read("config.ini")
# Initialization # Initialization
self.timer = QTimer(self) # Audio timing things self.timer = QTimer(self) # Audio timing things
self.player.setVolume(self.current_volume) self.player.setVolume(self.current_volume)
@ -242,9 +249,13 @@ class ApplicationWindow(QMainWindow, Ui_MainWindow):
self.delete_album_art_for_current_song self.delete_album_art_for_current_song
) )
def reload_config(self) -> None: def load_config(self) -> None:
"""does what it says""" """does what it says"""
self.config.read("config.ini") cfg_loc = (
Path(user_config_dir(appname="musicpom", appauthor="billypom"))
/ "config.ini"
)
self.config.read(cfg_loc)
def get_thread_pool(self) -> QThreadPool: def get_thread_pool(self) -> QThreadPool:
"""Returns the threadpool instance""" """Returns the threadpool instance"""
@ -265,7 +276,7 @@ class ApplicationWindow(QMainWindow, Ui_MainWindow):
self.config["table"]["column_widths"] = column_widths_as_string self.config["table"]["column_widths"] = column_widths_as_string
# Save the config # Save the config
with open("config.ini", "w") as configfile: with open(self.cfg_file, "w") as configfile:
self.config.write(configfile) self.config.write(configfile)
if a0 is not None: if a0 is not None:
super().closeEvent(a0) super().closeEvent(a0)
@ -506,10 +517,9 @@ class ApplicationWindow(QMainWindow, Ui_MainWindow):
def open_preferences(self) -> None: def open_preferences(self) -> None:
"""Opens the preferences window""" """Opens the preferences window"""
preferences_window = PreferencesWindow(self.config) preferences_window = PreferencesWindow(self.reloadConfigSignal)
preferences_window.reloadConfigSignal.connect(self.load_config)
preferences_window.exec_() # Display the preferences window modally preferences_window.exec_() # Display the preferences window modally
self.reload_config()
self.tableView.load_music_table()
# Quick Actions # Quick Actions
@ -568,15 +578,33 @@ class ApplicationWindow(QMainWindow, Ui_MainWindow):
if __name__ == "__main__": if __name__ == "__main__":
# Initialization # Initialization
if not os.path.exists("config.ini"):
cfg_file = (
Path(user_config_dir(appname="musicpom", appauthor="billypom")) / "config.ini"
)
cfg_path = str(Path(user_config_dir(appname="musicpom", appauthor="billypom")))
# If the config file doesn't exist, create it from the sample config
if not os.path.exists(cfg_file):
# Create config file from sample # Create config file from sample
run(["cp", "sample_config.ini", "config.ini"]) run(["cp", "sample_config.ini", cfg_file])
config = ConfigParser() config = ConfigParser()
config.read("config.ini") config.read(cfg_file)
db_filepath: str = config.get("db", "database")
# If the database location isnt set at the config location, move it
if not db_filepath.startswith(cfg_path):
config["db"]["database"] = f"{cfg_path}/{db_filepath}"
# Save the config
with open(cfg_file, "w") as configfile:
config.write(configfile)
config.read(cfg_file)
db_filepath: str = config.get("db", "database") db_filepath: str = config.get("db", "database")
db_path = db_filepath.split("/") db_path = db_filepath.split("/")
db_path.pop() db_path.pop()
db_path = "/".join(db_path) db_path = "/".join(db_path)
# If the db file doesn't exist # If the db file doesn't exist
if not os.path.exists(db_filepath): if not os.path.exists(db_filepath):
# If the db directory doesn't exist # If the db directory doesn't exist

View File

@ -1,5 +1,6 @@
mutagen mutagen
matplotlib matplotlib
appdirs
pyqt5 pyqt5
pydub pydub
pyqtdarktheme-fork pyqtdarktheme-fork

View File

@ -1,11 +1,16 @@
import DBA import DBA
from configparser import ConfigParser
from utils import get_id3_tags, id3_timestamp_to_datetime
import logging import logging
from utils import get_id3_tags, id3_timestamp_to_datetime
from PyQt5.QtCore import pyqtSignal from PyQt5.QtCore import pyqtSignal
from configparser import ConfigParser
from pathlib import Path
from appdirs import user_config_dir
config = ConfigParser() config = ConfigParser()
config.read("config.ini") cfg_file = (
Path(user_config_dir(appname="musicpom", appauthor="billypom")) / "config.ini"
)
config.read(cfg_file)
def add_files_to_library(files, progress_callback=None): def add_files_to_library(files, progress_callback=None):

View File

@ -1,11 +1,15 @@
import os import os
from configparser import ConfigParser
from PyQt5.QtCore import pyqtSignal from PyQt5.QtCore import pyqtSignal
from utils.add_files_to_library import add_files_to_library from utils.add_files_to_library import add_files_to_library
from configparser import ConfigParser
from pathlib import Path
from appdirs import user_config_dir
config = ConfigParser() config = ConfigParser()
config.read("config.ini") cfg_file = (
Path(user_config_dir(appname="musicpom", appauthor="billypom")) / "config.ini"
)
config.read(cfg_file)
def scan_for_music(): def scan_for_music():