Skip to content

armgit / manim

Public Imported from GitHub
0

A community-maintained Python framework for creating mathematical animations.

prefer Path to raw strings

leotrs58705ab514e5
diff --git a/manim/config/utils.py b/manim/config/utils.py
index fb8acefeb..9d1111ab5 100644
--- a/manim/config/utils.py
+++ b/manim/config/utils.py
@@ -743,13 +743,13 @@ class ManimConfig(MutableMapping):
js_renderer_path = property(
lambda self: self._d["js_renderer_path"],
- lambda self, val: self._d.__setitem__("js_renderer_path", Path(val)),
+ lambda self, val: self._d.__setitem__("js_renderer_path", val),
doc="Path to JS renderer.",
)
media_dir = property(
- lambda self: self._d["media_dir"],
- lambda self, val: self._d.__setitem__("media_dir", Path(val)),
+ lambda self: Path(self._d["media_dir"]),
+ lambda self, val: self._d.__setitem__("media_dir", val),
doc="Main output directory, relative to execution directory.",
)
@@ -766,35 +766,42 @@ class ManimConfig(MutableMapping):
]
dirs.remove(key)
dirs = {k: self._d[k] for k in dirs}
- return self._d[key].format(**dirs)
+ path = self._d[key].format(**dirs)
+ return Path(path) if path else None
+
+ def _set_dir(self, key, val):
+ if isinstance(val, Path):
+ self._d.__setitem__(key, str(val))
+ else:
+ self._d.__setitem__(key, val)
log_dir = property(
lambda self: self._get_dir("log_dir"),
- lambda self, val: self._d.__setitem__("log_dir", val),
+ lambda self, val: self._set_dir("log_dir", val),
doc="Directory to place logs",
)
video_dir = property(
lambda self: self._get_dir("video_dir"),
- lambda self, val: self._d.__setitem__("video_dir", val),
+ lambda self, val: self._set_dir("video_dir", val),
doc="Directory to place videos",
)
images_dir = property(
lambda self: self._get_dir("images_dir"),
- lambda self, val: self._d.__setitem__("images_dir", val),
+ lambda self, val: self._set_dir("images_dir", val),
doc="Directory to place images",
)
text_dir = property(
lambda self: self._get_dir("text_dir"),
- lambda self, val: self._d.__setitem__("text_dir", val),
+ lambda self, val: self._set_dir("text_dir", val),
doc="Directory to place text",
)
tex_dir = property(
lambda self: self._get_dir("tex_dir"),
- lambda self, val: self._d.__setitem__("tex_dir", val),
+ lambda self, val: self._set_dir("tex_dir", val),
doc="Directory to place tex",
)
@@ -806,13 +813,13 @@ class ManimConfig(MutableMapping):
input_file = property(
lambda self: self._get_dir("input_file"),
- lambda self, val: self._d.__setitem__("input_file", val),
+ lambda self, val: self._set_dir("input_file", val),
doc="Input file name.",
)
output_file = property(
lambda self: self._get_dir("output_file"),
- lambda self, val: self._d.__setitem__("output_file", val),
+ lambda self, val: self._set_dir("output_file", val),
doc="Output file name.",
)
diff --git a/manim/scene/scene_file_writer.py b/manim/scene/scene_file_writer.py
index 874cd7fdd..2b4cd42bf 100644
--- a/manim/scene/scene_file_writer.py
+++ b/manim/scene/scene_file_writer.py
@@ -12,6 +12,7 @@ import _thread as thread
from time import sleep
import datetime
from PIL import Image
+from pathlib import Path
from .. import config, logger, console
from ..constants import FFMPEG_BIN, GIF_FILE_EXTENSION
@@ -98,10 +99,11 @@ class SceneFileWriter(object):
default_name, config["movie_file_extension"]
),
)
- self.gif_file_path = os.path.join(
- movie_dir,
- add_extension_if_not_present(default_name, GIF_FILE_EXTENSION),
- )
+ if config['save_as_gif']:
+ self.gif_file_path = os.path.join(
+ movie_dir,
+ add_extension_if_not_present(default_name, GIF_FILE_EXTENSION),
+ )
if not config["custom_folders"]:
self.partial_movie_directory = guarantee_existence(
os.path.join(
@@ -172,7 +174,7 @@ class SceneFileWriter(object):
The default scene name.
"""
fn = config["output_file"]
- return fn if fn else scene_name
+ return fn if fn else Path(scene_name)
def get_resolution_directory(self):
"""Get the name of the resolution directory directly containing
diff --git a/manim/utils/file_ops.py b/manim/utils/file_ops.py
index 856c59941..b79e6c316 100644
--- a/manim/utils/file_ops.py
+++ b/manim/utils/file_ops.py
@@ -11,16 +11,14 @@ __all__ = [
import os
import platform
-import numpy as np
import time
-import re
import subprocess as sp
+from pathlib import Path
def add_extension_if_not_present(file_name, extension):
- # This could conceivably be smarter about handling existing differing extensions
- if file_name[-len(extension) :] != extension:
- return file_name + extension
+ if file_name.suffix != extension:
+ return file_name.with_suffix(extension)
else:
return file_name
diff --git a/manim/utils/module_ops.py b/manim/utils/module_ops.py
index 455e3ee45..1ef96befb 100644
--- a/manim/utils/module_ops.py
+++ b/manim/utils/module_ops.py
@@ -28,9 +28,10 @@ def get_module(file_name):
sys.exit(2)
else:
if os.path.exists(file_name):
- if file_name[-3:] != ".py":
+ ext = file_name.suffix
+ if ext != ".py":
raise ValueError(f"{file_name} is not a valid Manim python script.")
- module_name = file_name[:-3].replace(os.sep, ".").split(".")[-1]
+ module_name = ext.replace(os.sep, ".").split(".")[-1]
spec = importlib.util.spec_from_file_location(module_name, file_name)
module = importlib.util.module_from_spec(spec)
sys.modules[module_name] = module
diff --git a/tests/test_config.py b/tests/test_config.py
index c58f197db..946055cee 100644
--- a/tests/test_config.py
+++ b/tests/test_config.py
@@ -1,5 +1,7 @@
-import pytest
+import tempfile
+from pathlib import Path
import numpy as np
+
from manim import config, tempconfig, Scene, Square, WHITE