mirror of
https://github.com/jeertmans/manim-slides.git
synced 2025-08-06 06:12:56 +08:00
refactor(lib): change how manim API is imported (#285)
* refactor(lib): change how manim API is imported * chore(lib): delete old files * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * wip: moving all commands * adding animations * fix tests * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * fix mypy * fixes * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * trying to fix docs * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * wip: docs * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * make it work * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * wip test * tests are working * improving docs * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * fix index * docs: nicer shift * docs: nicer quickstart example * fix tests * change tests * move coverage to test workflow * fix(tests): remove resolve * strict resolve * change local path test * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * documented changes * cleanup docs * cleanup files * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * fix(ci): set type --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
This commit is contained in:
@ -1,62 +1,55 @@
|
||||
import importlib
|
||||
import os
|
||||
import sys
|
||||
from contextlib import contextmanager
|
||||
from importlib.abc import MetaPathFinder
|
||||
from importlib.machinery import ModuleSpec
|
||||
from types import ModuleType
|
||||
from typing import Iterator, Optional, Sequence
|
||||
|
||||
import pytest
|
||||
|
||||
import manim_slides.manim as msm
|
||||
|
||||
|
||||
@contextmanager
|
||||
def suppress_module_finder() -> Iterator[None]:
|
||||
meta_path = sys.meta_path
|
||||
try:
|
||||
|
||||
class PathFinder(MetaPathFinder):
|
||||
@classmethod
|
||||
def find_spec(
|
||||
cls,
|
||||
fullname: str,
|
||||
path: Optional[Sequence[str]],
|
||||
target: Optional[ModuleType] = None,
|
||||
) -> Optional[ModuleSpec]:
|
||||
if fullname in ["manim", "manimlib"]:
|
||||
return None
|
||||
|
||||
for finder in meta_path:
|
||||
spec = finder.find_spec(fullname, path, target=target)
|
||||
if spec is not None:
|
||||
return spec
|
||||
|
||||
return None
|
||||
|
||||
sys.meta_path = [PathFinder]
|
||||
yield
|
||||
finally:
|
||||
sys.meta_path = meta_path
|
||||
import manim_slides.slide as slide
|
||||
|
||||
|
||||
def assert_import(
|
||||
*,
|
||||
api_name: str,
|
||||
manim: bool,
|
||||
manim_available: bool,
|
||||
manim_imported: bool,
|
||||
manimgl: bool,
|
||||
manimgl_available: bool,
|
||||
manimgl_imported: bool,
|
||||
) -> None:
|
||||
importlib.reload(msm)
|
||||
importlib.reload(slide)
|
||||
|
||||
assert msm.MANIM == manim
|
||||
assert msm.MANIM_AVAILABLE == manim_available
|
||||
assert msm.MANIM_IMPORTED == manim_imported
|
||||
assert msm.MANIMGL == manimgl
|
||||
assert msm.MANIMGL_AVAILABLE == manim_available
|
||||
assert msm.MANIMGL_IMPORTED == manimgl_imported
|
||||
assert slide.API_NAME == api_name
|
||||
assert slide.MANIM == manim
|
||||
assert slide.MANIMGL == manimgl
|
||||
|
||||
|
||||
def test_force_api() -> None:
|
||||
import manim # noqa: F401
|
||||
|
||||
if "manimlib" in sys.modules:
|
||||
del sys.modules["manimlib"]
|
||||
|
||||
os.environ[slide.MANIM_API] = "manimlib"
|
||||
os.environ[slide.FORCE_MANIM_API] = "1"
|
||||
|
||||
assert_import(
|
||||
api_name="manimlib",
|
||||
manim=False,
|
||||
manimgl=True,
|
||||
)
|
||||
|
||||
del os.environ[slide.MANIM_API]
|
||||
del os.environ[slide.FORCE_MANIM_API]
|
||||
|
||||
|
||||
def test_invalid_api() -> None:
|
||||
os.environ[slide.MANIM_API] = "manim_slides"
|
||||
|
||||
with pytest.raises(ImportError):
|
||||
assert_import(
|
||||
api_name="",
|
||||
manim=False,
|
||||
manimgl=False,
|
||||
)
|
||||
|
||||
del os.environ[slide.MANIM_API]
|
||||
|
||||
|
||||
@pytest.mark.filterwarnings("ignore:assert_import")
|
||||
@ -65,12 +58,9 @@ def test_manim_and_manimgl_imported() -> None:
|
||||
import manimlib # noqa: F401
|
||||
|
||||
assert_import(
|
||||
api_name="manim",
|
||||
manim=True,
|
||||
manim_available=True,
|
||||
manim_imported=True,
|
||||
manimgl=False,
|
||||
manimgl_available=True,
|
||||
manimgl_imported=True,
|
||||
)
|
||||
|
||||
|
||||
@ -81,12 +71,9 @@ def test_manim_imported() -> None:
|
||||
del sys.modules["manimlib"]
|
||||
|
||||
assert_import(
|
||||
api_name="manim",
|
||||
manim=True,
|
||||
manim_available=True,
|
||||
manim_imported=True,
|
||||
manimgl=False,
|
||||
manimgl_available=True,
|
||||
manimgl_imported=False,
|
||||
)
|
||||
|
||||
|
||||
@ -97,12 +84,9 @@ def test_manimgl_imported() -> None:
|
||||
del sys.modules["manim"]
|
||||
|
||||
assert_import(
|
||||
api_name="manimlib",
|
||||
manim=False,
|
||||
manim_available=True,
|
||||
manim_imported=False,
|
||||
manimgl=True,
|
||||
manimgl_available=True,
|
||||
manimgl_imported=True,
|
||||
)
|
||||
|
||||
|
||||
@ -114,30 +98,7 @@ def test_nothing_imported() -> None:
|
||||
del sys.modules["manimlib"]
|
||||
|
||||
assert_import(
|
||||
api_name="manim",
|
||||
manim=True,
|
||||
manim_available=True,
|
||||
manim_imported=False,
|
||||
manimgl=False,
|
||||
manimgl_available=True,
|
||||
manimgl_imported=False,
|
||||
)
|
||||
|
||||
|
||||
def test_no_package_available() -> None:
|
||||
with suppress_module_finder():
|
||||
if "manim" in sys.modules:
|
||||
del sys.modules["manim"]
|
||||
|
||||
if "manimlib" in sys.modules:
|
||||
del sys.modules["manimlib"]
|
||||
|
||||
with pytest.raises(ModuleNotFoundError):
|
||||
# Actual values are not important
|
||||
assert_import(
|
||||
manim=False,
|
||||
manim_available=False,
|
||||
manim_imported=False,
|
||||
manimgl=False,
|
||||
manimgl_available=False,
|
||||
manimgl_imported=False,
|
||||
)
|
||||
|
Reference in New Issue
Block a user