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

chore(lib): reduce import overhead #147

Merged
merged 6 commits into from
Mar 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 46 additions & 1 deletion manim_slides/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,48 @@
# flake8: noqa: F401
import sys
from types import ModuleType
from typing import Any, List

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"),
}
)
5 changes: 3 additions & 2 deletions manim_slides/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
import requests
from click_default_group import DefaultGroup

from . import __version__
from .__version__ import __version__
from .convert import convert
from .manim import logger
from .logger import make_logger
from .present import list_scenes, present
from .wizard import init, wizard

Expand All @@ -27,6 +27,7 @@ def cli(notify_outdated_version: bool) -> None:

If no command is specified, defaults to `present`.
"""
logger = make_logger()
# Code below is mostly a copy from:
# https://github.com/ManimCommunity/manim/blob/main/manim/cli/render/commands.py
if notify_outdated_version:
Expand Down
2 changes: 1 addition & 1 deletion manim_slides/commons.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from click import Context, Parameter

from .defaults import CONFIG_PATH, FOLDER_PATH
from .manim import logger
from .logger import logger

F = Callable[..., Any]
Wrapper = Callable[[F], F]
Expand Down
3 changes: 2 additions & 1 deletion manim_slides/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
from pydantic import BaseModel, FilePath, root_validator, validator
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:
Expand Down
1 change: 1 addition & 0 deletions manim_slides/defaults.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
FOLDER_PATH: str = "./slides"
CONFIG_PATH: str = ".manim-slides.json"
FFMPEG_BIN: str = "ffmpeg"
42 changes: 42 additions & 0 deletions manim_slides/logger.py
Original file line number Diff line number Diff line change
@@ -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")
2 changes: 1 addition & 1 deletion manim_slides/present.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
from .commons import config_path_option, verbosity_option
from .config import DEFAULT_CONFIG, Config, PresentationConfig, SlideConfig
from .defaults import FOLDER_PATH
from .manim import logger
from .logger import logger
from .resources import * # noqa: F401, F403

os.environ.pop(
Expand Down
2 changes: 1 addition & 1 deletion manim_slides/wizard.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
from .commons import config_options, verbosity_option
from .config import Config, Key
from .defaults import CONFIG_PATH
from .manim import logger
from .logger import logger
from .resources import * # noqa: F401, F403

WINDOW_NAME: str = "Configuration Wizard"
Expand Down
2,702 changes: 1,359 additions & 1,343 deletions poetry.lock

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ pydantic = "^1.10.2"
pyside6 = "^6.4.1"
python = ">=3.8.1,<3.12"
requests = "^2.28.1"
rich = "^13.3.2"
tqdm = "^4.64.1"

[tool.poetry.group.dev.dependencies]
Expand Down