SQLA2.0 rewrote logging

This commit is contained in:
Keith Edmunds 2022-07-03 15:17:25 +01:00
parent ff2f0d576c
commit bef4507ef6
8 changed files with 142 additions and 193 deletions

View File

@ -1,5 +1,6 @@
import logging import logging
import os import os
from typing import List, Optional
class Config(object): class Config(object):
@ -31,6 +32,8 @@ class Config(object):
COLUMN_NAME_TITLE = "Title" COLUMN_NAME_TITLE = "Title"
DBFS_FADE = -12 DBFS_FADE = -12
DBFS_SILENCE = -50 DBFS_SILENCE = -50
DEBUG_FUNCTIONS: List[Optional[str]] = []
DEBUG_MODULES: List[Optional[str]] = ['dbconfig']
DEFAULT_COLUMN_WIDTH = 200 DEFAULT_COLUMN_WIDTH = 200
DEFAULT_IMPORT_DIRECTORY = "/home/kae/Nextcloud/tmp" DEFAULT_IMPORT_DIRECTORY = "/home/kae/Nextcloud/tmp"
DEFAULT_OUTPUT_DIRECTORY = "/home/kae/music/Singles" DEFAULT_OUTPUT_DIRECTORY = "/home/kae/music/Singles"
@ -40,7 +43,7 @@ class Config(object):
FADE_TIME = 3000 FADE_TIME = 3000
INFO_TAB_TITLE_LENGTH = 15 INFO_TAB_TITLE_LENGTH = 15
INFO_TAB_URL = "https://www.wikipedia.org/w/index.php?search=%s" INFO_TAB_URL = "https://www.wikipedia.org/w/index.php?search=%s"
LOG_LEVEL_STDERR = logging.INFO LOG_LEVEL_STDERR = logging.DEBUG
LOG_LEVEL_SYSLOG = logging.DEBUG LOG_LEVEL_SYSLOG = logging.DEBUG
LOG_NAME = "musicmuster" LOG_NAME = "musicmuster"
MAIL_PASSWORD = os.environ.get('MAIL_PASSWORD') MAIL_PASSWORD = os.environ.get('MAIL_PASSWORD')

View File

