Restructure project layout; implement logging

This commit is contained in:
Keith Edmunds 2021-03-26 09:19:04 +00:00
parent bf990f7486
commit b58b9fc188
9 changed files with 81 additions and 13 deletions

19
app/config.py Normal file
View File

@ -0,0 +1,19 @@
import logging
import os
class Config(object):
DISPLAY_SQL = False
ERRORS_TO = ['kae@midnighthax.com']
LOG_LEVEL_STDERR = logging.INFO
LOG_LEVEL_SYSLOG = logging.DEBUG
MAIL_PASSWORD = os.environ.get('MAIL_PASSWORD')
MAIL_PORT = int(os.environ.get('MAIL_PORT') or 25)
MAIL_SERVER = os.environ.get('MAIL_SERVER') or "woodlands.midnighthax.com"
MAIL_USERNAME = os.environ.get('MAIL_USERNAME')
MAIL_USE_TLS = os.environ.get('MAIL_USE_TLS') is not None
MYSQL_CONNECT = "mysql+mysqldb://songdb:songdb@localhost/songdb"
ROOT = "/home/kae/music"
config = Config

45
app/log.py Executable file
View File

@ -0,0 +1,45 @@
#!/usr/bin/python3
import logging
import logging.handlers
from config import Config
log = logging.getLogger("musicmuster")
log.setLevel(logging.DEBUG)
# stderr
stderr = logging.StreamHandler()
stderr.setLevel(Config.LOG_LEVEL_STDERR)
# syslog
syslog = logging.handlers.SysLogHandler(address='/dev/log')
syslog.setLevel(Config.LOG_LEVEL_SYSLOG)
# create formatter and add it to the handlers
formatter = logging.Formatter('[%(name)s:%(levelname)s]: %(message)s')
stderr.setFormatter(formatter)
syslog.setFormatter(formatter)
# add the handlers to the log
log.addHandler(stderr)
log.addHandler(syslog)
def DEBUG(msg):
log.debug(msg)
def ERROR(msg):
log.error(msg)
def INFO(msg):
log.info(msg)
if __name__ == "__main__":
DEBUG("hi debug")
ERROR("hi error")
INFO("hi info")

View File

@ -11,11 +11,9 @@ from sqlalchemy import Column, Float, DateTime, Integer, String
from sqlalchemy.orm.exc import NoResultFound
from sqlalchemy.orm import sessionmaker
# "Constants"
DISPLAY_SQL = False
MYSQL_CONNECT = "mysql+mysqldb://songdb:songdb@localhost/songdb"
ROOT = "/home/kae/music"
from config import Config
# "Constants"
# Instantiate logging
pytiger.logging.config.basic_config(stderr=False, level=logging.INFO)
log = logging.getLogger(__name__)
@ -26,9 +24,9 @@ log.info("Starting")
# Set up database connection
log.info("Connect to database")
engine = sqlalchemy.create_engine(f"{MYSQL_CONNECT}?charset=utf8",
engine = sqlalchemy.create_engine(f"{Config.MYSQL_CONNECT}?charset=utf8",
encoding='utf-8',
echo=DISPLAY_SQL)
echo=Config.DISPLAY_SQL)
Base = declarative_base()
Base.metadata.create_all(engine)
@ -73,7 +71,7 @@ class Tracks(Base):
def get_path(id):
try:
path = session.query(Tracks.path).filter(Tracks.id == id).one()[0]
return os.path.join(ROOT, path)
return os.path.join(Config.ROOT, path)
except NoResultFound:
print(f"Can't find track id {id}")
return None

View File

@ -3,15 +3,18 @@
import vlc
import sys
from log import INFO, ERROR, DEBUG
from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import QApplication, QDialog, QMainWindow
from PyQt5.QtWidgets import QTableWidgetItem, QFileDialog, QListWidgetItem
from threading import Timer
from tinytag import TinyTag
from main_window_ui import Ui_MainWindow
from dlg_search_database_ui import Ui_Dialog
from ui.main_window_ui import Ui_MainWindow
from ui.dlg_search_database_ui import Ui_Dialog
from config import Config
from model import Tracks
@ -93,16 +96,15 @@ class Window(QMainWindow, Ui_MainWindow):
# TODO: get_path may raise exception
track_path = Tracks.get_path(track_id)
if track_path:
INFO(f"play: {track_path}")
player = vlc.MediaPlayer(track_path)
import ipdb; ipdb.set_trace()
player.play()
def selectFile(self):
dlg = QFileDialog()
dlg.setFileMode(QFileDialog.ExistingFile)
dlg.setViewMode(QFileDialog.Detail)
ROOT = "/home/kae/music/"
dlg.setDirectory(ROOT)
dlg.setDirectory(Config.ROOT)
dlg.setNameFilter("Music files (*.flac *.mp3)")
if dlg.exec_():
@ -164,8 +166,12 @@ class DbDialog(QDialog):
self.parent().add_to_playlist(track)
if __name__ == "__main__":
def main():
app = QApplication(sys.argv)
win = Window()
win.show()
sys.exit(app.exec())
if __name__ == "__main__":
main()

View File

Before

Width:  |  Height:  |  Size: 8.7 KiB

After

Width:  |  Height:  |  Size: 8.7 KiB

View File

Before

Width:  |  Height:  |  Size: 4.6 KiB

After

Width:  |  Height:  |  Size: 4.6 KiB