Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor initialization, functions and classes #1388

Draft
wants to merge 13 commits into
base: master
Choose a base branch
from
58 changes: 30 additions & 28 deletions src/tauon/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@

from tauon.t_modules.logging import CustomLoggingFormatter, LogHistoryHandler

from tauon.t_modules import t_bootstrap
from tauon.t_modules.t_bootstrap import Holder

log = LogHistoryHandler()
formatter = logging.Formatter('[%(levelname)s] %(message)s')
Expand Down Expand Up @@ -427,32 +427,33 @@ def transfer_args_and_exit() -> None:
SDL_FreeSurface(raw_image)
SDL_DestroyTexture(sdl_texture)

holder = t_bootstrap.holder
holder.t_window = t_window
holder.renderer = renderer
holder.logical_size = logical_size
holder.window_size = window_size
holder.window_default_size = window_default_size
holder.scale = scale
holder.maximized = maximized
holder.transfer_args_and_exit = transfer_args_and_exit
holder.draw_border = draw_border
holder.window_opacity = window_opacity
holder.old_window_position = old_window_position
holder.install_directory = install_directory
holder.user_directory = user_directory
holder.pyinstaller_mode = pyinstaller_mode
holder.phone = phone
holder.window_title = window_title
holder.fs_mode = fs_mode
holder.t_title = t_title
holder.n_version = n_version
holder.t_version = t_version
holder.t_id = t_id
holder.t_agent = t_agent
holder.dev_mode = dev_mode
holder.instance_lock = fp
holder.log = log
holder = Holder(
t_window=t_window,
renderer=renderer,
logical_size=logical_size,
window_size=window_size,
window_default_size=window_default_size,
scale=scale,
maximized=maximized,
transfer_args_and_exit=transfer_args_and_exit,
draw_border=draw_border,
window_opacity=window_opacity,
old_window_position=old_window_position,
install_directory=install_directory,
user_directory=user_directory,
pyinstaller_mode=pyinstaller_mode,
phone=phone,
window_title=window_title,
fs_mode=fs_mode,
t_title=t_title,
n_version=n_version,
t_version=t_version,
t_id=t_id,
t_agent=t_agent,
dev_mode=dev_mode,
instance_lock=fp,
log=log,
)

del raw_image
del sdl_texture
Expand All @@ -464,7 +465,8 @@ def transfer_args_and_exit() -> None:

def main() -> None:
"""Launch Tauon by means of importing t_main.py"""
from tauon.t_modules import t_main
from tauon.t_modules.t_main import main as t_main
t_main(holder)

if __name__ == "__main__":
main()
Expand Down
34 changes: 16 additions & 18 deletions src/tauon/t_modules/t_bootstrap.py
Original file line number Diff line number Diff line change
@@ -1,44 +1,42 @@
from __future__ import annotations

#from dataclasses import dataclass
from dataclasses import dataclass
from typing import TYPE_CHECKING

if TYPE_CHECKING:
from collections.abc import Callable
from io import TextIOWrapper
from pathlib import Path
from typing import Any
from sdl2 import SDL_Renderer, SDL_Window

from tauon.__main__ import LogHistoryHandler

#@dataclass
@dataclass
class Holder:
"""Class that holds variables for forwarding them from tauon.py to t_main.py"""
"""Class that holds variables for forwarding them from __main__.py to t_main.py"""

t_window: Any # SDL_CreateWindow() return type (???)
renderer: Any # SDL_CreateRenderer() return type (???)
logical_size: list[int] # X Y res
window_size: list[int] # X Y res
t_window: SDL_Window # SDL_CreateWindow() return type
renderer: SDL_Renderer # SDL_CreateRenderer() return type
logical_size: list[int] # X Y res
window_size: list[int] # X Y res
maximized: bool
scale: float
window_opacity: float
draw_border: bool
transfer_args_and_exit: Callable[[]] # transfer_args_and_exit() - TODO(Martin): This should probably be moved to extra module
transfer_args_and_exit: Callable[[]] # transfer_args_and_exit() - TODO(Martin): This should probably be moved to extra module
old_window_position: tuple [int, int] | None # X Y res
install_directory: Path
user_directory: Path
pyinstaller_mode: bool
phone: bool
window_default_size: tuple[int, int] # X Y res
window_title: bytes # t_title.encode("utf-8")
window_default_size: tuple[int, int] # X Y res
window_title: bytes # t_title.encode("utf-8")
fs_mode: bool
t_title: str # "Tauon"
n_version: str # "7.9.0"
t_version: str # "v" + n_version
t_id: str # "tauonmb" | "com.github.taiko2k.tauonmb"
t_agent: str # "TauonMusicBox/7.9.0"
t_title: str # "Tauon"
n_version: str # "7.9.0"
t_version: str # "v" + n_version
t_id: str # "tauonmb" | "com.github.taiko2k.tauonmb"
t_agent: str # "TauonMusicBox/7.9.0"
dev_mode: bool
instance_lock: TextIOWrapper | None
log: LogHistoryHandler