@ -1,58 +1,32 @@
# import inspect import logging
import os import os
#
from config import Config from config import Config
from sqlalchemy import create_engine from sqlalchemy import create_engine
# from contextlib import contextmanager from contextlib import contextmanager
# from log import DEBUG from sqlalchemy.orm import (sessionmaker, scoped_session)
# from sqlalchemy.orm import (sessionmaker, scoped_session)
# from log import log
#
# class Counter:
# def __init__(self):
# self.count = 0
#
# def __repr__(self):
# return(f"<Counter({self.count=})>")
#
# def inc(self):
# self.count += 1
# return self.count
#
# def dec(self):
# self.count -= 1
# return self.count
#
#
MYSQL_CONNECT = os.environ.get('MM_DB') MYSQL_CONNECT = os.environ.get('MM_DB')
if MYSQL_CONNECT is None: if MYSQL_CONNECT is None:
raise ValueError("MYSQL_CONNECT is undefined") raise ValueError("MYSQL_CONNECT is undefined")
else:
dbname = MYSQL_CONNECT.split('/')[-1]
log.debug(f"Database: {dbname}")
# MM_ENV = os.environ.get('MM_ENV', 'PRODUCTION') # MM_ENV = os.environ.get('MM_ENV', 'PRODUCTION')
# testing = False # testing = False
# # if MM_ENV == 'TESTING':
# if MM_ENV == 'PRODUCTION':
# dbname = os.environ.get('MM_PRODUCTION_DBNAME', 'musicmuster_prod')
# dbuser = os.environ.get('MM_PRODUCTION_DBUSER', 'musicmuster')
# dbpw = os.environ.get('MM_PRODUCTION_DBPW', 'musicmuster')
# dbhost = os.environ.get('MM_PRODUCTION_DBHOST', 'localhost')
# elif MM_ENV == 'TESTING':
# dbname = os.environ.get('MM_TESTING_DBNAME', 'musicmuster_testing') # dbname = os.environ.get('MM_TESTING_DBNAME', 'musicmuster_testing')
# dbuser = os.environ.get('MM_TESTING_DBUSER', 'musicmuster_testing') # dbuser = os.environ.get('MM_TESTING_DBUSER', 'musicmuster_testing')
# dbpw = os.environ.get('MM_TESTING_DBPW', 'musicmuster_testing') # dbpw = os.environ.get('MM_TESTING_DBPW', 'musicmuster_testing')
# dbhost = os.environ.get('MM_TESTING_DBHOST', 'localhost') # dbhost = os.environ.get('MM_TESTING_DBHOST', 'localhost')
# testing = True # testing = True
# elif MM_ENV == 'DEVELOPMENT':
# dbname = os.environ.get('MM_DEVELOPMENT_DBNAME', 'musicmuster_dev')
# dbuser = os.environ.get('MM_DEVELOPMENT_DBUSER', 'musicmuster')
# dbpw = os.environ.get('MM_DEVELOPMENT_DBPW', 'musicmuster')
# dbhost = os.environ.get('MM_DEVELOPMENT_DBHOST', 'localhost')
# else: # else:
# raise ValueError(f"Unknown MusicMuster environment: {MM_ENV=}") # raise ValueError(f"Unknown MusicMuster environment: {MM_ENV=}")
# #
# DEBUG(f"Using {dbname} database")
# MYSQL_CONNECT = f"mysql+mysqldb://{dbuser}:{dbpw}@{dbhost}/{dbname}" # MYSQL_CONNECT = f"mysql+mysqldb://{dbuser}:{dbpw}@{dbhost}/{dbname}"
#
engine = create_engine( engine = create_engine(
MYSQL_CONNECT, MYSQL_CONNECT,
encoding='utf-8', encoding='utf-8',
@ -60,17 +34,17 @@ engine = create_engine(
pool_pre_ping=True, pool_pre_ping=True,
future=True future=True
) )
#
# #
# # @contextmanager @contextmanager
# # def Session(): def Session():
# # frame = inspect.stack()[2] frame = inspect.stack()[2]
# # file = frame.filename file = frame.filename
# # function = frame.function function = frame.function
# # lineno = frame.lineno lineno = frame.lineno
# # Session = scoped_session(sessionmaker(bind=engine, future=True)) Session = scoped_session(sessionmaker(bind=engine, future=True))
# # DEBUG(f"Session acquired, {file=}, {function=}, {lineno=}, {Session=}") log.debug(f"Session acquired, {file=}, {function=}, {lineno=}, {Session=}")
# # yield Session yield Session
# # DEBUG(" Session released") log.debug(" Session released")
# # Session.commit() Session.commit()
# # Session.close() Session.close()

View File

@ -20,6 +20,19 @@ class LevelTagFilter(logging.Filter):
return True return True
class DebugStdoutFilter(logging.Filter):
"""Filter debug messages sent to stdout"""
def filter(self, record):
if record.levelno != logging.DEBUG:
return True
if record.module in Config.DEBUG_MODULES:
return True
if record.funcName in Config.DEBUG_FUNCTIONS:
return True
return False
log = logging.getLogger(Config.LOG_NAME) log = logging.getLogger(Config.LOG_NAME)
log.setLevel(logging.DEBUG) log.setLevel(logging.DEBUG)
@ -33,13 +46,19 @@ syslog.setLevel(Config.LOG_LEVEL_SYSLOG)
# Filter # Filter
local_filter = LevelTagFilter() local_filter = LevelTagFilter()
debug_filter = DebugStdoutFilter()
syslog.addFilter(local_filter) syslog.addFilter(local_filter)
stderr.addFilter(local_filter) stderr.addFilter(local_filter)
stderr.addFilter(debug_filter)
# create formatter and add it to the handlers # create formatter and add it to the handlers
stderr_fmt = logging.Formatter('[%(asctime)s] %(leveltag)s: %(message)s', stderr_fmt = logging.Formatter('[%(asctime)s] %(leveltag)s: %(message)s',
datefmt='%H:%M:%S') datefmt='%H:%M:%S')
syslog_fmt = logging.Formatter('[%(name)s] %(leveltag)s: %(message)s') syslog_fmt = logging.Formatter(
'[%(name)s] %(module)s.%(funcName)s - %(leveltag)s: %(message)s'
)
stderr.setFormatter(stderr_fmt) stderr.setFormatter(stderr_fmt)
syslog.setFormatter(syslog_fmt) syslog.setFormatter(syslog_fmt)
@ -57,52 +76,3 @@ def log_uncaught_exceptions(ex_cls, ex, tb):
sys.excepthook = log_uncaught_exceptions sys.excepthook = log_uncaught_exceptions
def DEBUG(msg: str, force_stderr: bool = False) -> None:
"""
Outupt a log message at level DEBUG. If force_stderr is True,
output this message to stderr regardless of default stderr level
setting.
"""
if force_stderr:
old_level = stderr.level
stderr.setLevel(logging.DEBUG)
log.debug(msg)
stderr.setLevel(old_level)
else:
log.debug(msg)
def EXCEPTION(msg: str) -> None:
log.exception(msg, exc_info=True, stack_info=True)
def ERROR(msg: str) -> None:
log.error(msg)
def INFO(msg: str) -> None:
log.info(msg)
if __name__ == "__main__":
DEBUG("hi debug")
ERROR("hi error")
INFO("hi info")
EXCEPTION("hi exception")
def f():
return g()
def g():
return h()
def h():
return i()
def i():
return 1 / 0
f()

View File

@ -39,7 +39,7 @@ from sqlalchemy.orm import (
# leading_silence, # leading_silence,
# trailing_silence, # trailing_silence,
# ) # )
# from log import DEBUG, ERROR # from log import log.debug, log.error
# #
Base = declarative_base() Base = declarative_base()
# #
@ -134,7 +134,7 @@ Base = declarative_base()
# row: int, text: str) -> None: # row: int, text: str) -> None:
# """Create note""" # """Create note"""
# #
# DEBUG(f"Notes.__init__({playlist_id=}, {row=}, {text=})") # log.debug(f"Notes.__init__({playlist_id=}, {row=}, {text=})")
# self.playlist_id = playlist_id # self.playlist_id = playlist_id
# self.row = row # self.row = row
# self.note = text # self.note = text
@ -149,7 +149,7 @@ Base = declarative_base()
# def delete_note(self, session: Session) -> None: # def delete_note(self, session: Session) -> None:
# """Delete note""" # """Delete note"""
# #
# DEBUG(f"delete_note({self.id=}") # log.debug(f"delete_note({self.id=}")
# #
# session.query(Notes).filter_by(id=self.id).delete() # session.query(Notes).filter_by(id=self.id).delete()
# session.flush() # session.flush()
@ -183,11 +183,11 @@ Base = declarative_base()
# """Return note or None""" # """Return note or None"""
# #
# try: # try:
# DEBUG(f"Notes.get_track(track_id={note_id})") # log.debug(f"Notes.get_track(track_id={note_id})")
# note = session.query(cls).filter(cls.id == note_id).one() # note = session.query(cls).filter(cls.id == note_id).one()
# return note # return note
# except NoResultFound: # except NoResultFound:
# ERROR(f"get_track({note_id}): not found") # log.error(f"get_track({note_id}): not found")
# return None # return None
# #
# def update( # def update(
@ -197,7 +197,7 @@ Base = declarative_base()
# Update note details. If text=None, don't change text. # Update note details. If text=None, don't change text.
# """ # """
# #
# DEBUG(f"Notes.update_note({self.id=}, {row=}, {text=})") # log.debug(f"Notes.update_note({self.id=}, {row=}, {text=})")
# #
# self.row = row # self.row = row
# if text: # if text:
@ -217,7 +217,7 @@ Base = declarative_base()
# def __init__(self, session: Session, track_id: int) -> None: # def __init__(self, session: Session, track_id: int) -> None:
# """Record that track was played""" # """Record that track was played"""
# #
# DEBUG(f"add_playdate({track_id=})") # log.debug(f"add_playdate({track_id=})")
# #
# self.lastplayed = datetime.now() # self.lastplayed = datetime.now()
# self.track_id = track_id # self.track_id = track_id
@ -370,7 +370,7 @@ Base = declarative_base()
# session.flush() # session.flush()
# #
# def remove_track(self, session: Session, row: int) -> None: # def remove_track(self, session: Session, row: int) -> None:
# DEBUG(f"Playlist.remove_track({self.id=}, {row=})") # log.debug(f"Playlist.remove_track({self.id=}, {row=})")
# #
# # Refresh self first (this is necessary when calling remove_track # # Refresh self first (this is necessary when calling remove_track
# # multiple times before session.commit()) # # multiple times before session.commit())
@ -409,7 +409,7 @@ Base = declarative_base()
# def __init__( # def __init__(
# self, session: Session, playlist_id: int, track_id: int, # self, session: Session, playlist_id: int, track_id: int,
# row: int) -> None: # row: int) -> None:
# DEBUG(f"PlaylistTracks.__init__({playlist_id=}, {track_id=}, {row=})") # log.debug(f"PlaylistTracks.__init__({playlist_id=}, {track_id=}, {row=})")
# #
# self.playlist_id = playlist_id # self.playlist_id = playlist_id
# self.track_id = track_id # self.track_id = track_id
@ -549,7 +549,7 @@ Base = declarative_base()
# else created new track and return it # else created new track and return it
# """ # """
# #
# DEBUG(f"Tracks.get_or_create({path=})") # log.debug(f"Tracks.get_or_create({path=})")
# #
# try: # try:
# track = session.query(cls).filter(cls.path == path).one() # track = session.query(cls).filter(cls.path == path).one()
@ -567,7 +567,7 @@ Base = declarative_base()
# than one track matches. # than one track matches.
# """ # """
# #
# DEBUG(f"Tracks.get_track_from_filename({filename=})") # log.debug(f"Tracks.get_track_from_filename({filename=})")
# try: # try:
# track = session.query(Tracks).filter(Tracks.path.ilike( # track = session.query(Tracks).filter(Tracks.path.ilike(
# f'%{os.path.sep}{filename}')).one() # f'%{os.path.sep}{filename}')).one()
@ -581,7 +581,7 @@ Base = declarative_base()
# Return track with passee path, or None. # Return track with passee path, or None.
# """ # """
# #
# DEBUG(f"Tracks.get_track_from_path({path=})") # log.debug(f"Tracks.get_track_from_path({path=})")
# #
# return session.query(Tracks).filter(Tracks.path == path).first() # return session.query(Tracks).filter(Tracks.path == path).first()
# #
@ -590,11 +590,11 @@ Base = declarative_base()
# """Return track or None""" # """Return track or None"""
# #
# try: # try:
# DEBUG(f"Tracks.get_track(track_id={track_id})") # log.debug(f"Tracks.get_track(track_id={track_id})")
# track = session.query(Tracks).filter(Tracks.id == track_id).one() # track = session.query(Tracks).filter(Tracks.id == track_id).one()
# return track # return track
# except NoResultFound: # except NoResultFound:
# ERROR(f"get_track({track_id}): not found") # log.error(f"get_track({track_id}): not found")
# return None # return None
# #
# def rescan(self, session: Session) -> None: # def rescan(self, session: Session) -> None:
@ -617,13 +617,13 @@ Base = declarative_base()
# def remove_by_path(session: Session, path: str) -> None: # def remove_by_path(session: Session, path: str) -> None:
# """Remove track with passed path from database""" # """Remove track with passed path from database"""
# #
# DEBUG(f"Tracks.remove_path({path=})") # log.debug(f"Tracks.remove_path({path=})")
# #
# try: # try:
# session.query(Tracks).filter(Tracks.path == path).delete() # session.query(Tracks).filter(Tracks.path == path).delete()
# session.flush() # session.flush()
# except IntegrityError as exception: # except IntegrityError as exception:
# ERROR(f"Can't remove track with {path=} ({exception=})") # log.error(f"Can't remove track with {path=} ({exception=})")
# #
# @classmethod # @classmethod
# def search_artists(cls, session: Session, text: str) -> List["Tracks"]: # def search_artists(cls, session: Session, text: str) -> List["Tracks"]:

View File

@ -6,7 +6,7 @@
# from datetime import datetime # from datetime import datetime
# from time import sleep # from time import sleep
# #
# from log import DEBUG, ERROR # from log import log.debug, log.error
# #
# lock = threading.Lock() # lock = threading.Lock()
# #
@ -32,7 +32,7 @@
# to hold up the UI during the fade. # to hold up the UI during the fade.
# """ # """
# #
# DEBUG("music.fade()", True) # log.debug("music.fade()", True)
# #
# if not self.player: # if not self.player:
# return # return
@ -53,13 +53,13 @@
# # Take a copy of current player to allow another track to be # # Take a copy of current player to allow another track to be
# # started without interfering here # # started without interfering here
# #
# DEBUG(f"music._fade(), {self.player=}", True) # log.debug(f"music._fade(), {self.player=}", True)
# #
# with lock: # with lock:
# p = self.player # p = self.player
# self.player = None # self.player = None
# #
# DEBUG("music._fade() post-lock", True) # log.debug("music._fade() post-lock", True)
# #
# fade_time = Config.FADE_TIME / 1000 # fade_time = Config.FADE_TIME / 1000
# steps = Config.FADE_STEPS # steps = Config.FADE_STEPS
@ -81,10 +81,10 @@
# sleep(sleep_time) # sleep(sleep_time)
# #
# with lock: # with lock:
# DEBUG(f"music._fade(), stopping {p=}", True) # log.debug(f"music._fade(), stopping {p=}", True)
# #
# p.stop() # p.stop()
# DEBUG(f"Releasing player {p=}", True) # log.debug(f"Releasing player {p=}", True)
# p.release() # p.release()
# #
# self.fading -= 1 # self.fading -= 1
@ -102,7 +102,7 @@
# """Return current position""" # """Return current position"""
# #
# with lock: # with lock:
# DEBUG("music.get_position", True) # log.debug("music.get_position", True)
# #
# print(f"get_position, {self.player=}") # print(f"get_position, {self.player=}")
# if not self.player: # if not self.player:
@ -116,17 +116,17 @@
# Log and return if path not found. # Log and return if path not found.
# """ # """
# #
# DEBUG(f"music.play({path=})", True) # log.debug(f"music.play({path=})", True)
# #
# if not os.access(path, os.R_OK): # if not os.access(path, os.R_OK):
# ERROR(f"play({path}): path not found") # log.error(f"play({path}): path not found")
# return # return
# #
# self.track_path = path # self.track_path = path
# #
# self.player = self.VLC.media_player_new(path) # self.player = self.VLC.media_player_new(path)
# self.player.audio_set_volume(self.max_volume) # self.player.audio_set_volume(self.max_volume)
# DEBUG(f"music.play({path=}), {self.player}", True) # log.debug(f"music.play({path=}), {self.player}", True)
# self.player.play() # self.player.play()
# self.current_track_start_time = datetime.now() # self.current_track_start_time = datetime.now()
# #
@ -166,7 +166,7 @@
# def stop(self): # def stop(self):
# """Immediately stop playing""" # """Immediately stop playing"""
# #
# DEBUG(f"music.stop(), {self.player=}", True) # log.debug(f"music.stop(), {self.player=}", True)
# #
# with lock: # with lock:
# if not self.player: # if not self.player:
@ -174,7 +174,7 @@
# #
# position = self.player.get_position() # position = self.player.get_position()
# self.player.stop() # self.player.stop()
# DEBUG(f"music.stop(): Releasing player {self.player=}", True) # log.debug(f"music.stop(): Releasing player {self.player=}", True)
# self.player.release() # self.player.release()
# # Ensure we don't reference player after release # # Ensure we don't reference player after release
# self.player = None # self.player = None

View File

@ -1,5 +1,6 @@
#!/usr/bin/env python #!/usr/bin/env python
from log import log
# import argparse # import argparse
# import psutil # import psutil
import sys import sys
@ -9,8 +10,6 @@ import sys
# #
# #
# from datetime import datetime, timedelta # from datetime import datetime, timedelta
from log import EXCEPTION
# from log import DEBUG, EXCEPTION
# from typing import Callable, Dict, List, Optional, Tuple # from typing import Callable, Dict, List, Optional, Tuple
# #
# from PyQt5.QtCore import QDate, QEvent, QProcess, Qt, QTime, QTimer, QUrl # from PyQt5.QtCore import QDate, QEvent, QProcess, Qt, QTime, QTimer, QUrl
@ -46,10 +45,12 @@ from models import (
# from ui.dlg_search_database_ui import Ui_Dialog # from ui.dlg_search_database_ui import Ui_Dialog
# from ui.dlg_SelectPlaylist_ui import Ui_dlgSelectPlaylist # from ui.dlg_SelectPlaylist_ui import Ui_dlgSelectPlaylist
# from ui.downloadcsv_ui import Ui_DateSelect # from ui.downloadcsv_ui import Ui_DateSelect
from config import Config
from ui.main_window_ui import Ui_MainWindow from ui.main_window_ui import Ui_MainWindow
# from utilities import create_track_from_file, update_db # from utilities import create_track_from_file, update_db
# #
# #
# log = logging.getLogger(Config.LOG_NAME)
# class TrackData: # class TrackData:
# def __init__(self, track): # def __init__(self, track):
# self.id = track.id # self.id = track.id
@ -94,7 +95,7 @@ class Window(QMainWindow, Ui_MainWindow):
# self.visible_playlist_tab: Callable[[], PlaylistTab] = \ # self.visible_playlist_tab: Callable[[], PlaylistTab] = \
# self.tabPlaylist.currentWidget # self.tabPlaylist.currentWidget
# #
# self.load_last_playlists() #self.load_last_playlists()
# self.enable_play_next_controls() # self.enable_play_next_controls()
# self.check_audacity() # self.check_audacity()
# self.timer.start(Config.TIMER_MS) # self.timer.start(Config.TIMER_MS)
@ -144,13 +145,13 @@ class Window(QMainWindow, Ui_MainWindow):
# """Don't allow window to close when a track is playing""" # """Don't allow window to close when a track is playing"""
# #
# if self.music.playing(): # if self.music.playing():
# DEBUG("closeEvent() ignored as music is playing") # log.debug("closeEvent() ignored as music is playing")
# event.ignore() # event.ignore()
# helpers.show_warning( # helpers.show_warning(
# "Track playing", # "Track playing",
# "Can't close application while track is playing") # "Can't close application while track is playing")
# else: # else:
# DEBUG("closeEvent() accepted") # log.debug("closeEvent() accepted")
# #
# with Session() as session: # with Session() as session:
# record = Settings.get_int_settings( # record = Settings.get_int_settings(
@ -287,7 +288,7 @@ class Window(QMainWindow, Ui_MainWindow):
# Disable "play next" keyboard controls # Disable "play next" keyboard controls
# """ # """
# #
# DEBUG("disable_play_next_controls()") # log.debug("disable_play_next_controls()")
# self.actionPlay_next.setEnabled(False) # self.actionPlay_next.setEnabled(False)
# self.statusbar.showMessage("Play controls: Disabled", 0) # self.statusbar.showMessage("Play controls: Disabled", 0)
# #
@ -330,7 +331,7 @@ class Window(QMainWindow, Ui_MainWindow):
# Enable "play next" keyboard controls # Enable "play next" keyboard controls
# """ # """
# #
# DEBUG("enable_play_next_controls()") # log.debug("enable_play_next_controls()")
# self.actionPlay_next.setEnabled(True) # self.actionPlay_next.setEnabled(True)
# self.statusbar.showMessage("Play controls: Enabled", 0) # self.statusbar.showMessage("Play controls: Enabled", 0)
# #
@ -420,7 +421,7 @@ class Window(QMainWindow, Ui_MainWindow):
# def fade(self) -> None: # def fade(self) -> None:
# """Fade currently playing track""" # """Fade currently playing track"""
# #
# DEBUG("musicmuster:fade()", True) # log.debug("musicmuster:fade()", True)
# #
# self.stop_playing(fade=True) # self.stop_playing(fade=True)
# #
@ -495,7 +496,7 @@ class Window(QMainWindow, Ui_MainWindow):
# self.visible_playlist_tab().insert_track(session, track) # self.visible_playlist_tab().insert_track(session, track)
# #
# def load_last_playlists(self): # def load_last_playlists(self):
# """Load the playlists that we loaded at end of last session""" # """Load the playlists that were loaded at end of last session"""
# #
# with Session() as session: # with Session() as session:
# for playlist in Playlists.get_open(session): # for playlist in Playlists.get_open(session):
@ -564,14 +565,14 @@ class Window(QMainWindow, Ui_MainWindow):
# if title in self.info_tabs.keys(): # if title in self.info_tabs.keys():
# # We already have a tab for this track # # We already have a tab for this track
# continue # continue
# if len(self.info_tabs) >= Config.MAX_INFO_TABS: # if len(self.info_tabs) >= Config.MAX_log.info_TABS:
# # Find an unneeded info tab # # Find an unneeded info tab
# try: # try:
# old_title = list( # old_title = list(
# set(self.info_tabs.keys()) - set(title_list) # set(self.info_tabs.keys()) - set(title_list)
# )[0] # )[0]
# except IndexError: # except IndexError:
# DEBUG( # log.debug(
# f"ensure_info_tabs({title_list}): unable to add " # f"ensure_info_tabs({title_list}): unable to add "
# f"{title=}" # f"{title=}"
# ) # )
@ -580,16 +581,16 @@ class Window(QMainWindow, Ui_MainWindow):
# widget = self.info_tabs[title] = self.info_tabs[old_title] # widget = self.info_tabs[title] = self.info_tabs[old_title]
# idx = self.tabPlaylist.indexOf(widget) # idx = self.tabPlaylist.indexOf(widget)
# self.tabPlaylist.setTabText( # self.tabPlaylist.setTabText(
# idx, title[:Config.INFO_TAB_TITLE_LENGTH]) # idx, title[:Config.log.info_TAB_TITLE_LENGTH])
# del self.info_tabs[old_title] # del self.info_tabs[old_title]
# else: # else:
# # Create a new tab for this title # # Create a new tab for this title
# widget = self.info_tabs[title] = QWebView() # widget = self.info_tabs[title] = QWebView()
# widget.setZoomFactor(Config.WEB_ZOOM_FACTOR) # widget.setZoomFactor(Config.WEB_ZOOM_FACTOR)
# self.tabPlaylist.addTab( # self.tabPlaylist.addTab(
# widget, title[:Config.INFO_TAB_TITLE_LENGTH]) # widget, title[:Config.log.info_TAB_TITLE_LENGTH])
# txt = urllib.parse.quote_plus(title) # txt = urllib.parse.quote_plus(title)
# url = Config.INFO_TAB_URL % txt # url = Config.log.info_TAB_URL % txt
# widget.setUrl(QUrl(url)) # widget.setUrl(QUrl(url))
# #
# def play_next(self) -> None: # def play_next(self) -> None:
@ -613,7 +614,7 @@ class Window(QMainWindow, Ui_MainWindow):
# - Update clocks # - Update clocks
# """ # """
# #
# DEBUG( # log.debug(
# "musicmuster.play_next(), " # "musicmuster.play_next(), "
# f"next_track={self.next_track.title if self.next_track else None} " # f"next_track={self.next_track.title if self.next_track else None} "
# "current_track=" # "current_track="
@ -623,7 +624,7 @@ class Window(QMainWindow, Ui_MainWindow):
# #
# # If there is no next track set, return. # # If there is no next track set, return.
# if not self.next_track: # if not self.next_track:
# DEBUG("musicmuster.play_next(): no next track selected", True) # log.debug("musicmuster.play_next(): no next track selected", True)
# return # return
# #
# with Session() as session: # with Session() as session:
@ -772,13 +773,13 @@ class Window(QMainWindow, Ui_MainWindow):
# title = self.current_track.title # title = self.current_track.title
# if title: # if title:
# txt = urllib.parse.quote_plus(title) # txt = urllib.parse.quote_plus(title)
# url = Config.INFO_TAB_URL % txt # url = Config.log.info_TAB_URL % txt
# webbrowser.open(url, new=2) # webbrowser.open(url, new=2)
# #
# def stop(self) -> None: # def stop(self) -> None:
# """Stop playing immediately""" # """Stop playing immediately"""
# #
# DEBUG("musicmuster.stop()") # log.debug("musicmuster.stop()")
# #
# self.stop_playing(fade=False) # self.stop_playing(fade=False)
# #
@ -793,20 +794,20 @@ class Window(QMainWindow, Ui_MainWindow):
# - Run end-of-track actions # - Run end-of-track actions
# """ # """
# #
# DEBUG(f"musicmuster.stop_playing({fade=})", True) # log.debug(f"musicmuster.stop_playing({fade=})", True)
# #
# # Return if not playing # # Return if not playing
# if not self.playing: # if not self.playing:
# DEBUG("musicmuster.stop_playing(): not playing", True) # log.debug("musicmuster.stop_playing(): not playing", True)
# return # return
# #
# # Stop/fade track # # Stop/fade track
# self.previous_track_position = self.music.get_position() # self.previous_track_position = self.music.get_position()
# if fade: # if fade:
# DEBUG("musicmuster.stop_playing(): fading music", True) # log.debug("musicmuster.stop_playing(): fading music", True)
# self.music.fade() # self.music.fade()
# else: # else:
# DEBUG("musicmuster.stop_playing(): stopping music", True) # log.debug("musicmuster.stop_playing(): stopping music", True)
# self.music.stop() # self.music.stop()
# #
# # Reset playlist_tab colour # # Reset playlist_tab colour
@ -1157,11 +1158,11 @@ if __name__ == "__main__":
# #
# # Run as required # # Run as required
# if args.update: # if args.update:
# DEBUG("Updating database") # log.debug("Updating database")
# with Session() as session: # with Session() as session:
# update_db(session) # update_db(session)
# # elif args.full_update: # # elif args.full_update:
# # DEBUG("Full update of database") # # log.debug("Full update of database")
# # with Session() as session: # # with Session() as session:
# # full_update_db(session) # # full_update_db(session)
# else: # else:
@ -1173,4 +1174,5 @@ if __name__ == "__main__":
win.show() win.show()
sys.exit(app.exec()) sys.exit(app.exec())
except Exception: except Exception:
EXCEPTION("Unhandled Exception caught by musicmuster.main()") msg = "Unhandled Exception caught by musicmuster.main()"
log.exception(msg, exc_info=True, stack_info=True)

View File

@ -28,7 +28,7 @@
# from config import Config # from config import Config
# from datetime import datetime, timedelta # from datetime import datetime, timedelta
# from helpers import get_relative_date, open_in_audacity # from helpers import get_relative_date, open_in_audacity
# from log import DEBUG, ERROR # from log import log.debug, log.error
# from models import ( # from models import (
# Notes, # Notes,
# Playdates, # Playdates,
@ -230,7 +230,7 @@
# self.scrollToItem(self.item(row, 1)) # self.scrollToItem(self.item(row, 1))
# super().dropEvent(event) # super().dropEvent(event)
# #
# DEBUG( # log.debug(
# "playlist.dropEvent(): " # "playlist.dropEvent(): "
# f"Moved row(s) {rows} to become row {drop_row}" # f"Moved row(s) {rows} to become row {drop_row}"
# ) # )
@ -258,7 +258,7 @@
# item = self.itemAt(event.pos()) # item = self.itemAt(event.pos())
# if item is not None: # if item is not None:
# row = item.row() # row = item.row()
# DEBUG(f"playlist.eventFilter(): Right-click on row {row}") # log.debug(f"playlist.eventFilter(): Right-click on row {row}")
# current = row == self._get_current_track_row() # current = row == self._get_current_track_row()
# next_row = row == self._get_next_track_row() # next_row = row == self._get_next_track_row()
# self.menu = QMenu(self) # self.menu = QMenu(self)
@ -300,7 +300,7 @@
# def closeEvent(self, event) -> None: # def closeEvent(self, event) -> None:
# """Save column widths""" # """Save column widths"""
# #
# DEBUG(f"playlists.closeEvent()") # log.debug(f"playlists.closeEvent()")
# with Session() as session: # with Session() as session:
# for column in range(self.columnCount()): # for column in range(self.columnCount()):
# width = self.columnWidth(column) # width = self.columnWidth(column)
@ -381,7 +381,7 @@
# row = self.currentRow() # row = self.currentRow()
# else: # else:
# row = self.rowCount() # row = self.rowCount()
# DEBUG( # log.debug(
# f"playlists.insert_track({session=}, {track=}, {repaint=}), " # f"playlists.insert_track({session=}, {track=}, {repaint=}), "
# f"{row=}" # f"{row=}"
# ) # )
@ -613,7 +613,7 @@
# session.add(note) # session.add(note)
# playlist_notes[note.id] = note # playlist_notes[note.id] = note
# if row != note.row: # if row != note.row:
# DEBUG(f"Updating: {playlist.name=}, {row=}, {note.row=}") # log.debug(f"Updating: {playlist.name=}, {row=}, {note.row=}")
# note.update(session=session, row=row) # note.update(session=session, row=row)
# #
# # Database # # Database
@ -626,7 +626,7 @@
# #
# # Notes to remove from database # # Notes to remove from database
# for note_id in set(database_notes.keys()) - set(playlist_notes.keys()): # for note_id in set(database_notes.keys()) - set(playlist_notes.keys()):
# DEBUG( # log.debug(
# "_save_playlist(): " # "_save_playlist(): "
# f"Delete {note_id=} from {self=} in database" # f"Delete {note_id=} from {self=} in database"
# ) # )
@ -635,7 +635,7 @@
# # Note rows to update in playlist database # # Note rows to update in playlist database
# for note_id in set(playlist_notes.keys()) & set(database_notes.keys()): # for note_id in set(playlist_notes.keys()) & set(database_notes.keys()):
# if playlist_notes[note_id].row != database_notes[note_id].row: # if playlist_notes[note_id].row != database_notes[note_id].row:
# DEBUG( # log.debug(
# f"_save_playlist(): Update notes row in database " # f"_save_playlist(): Update notes row in database "
# f"from {database_notes[note_id]=} " # f"from {database_notes[note_id]=} "
# f"to {playlist_notes[note_id]=}" # f"to {playlist_notes[note_id]=}"
@ -973,7 +973,7 @@
# def _audacity(self, row: int) -> None: # def _audacity(self, row: int) -> None:
# """Open track in Audacity. Audacity must be already running""" # """Open track in Audacity. Audacity must be already running"""
# #
# DEBUG(f"_audacity({row})") # log.debug(f"_audacity({row})")
# #
# if row in self._get_notes_rows(): # if row in self._get_notes_rows():
# return None # return None
@ -1003,7 +1003,7 @@
# Otherwise, return None. # Otherwise, return None.
# """ # """
# #
# DEBUG(f"_copy_path({row})") # log.debug(f"_copy_path({row})")
# #
# if row in self._get_notes_rows(): # if row in self._get_notes_rows():
# return None # return None
@ -1024,7 +1024,7 @@
# return # return
# #
# new_text: str = self.item(row, column).text() # new_text: str = self.item(row, column).text()
# DEBUG(f"_cell_changed({row=}, {column=}, {new_text=}") # log.debug(f"_cell_changed({row=}, {column=}, {new_text=}")
# #
# with Session() as session: # with Session() as session:
# if row in self._get_notes_rows(): # if row in self._get_notes_rows():
@ -1035,14 +1035,14 @@
# start_time = self._get_note_text_time(new_text) # start_time = self._get_note_text_time(new_text)
# if start_time: # if start_time:
# self._set_row_start_time(row, start_time) # self._set_row_start_time(row, start_time)
# DEBUG( # log.debug(
# f"_cell_changed:Note {new_text} contains valid " # f"_cell_changed:Note {new_text} contains valid "
# f"{start_time=}" # f"{start_time=}"
# ) # )
# else: # else:
# # Reset row start time in case it used to have one # # Reset row start time in case it used to have one
# self._set_row_start_time(row, None) # self._set_row_start_time(row, None)
# DEBUG( # log.debug(
# f"_cell_changed:Note {new_text} does not contain " # f"_cell_changed:Note {new_text} does not contain "
# "start time" # "start time"
# ) # )
@ -1053,12 +1053,12 @@
# elif column == self.COL_TITLE: # elif column == self.COL_TITLE:
# track.update_title(session, title=new_text) # track.update_title(session, title=new_text)
# else: # else:
# ERROR("_cell_changed(): unrecognised column") # log.error("_cell_changed(): unrecognised column")
# #
# def _cell_edit_ended(self) -> None: # def _cell_edit_ended(self) -> None:
# """Called when cell edit ends""" # """Called when cell edit ends"""
# #
# DEBUG("_cell_edit_ended()") # log.debug("_cell_edit_ended()")
# #
# self.editing_cell = False # self.editing_cell = False
# #
@ -1075,7 +1075,7 @@
# that keys work during edit. # that keys work during edit.
# """ # """
# #
# DEBUG(f"_cell_edit_started({row=}, {column=})") # log.debug(f"_cell_edit_started({row=}, {column=})")
# #
# self.editing_cell = True # self.editing_cell = True
# # Disable play controls so that keyboard input doesn't disturb playing # # Disable play controls so that keyboard input doesn't disturb playing
@ -1119,7 +1119,7 @@
# def _delete_rows(self) -> None: # def _delete_rows(self) -> None:
# """Delete mutliple rows""" # """Delete mutliple rows"""
# #
# DEBUG("playlist._delete_rows()") # log.debug("playlist._delete_rows()")
# #
# selected_rows: List[int] = sorted( # selected_rows: List[int] = sorted(
# set(item.row() for item in self.selectedItems()) # set(item.row() for item in self.selectedItems())
@ -1378,7 +1378,7 @@
# #
# if row is None: # if row is None:
# row = self.rowCount() # row = self.rowCount()
# DEBUG(f"playlist.inset_note(): row={row}") # log.debug(f"playlist.inset_note(): row={row}")
# #
# self.insertRow(row) # self.insertRow(row)
# #
@ -1493,7 +1493,7 @@
# if len(matches) <= 1: # if len(matches) <= 1:
# return matches # return matches
# else: # else:
# ERROR( # log.error(
# f"Multiple matches for metadata '{metadata}' found " # f"Multiple matches for metadata '{metadata}' found "
# f"in rows: {', '.join([str(x) for x in matches])}" # f"in rows: {', '.join([str(x) for x in matches])}"
# ) # )
@ -1516,7 +1516,7 @@
# def _mplayer(self, row: int) -> None: # def _mplayer(self, row: int) -> None:
# """Play track with mplayer""" # """Play track with mplayer"""
# #
# DEBUG(f"_mplayer({row})") # log.debug(f"_mplayer({row})")
# #
# if row in self._get_notes_rows(): # if row in self._get_notes_rows():
# return None # return None
@ -1534,7 +1534,7 @@
# Otherwise, return None. # Otherwise, return None.
# """ # """
# #
# DEBUG(f"_rescan({row=})") # log.debug(f"_rescan({row=})")
# #
# with Session() as session: # with Session() as session:
# if row in self._get_track_rows(): # if row in self._get_track_rows():
@ -1650,7 +1650,7 @@
# - Notify musicmuster # - Notify musicmuster
# """ # """
# #
# DEBUG(f"_set_next({row=})") # log.debug(f"_set_next({row=})")
# #
# # Check row is a track row # # Check row is a track row
# if row in self._get_notes_rows(): # if row in self._get_notes_rows():
@ -1740,7 +1740,7 @@
# duration = helpers.ms_to_mmss(ms) # duration = helpers.ms_to_mmss(ms)
# note_object = self._get_row_notes_object(start_row, session) # note_object = self._get_row_notes_object(start_row, session)
# if not note_object: # if not note_object:
# ERROR("Can't get note_object in playlists._set_timed_section") # log.error("Can't get note_object in playlists._set_timed_section")
# note_text = note_object.note # note_text = note_object.note
# caveat = "" # caveat = ""
# if no_end: # if no_end:
@ -1754,7 +1754,7 @@
# Update the passed row with info from the passed track. # Update the passed row with info from the passed track.
# """ # """
# #
# DEBUG(f"_update_row({row=}, {track=}") # log.debug(f"_update_row({row=}, {track=}")
# #
# item_startgap: QTableWidgetItem = self.item(row, self.COL_MSS) # item_startgap: QTableWidgetItem = self.item(row, self.COL_MSS)
# item_startgap.setText(str(track.start_gap)) # item_startgap.setText(str(track.start_gap))

View File

@ -14,7 +14,7 @@
# leading_silence, # leading_silence,
# trailing_silence, # trailing_silence,
# ) # )
# from log import DEBUG, INFO # from log import log.debug, log.info
# from models import Notes, Playdates, Session, Tracks # from models import Notes, Playdates, Session, Tracks
# from mutagen.flac import FLAC # from mutagen.flac import FLAC
# from mutagen.mp3 import MP3 # from mutagen.mp3 import MP3
@ -27,7 +27,7 @@
# def main(): # def main():
# """Main loop""" # """Main loop"""
# #
# DEBUG("Starting") # log.debug("Starting")
# #
# p = argparse.ArgumentParser() # p = argparse.ArgumentParser()
# # Only allow one option to be specified # # Only allow one option to be specified
@ -42,17 +42,17 @@
# #
# # Run as required # # Run as required
# if args.update: # if args.update:
# DEBUG("Updating database") # log.debug("Updating database")
# with Session() as session: # with Session() as session:
# update_db(session) # update_db(session)
# elif args.full_update: # elif args.full_update:
# DEBUG("Full update of database") # log.debug("Full update of database")
# with Session() as session: # with Session() as session:
# full_update_db(session) # full_update_db(session)
# else: # else:
# INFO("No action specified") # log.info("No action specified")
# #
# DEBUG("Finished") # log.debug("Finished")
# #
# #
# def create_track_from_file(session, path, normalise=None, tags=None): # def create_track_from_file(session, path, normalise=None, tags=None):
@ -85,7 +85,7 @@
# # Check type # # Check type
# ftype = os.path.splitext(path)[1][1:] # ftype = os.path.splitext(path)[1][1:]
# if ftype not in ['mp3', 'flac']: # if ftype not in ['mp3', 'flac']:
# INFO(f"File type {ftype} not implemented") # log.info(f"File type {ftype} not implemented")
# return track # return track
# #
# # Get current file gid, uid and permissions # # Get current file gid, uid and permissions
@ -95,7 +95,7 @@
# fd, temp_path = tempfile.mkstemp() # fd, temp_path = tempfile.mkstemp()
# shutil.copyfile(path, temp_path) # shutil.copyfile(path, temp_path)
# except Exception as err: # except Exception as err:
# DEBUG(f"songdb.create_track_from_file({path}): err1: {repr(err)}") # log.debug(f"songdb.create_track_from_file({path}): err1: {repr(err)}")
# return # return
# #
# # Overwrite original file with normalised output # # Overwrite original file with normalised output
@ -118,7 +118,7 @@
# dst[tag] = src[tag] # dst[tag] = src[tag]
# dst.save() # dst.save()
# except Exception as err: # except Exception as err:
# DEBUG(f"songdb.create_track_from_file({path}): err2: {repr(err)}") # log.debug(f"songdb.create_track_from_file({path}): err2: {repr(err)}")
# # Restore original file # # Restore original file
# shutil.copyfile(path, temp_path) # shutil.copyfile(path, temp_path)
# finally: # finally:
@ -132,7 +132,7 @@
# """Rescan all entries in database""" # """Rescan all entries in database"""
# #
# def log(msg): # def log(msg):
# INFO(f"full_update_db(): {msg}") # log.info(f"full_update_db(): {msg}")
# #
# def check_change(track_id, title, attribute, old, new): # def check_change(track_id, title, attribute, old, new):
# if new > (old * 1.1) or new < (old * 0.9): # if new > (old * 1.1) or new < (old * 0.9):
@ -235,7 +235,7 @@
# #
# track = Tracks.get_by_path(session, path) # track = Tracks.get_by_path(session, path)
# if not track: # if not track:
# ERROR(f"update_db: {path} not found in db") # log.error(f"update_db: {path} not found in db")
# continue # continue
# #
# paths_not_found.append(track) # paths_not_found.append(track)
@ -264,7 +264,7 @@
# # Spike # # Spike
# # # #
# # # Manage tracks listed in database but where path is invalid # # # Manage tracks listed in database but where path is invalid
# # DEBUG(f"Invalid {path=} in database", True) # # log.debug(f"Invalid {path=} in database", True)
# # track = Tracks.get_by_path(session, path) # # track = Tracks.get_by_path(session, path)
# # messages.append(f"Remove from database: {path=} {track=}") # # messages.append(f"Remove from database: {path=} {track=}")
# # # #
@ -279,10 +279,10 @@
# # for playlist_track in track.playlists: # # for playlist_track in track.playlists:
# # row = playlist_track.row # # row = playlist_track.row
# # # Remove playlist entry # # # Remove playlist entry
# # DEBUG(f"Remove {row=} from {playlist_track.playlist_id}", True) # # log.debug(f"Remove {row=} from {playlist_track.playlist_id}", True)
# # playlist_track.playlist.remove_track(session, row) # # playlist_track.playlist.remove_track(session, row)
# # # Create note # # # Create note
# # DEBUG(f"Add note at {row=} to {playlist_track.playlist_id=}", True) # # log.debug(f"Add note at {row=} to {playlist_track.playlist_id=}", True)
# # Notes(session, playlist_track.playlist_id, row, note_txt) # # Notes(session, playlist_track.playlist_id, row, note_txt)
# # # #
# # # Remove Track entry pointing to invalid path # # # Remove Track entry pointing to invalid path