mirror of
https://github.com/jeertmans/manim-slides.git
synced 2025-05-21 20:46:01 +08:00
chore(lib): reduce import overhead (#147)
* chore(lib): reduce import overhead This PR should reduce the import time overhead caused by manim imports. To solve this, manim is only imported when Slide or ThreeDSlide is needed. A custom logger is now defined, which copies the one from Manim Community. FFMPEG_BIN is now hardcoded, but should be configurable in the future via the CLI or some config file. * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * fix(lib): remove last .manim import * fix(lib): remove print * chore(lib): fix typo --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
This commit is contained in:
@ -1,3 +1,48 @@
|
|||||||
# flake8: noqa: F401
|
# flake8: noqa: F401
|
||||||
|
import sys
|
||||||
|
from types import ModuleType
|
||||||
|
from typing import Any, List
|
||||||
|
|
||||||
from .__version__ import __version__
|
from .__version__ import __version__
|
||||||
from .slide import Slide, ThreeDSlide
|
|
||||||
|
|
||||||
|
class module(ModuleType):
|
||||||
|
def __getattr__(self, name: str) -> Any:
|
||||||
|
if name == "Slide" or name == "ThreeDSlide":
|
||||||
|
module = __import__(
|
||||||
|
"manim_slides.slide", None, None, ["Slide", "ThreeDSlide"]
|
||||||
|
)
|
||||||
|
return getattr(module, name)
|
||||||
|
|
||||||
|
return ModuleType.__getattribute__(self, name)
|
||||||
|
|
||||||
|
def __dir__(self) -> List[str]:
|
||||||
|
result = list(new_module.__all__)
|
||||||
|
result.extend(
|
||||||
|
(
|
||||||
|
"__file__",
|
||||||
|
"__doc__",
|
||||||
|
"__all__",
|
||||||
|
"__docformat__",
|
||||||
|
"__name__",
|
||||||
|
"__path__",
|
||||||
|
"__package__",
|
||||||
|
"__version__",
|
||||||
|
)
|
||||||
|
)
|
||||||
|
return result
|
||||||
|
|
||||||
|
|
||||||
|
old_module = sys.modules["manim_slides"]
|
||||||
|
new_module = sys.modules["manim_slides"] = module("manim_slides")
|
||||||
|
|
||||||
|
new_module.__dict__.update(
|
||||||
|
{
|
||||||
|
"__file__": __file__,
|
||||||
|
"__package__": "manim_slides",
|
||||||
|
"__path__": __path__,
|
||||||
|
"__doc__": __doc__,
|
||||||
|
"__version__": __version__,
|
||||||
|
"__all__": ("__version__", "Slides", "ThreeDSlide"),
|
||||||
|
}
|
||||||
|
)
|
||||||
|
@ -4,9 +4,9 @@ import click
|
|||||||
import requests
|
import requests
|
||||||
from click_default_group import DefaultGroup
|
from click_default_group import DefaultGroup
|
||||||
|
|
||||||
from . import __version__
|
from .__version__ import __version__
|
||||||
from .convert import convert
|
from .convert import convert
|
||||||
from .manim import logger
|
from .logger import make_logger
|
||||||
from .present import list_scenes, present
|
from .present import list_scenes, present
|
||||||
from .wizard import init, wizard
|
from .wizard import init, wizard
|
||||||
|
|
||||||
@ -27,6 +27,7 @@ def cli(notify_outdated_version: bool) -> None:
|
|||||||
|
|
||||||
If no command is specified, defaults to `present`.
|
If no command is specified, defaults to `present`.
|
||||||
"""
|
"""
|
||||||
|
logger = make_logger()
|
||||||
# Code below is mostly a copy from:
|
# Code below is mostly a copy from:
|
||||||
# https://github.com/ManimCommunity/manim/blob/main/manim/cli/render/commands.py
|
# https://github.com/ManimCommunity/manim/blob/main/manim/cli/render/commands.py
|
||||||
if notify_outdated_version:
|
if notify_outdated_version:
|
||||||
|
@ -5,7 +5,7 @@ import click
|
|||||||
from click import Context, Parameter
|
from click import Context, Parameter
|
||||||
|
|
||||||
from .defaults import CONFIG_PATH, FOLDER_PATH
|
from .defaults import CONFIG_PATH, FOLDER_PATH
|
||||||
from .manim import logger
|
from .logger import logger
|
||||||
|
|
||||||
F = Callable[..., Any]
|
F = Callable[..., Any]
|
||||||
Wrapper = Callable[[F], F]
|
Wrapper = Callable[[F], F]
|
||||||
|
@ -10,7 +10,8 @@ from typing import Dict, List, Optional, Set, Union
|
|||||||
from pydantic import BaseModel, FilePath, root_validator, validator
|
from pydantic import BaseModel, FilePath, root_validator, validator
|
||||||
from PySide6.QtCore import Qt
|
from PySide6.QtCore import Qt
|
||||||
|
|
||||||
from .manim import FFMPEG_BIN, logger
|
from .defaults import FFMPEG_BIN
|
||||||
|
from .logger import logger
|
||||||
|
|
||||||
|
|
||||||
def merge_basenames(files: List[FilePath]) -> Path:
|
def merge_basenames(files: List[FilePath]) -> Path:
|
||||||
|
@ -1,2 +1,3 @@
|
|||||||
FOLDER_PATH: str = "./slides"
|
FOLDER_PATH: str = "./slides"
|
||||||
CONFIG_PATH: str = ".manim-slides.json"
|
CONFIG_PATH: str = ".manim-slides.json"
|
||||||
|
FFMPEG_BIN: str = "ffmpeg"
|
||||||
|
42
manim_slides/logger.py
Normal file
42
manim_slides/logger.py
Normal file
@ -0,0 +1,42 @@
|
|||||||
|
"""
|
||||||
|
Logger utils, mostly copied from Manim Community:
|
||||||
|
https://github.com/ManimCommunity/manim/blob/d5b65b844b8ce8ff5151a2f56f9dc98cebbc1db4/manim/_config/logger_utils.py#L29-L101
|
||||||
|
"""
|
||||||
|
|
||||||
|
import logging
|
||||||
|
|
||||||
|
from rich.logging import RichHandler
|
||||||
|
|
||||||
|
__all__ = ["logger", "make_logger"]
|
||||||
|
|
||||||
|
HIGHLIGHTED_KEYWORDS = [ # these keywords are highlighted specially
|
||||||
|
"Played",
|
||||||
|
"animations",
|
||||||
|
"scene",
|
||||||
|
"Reading",
|
||||||
|
"Writing",
|
||||||
|
"script",
|
||||||
|
"arguments",
|
||||||
|
"Invalid",
|
||||||
|
"Aborting",
|
||||||
|
"module",
|
||||||
|
"File",
|
||||||
|
"Rendering",
|
||||||
|
"Rendered",
|
||||||
|
]
|
||||||
|
|
||||||
|
|
||||||
|
def make_logger() -> logging.Logger:
|
||||||
|
"""
|
||||||
|
Make a logger similar to the one used by Manim.
|
||||||
|
"""
|
||||||
|
RichHandler.KEYWORDS = HIGHLIGHTED_KEYWORDS
|
||||||
|
rich_handler = RichHandler(
|
||||||
|
show_time=True,
|
||||||
|
)
|
||||||
|
logger = logging.getLogger("manim-slides")
|
||||||
|
logger.addHandler(rich_handler)
|
||||||
|
return logger
|
||||||
|
|
||||||
|
|
||||||
|
logger = logging.getLogger("manim-slides")
|
@ -18,7 +18,7 @@ from tqdm import tqdm
|
|||||||
from .commons import config_path_option, verbosity_option
|
from .commons import config_path_option, verbosity_option
|
||||||
from .config import DEFAULT_CONFIG, Config, PresentationConfig, SlideConfig
|
from .config import DEFAULT_CONFIG, Config, PresentationConfig, SlideConfig
|
||||||
from .defaults import FOLDER_PATH
|
from .defaults import FOLDER_PATH
|
||||||
from .manim import logger
|
from .logger import logger
|
||||||
from .resources import * # noqa: F401, F403
|
from .resources import * # noqa: F401, F403
|
||||||
|
|
||||||
os.environ.pop(
|
os.environ.pop(
|
||||||
|
@ -21,7 +21,7 @@ from PySide6.QtWidgets import (
|
|||||||
from .commons import config_options, verbosity_option
|
from .commons import config_options, verbosity_option
|
||||||
from .config import Config, Key
|
from .config import Config, Key
|
||||||
from .defaults import CONFIG_PATH
|
from .defaults import CONFIG_PATH
|
||||||
from .manim import logger
|
from .logger import logger
|
||||||
from .resources import * # noqa: F401, F403
|
from .resources import * # noqa: F401, F403
|
||||||
|
|
||||||
WINDOW_NAME: str = "Configuration Wizard"
|
WINDOW_NAME: str = "Configuration Wizard"
|
||||||
|
3130
poetry.lock
generated
3130
poetry.lock
generated
File diff suppressed because it is too large
Load Diff
@ -72,6 +72,7 @@ pydantic = "^1.10.2"
|
|||||||
pyside6 = "^6.4.1"
|
pyside6 = "^6.4.1"
|
||||||
python = ">=3.8.1,<3.12"
|
python = ">=3.8.1,<3.12"
|
||||||
requests = "^2.28.1"
|
requests = "^2.28.1"
|
||||||
|
rich = "^13.3.2"
|
||||||
tqdm = "^4.64.1"
|
tqdm = "^4.64.1"
|
||||||
|
|
||||||
[tool.poetry.group.dev.dependencies]
|
[tool.poetry.group.dev.dependencies]
|
||||||
|
Reference in New Issue
Block a user