holder = Holder()
30 changes: 27 additions & 3 deletions src/tauon/t_modules/t_db_migrate.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from pathlib import Path
from typing import TYPE_CHECKING

from tauon.t_modules.t_extra import TauonPlaylist, TauonQueueItem
from tauon.t_modules.t_extra import RadioPlaylist, RadioStation, TauonPlaylist, TauonQueueItem

if TYPE_CHECKING:
from tauon.t_modules.t_main import GuiVar, Prefs, StarStore, TrackClass
Expand All @@ -28,7 +28,7 @@ def database_migrate(
gui: GuiVar,
gen_codes: dict[int, str],
prefs: Prefs,
radio_playlists: list[TauonPlaylist],
radio_playlists: list[dict[str, int | str | list[dict[str, str]]]] | list[RadioPlaylist],
p_force_queue: list | list[TauonQueueItem],
theme: int,
) -> tuple[
Expand All @@ -40,7 +40,7 @@ def database_migrate(
Prefs,
GuiVar,
dict[int, str],
list[TauonPlaylist]]:
list[RadioPlaylist]]:
"""Migrate database to a newer version if we're behind

Returns all the objects that could've been possibly changed:
Expand Down Expand Up @@ -543,4 +543,28 @@ def database_migrate(
multi_playlist = new_multi_playlist
p_force_queue = new_queue

if db_version <= 69:
logging.info("Updating database to version 69")
new_radio_playlists: list[RadioPlaylist] = []
for playlist in radio_playlists:
stations: list[RadioStation] = []

for station in playlist["items"]:
stations.append(
RadioStation(
title=station["title"],
stream_url=station["stream_url"],
country=station.get("country", ""),
website_url=station.get("website_url", ""),
icon=station.get("icon", ""),
stream_url_fallback=station.get("stream_url_unresolved", "")))
new_radio_playlists.append(
RadioPlaylist(
uid=playlist["uid"],
name=playlist["name"],
scroll=playlist.get("scroll", 0),
stations=stations))
radio_playlists = new_radio_playlists


return master_library, multi_playlist, star_store, p_force_queue, theme, prefs, gui, gen_codes, radio_playlists
22 changes: 20 additions & 2 deletions src/tauon/t_modules/t_extra.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
import time
import urllib.parse
import zipfile
from dataclasses import dataclass
from dataclasses import dataclass, field
from typing import TYPE_CHECKING

from gi.repository import GLib
Expand All @@ -45,6 +45,21 @@

from tauon.t_modules.t_main import TrackClass

@dataclass
class RadioStation:
title: str
stream_url: str
country: str = ""
website_url: str = ""
icon: str = ""
stream_url_fallback: str = ""

@dataclass
class RadioPlaylist:
name: str
uid: int
scroll: int = 0
stations: list[RadioStation] = field(default_factory=list)

@dataclass
class TauonQueueItem:
Expand Down Expand Up @@ -176,8 +191,11 @@ def rm_16(line: str) -> str:
return line


def get_display_time(seconds: str) -> str:
def get_display_time(seconds: float) -> str:
"""Returns a string from seconds to a compact time format, e.g 2h:23"""
if math.isinf(seconds) or math.isnan(seconds):
logging.error("Infinite/NaN time passed to get_display_time()!")
return "??:??"
result = divmod(int(seconds), 60)
if result[0] > 99:
result = divmod(result[0], 60)
Expand Down
Loading