buzz/buzz/buzz.py
2024-03-14 01:51:06 +00:00

70 lines
2.2 KiB
Python

import faulthandler
import logging
import multiprocessing
import os
import platform
import sys
from typing import TextIO
from platformdirs import user_log_dir
# Check for segfaults if not running in frozen mode
if getattr(sys, "frozen", False) is False:
faulthandler.enable()
# Sets stderr to no-op TextIO when None (run as Windows GUI).
# Resolves https://github.com/chidiwilliams/buzz/issues/221
if sys.stderr is None:
sys.stderr = TextIO()
# Adds the current directory to the PATH, so the ffmpeg binary get picked up:
# https://stackoverflow.com/a/44352931/9830227
app_dir = getattr(sys, "_MEIPASS", os.path.dirname(os.path.abspath(__file__)))
os.environ["PATH"] += os.pathsep + app_dir
# Add the app directory to the DLL list: https://stackoverflow.com/a/64303856
if platform.system() == "Windows":
os.add_dll_directory(app_dir)
def main():
if platform.system() == "Linux":
multiprocessing.set_start_method("spawn")
# Fixes opening new window when app has been frozen on Windows:
# https://stackoverflow.com/a/33979091
multiprocessing.freeze_support()
log_dir = user_log_dir(appname="Buzz")
os.makedirs(log_dir, exist_ok=True)
log_format = (
"[%(asctime)s] %(module)s.%(funcName)s:%(lineno)d %(levelname)s -> %(message)s"
)
logging.basicConfig(
filename=os.path.join(log_dir, "logs.txt"),
level=logging.DEBUG,
format=log_format,
)
logging.debug("app_dir: %s", app_dir)
if getattr(sys, "frozen", False) is False:
stdout_handler = logging.StreamHandler(sys.stdout)
stdout_handler.setLevel(logging.DEBUG)
stdout_handler.setFormatter(logging.Formatter(log_format))
logging.getLogger().addHandler(stdout_handler)
from buzz.cli import parse_command_line
from buzz.widgets.application import Application
from buzz.db.dao.transcription_dao import TranscriptionDAO
from buzz.db.dao.transcription_segment_dao import TranscriptionSegmentDAO
from buzz.db.service.transcription_service import TranscriptionService
from buzz.db.db import setup_app_db
db = setup_app_db()
app = Application(
TranscriptionService(TranscriptionDAO(db), TranscriptionSegmentDAO(db))
)
parse_command_line(app)
sys.exit(app.exec())