refactor(server): register loaded module in sys.modules, add __all__

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
2026-06-08 19:54:37 -07:00
parent ad660afae3
commit c46ff2ff1a
2 changed files with 9 additions and 0 deletions

View File

@@ -4,6 +4,7 @@ between the REST API and the CLI; musicfetch itself is unchanged."""
import importlib.machinery
import importlib.util
import os
import sys
_HERE = os.path.dirname(os.path.abspath(__file__))
_MF_PATH = os.environ.get("MUSICFETCH_BIN", os.path.join(_HERE, "..", "musicfetch"))
@@ -14,6 +15,7 @@ _loader = importlib.machinery.SourceFileLoader("musicfetch_core", _MF_PATH)
_spec = importlib.util.spec_from_loader("musicfetch_core", _loader)
_mod = importlib.util.module_from_spec(_spec)
_spec.loader.exec_module(_mod) # safe: musicfetch guards main() behind __main__
sys.modules["musicfetch_core"] = _mod
Hit = _mod.Hit
build_combined_hits = _mod.build_combined_hits
@@ -22,3 +24,6 @@ act_youtube = _mod.act_youtube
act_lidarr_album = _mod.act_lidarr_album
act_lidarr_artist = _mod.act_lidarr_artist
QUALITY_CHOICES = _mod.QUALITY_CHOICES
__all__ = ["Hit", "build_combined_hits", "pick", "act_youtube",
"act_lidarr_album", "act_lidarr_artist", "QUALITY_CHOICES"]

View File

@@ -1,3 +1,6 @@
"""Tests for the server.mf loader."""
def test_mf_reexports_musicfetch_symbols():
from server import mf
assert hasattr(mf, "Hit")
@@ -6,6 +9,7 @@ def test_mf_reexports_musicfetch_symbols():
assert callable(mf.act_youtube)
assert callable(mf.act_lidarr_album)
assert callable(mf.act_lidarr_artist)
assert isinstance(mf.QUALITY_CHOICES, list)
def test_mf_hit_constructs():