modularize src

+ merge main.py and manage.py
+ move start logic to swingmusic/__main__.py
+ add a run.py on the project root
This commit is contained in:
cwilvx
2025-05-25 20:35:54 +03:00
parent 76fc97e088
commit 86fabcd5e3
171 changed files with 658 additions and 627 deletions
+54
View File
@@ -0,0 +1,54 @@
"""
Prepares the server for use.
"""
from time import time
import uuid
from swingmusic.lib.mapstuff import (
map_album_colors,
map_artist_colors,
map_favorites,
map_scrobble_data,
)
from swingmusic.setup.files import create_config_dir
from swingmusic.setup.sqlite import run_migrations, setup_sqlite
from swingmusic.store.albums import AlbumStore
from swingmusic.store.artists import ArtistStore
from swingmusic.store.folder import FolderStore
from swingmusic.store.tracks import TrackStore
from swingmusic.utils.generators import get_random_str
from swingmusic.config import UserConfig
def run_setup():
"""
Creates the config directory, runs migrations, and loads settings.
"""
create_config_dir()
# setup config file
config = UserConfig()
config.setup_config_file()
if not config.serverId:
config.serverId = str(uuid.uuid4())
setup_sqlite()
run_migrations()
def load_into_mem():
"""
Load all tracks, albums, and artists into memory.
"""
# INFO: Load all tracks, albums, and artists data into memory
key = str(time())
TrackStore.load_all_tracks(get_random_str())
AlbumStore.load_albums(key)
ArtistStore.load_artists(key)
FolderStore.load_filepaths()
map_scrobble_data()
map_favorites()
map_artist_colors()
map_album_colors()
+98
View File
@@ -0,0 +1,98 @@
"""
This module contains the functions that are used to
create the config directory and copy the assets to the app directory.
"""
import os
import shutil
from swingmusic import settings
from swingmusic.utils.filesystem import get_home_res_path
class CopyFiles:
"""
Copies assets to the app directory.
"""
def __init__(self) -> None:
assets_dir = "assets"
if settings.IS_BUILD:
assets_dir = get_home_res_path("assets")
files = [
{
"src": assets_dir,
"dest": os.path.join(settings.Paths.get_app_dir(), "assets"),
"is_dir": True,
}
]
for entry in files:
src = os.path.join(os.getcwd(), entry["src"])
if entry["is_dir"]:
shutil.copytree(
src,
entry["dest"],
ignore=shutil.ignore_patterns(
"*.pyc",
),
copy_function=shutil.copy2,
dirs_exist_ok=True,
)
break
shutil.copy2(src, entry["dest"])
def create_config_dir() -> None:
"""
Creates the config directory if it doesn't exist.
"""
sm_thumb_path = settings.Paths.get_sm_thumb_path()
lg_thumb_path = settings.Paths.get_lg_thumb_path()
md_thumb_path = settings.Paths.get_md_thumb_path()
xsm_thumb_path = settings.Paths.get_xsm_thumb_path()
small_artist_img_path = settings.Paths.get_sm_artist_img_path()
md_artist_img_path = settings.Paths.get_md_artist_img_path()
large_artist_img_path = settings.Paths.get_lg_artist_img_path()
playlist_img_path = os.path.join("images", "playlists")
mixes_img_path = settings.Paths.get_mixes_img_path()
og_mixes_img_path = settings.Paths.get_og_mixes_img_path()
md_mixes_img_path = settings.Paths.get_md_mixes_img_path()
sm_mixes_img_path = settings.Paths.get_sm_mixes_img_path()
dirs = [
"", # creates the config folder
sm_thumb_path,
lg_thumb_path,
md_thumb_path,
xsm_thumb_path,
"plugins/lyrics",
playlist_img_path,
md_artist_img_path,
small_artist_img_path,
large_artist_img_path,
mixes_img_path,
og_mixes_img_path,
md_mixes_img_path,
sm_mixes_img_path,
]
for _dir in dirs:
path = os.path.join(settings.Paths.get_app_dir(), _dir)
exists = os.path.exists(path)
if not exists:
# exist_ok=True to create parent directories if they don't exist
os.makedirs(path, exist_ok=True)
os.chmod(path, 0o755)
# copy assets to the app directory
CopyFiles()
+38
View File
@@ -0,0 +1,38 @@
"""
Module to setup Sqlite databases and tables.
Applies migrations.
"""
from sqlalchemy import create_engine
from swingmusic.db.userdata import UserTable
from swingmusic.migrations import apply_migrations
from swingmusic.settings import DbPaths
from swingmusic.db.engine import DbEngine
from swingmusic.db import create_all_tables
# from swingmusic.db.libdata import create_all as create_user_tables
def run_migrations():
"""
Run migrations and updates migration version.
"""
apply_migrations()
def setup_sqlite():
"""
Create Sqlite databases and tables.
"""
DbEngine._engine = create_engine(
f"sqlite+pysqlite:///{DbPaths.get_app_db_path()}",
echo=False,
max_overflow=20,
pool_size=10,
)
create_all_tables()
# create_user_tables()
if not list(UserTable.get_all()):
UserTable.insert_default_user()