Skip to content

armgit / manim

Public Imported from GitHub
0

A community-maintained Python framework for creating mathematical animations.

added new test and fixed a couple of things with deepcopy

leotrs45980e56fcae
diff --git a/manim/config/utils.py b/manim/config/utils.py
index 02bcc209b..fb8acefeb 100644
--- a/manim/config/utils.py
+++ b/manim/config/utils.py
@@ -153,9 +153,11 @@ class ManimConfig(MutableMapping):
"write_to_movie",
}
- def __init__(self, parser):
+ def __init__(self, parser=None):
self._d = {k: None for k in self._OPTS}
- self.digest_parser(parser)
+ self._parser = parser
+ if parser:
+ self.digest_parser(parser)
# behave like a dict
def __iter__(self):
@@ -177,6 +179,21 @@ class ManimConfig(MutableMapping):
def __setitem__(self, key, val):
getattr(ManimConfig, key).fset(self, val) # fset is the property's setter
+ def update(self, obj):
+ if isinstance(obj, ManimConfig):
+ self._d.update(obj._d)
+
+ elif isinstance(obj, dict):
+ # First update the underlying _d, then update other properties
+ _dict = {k: v for k, v in obj.items() if k in self._d}
+ for k, v in _dict.items():
+ self[k] = v
+
+ _dict = {k: v for k, v in obj.items() if k not in self._d}
+ for k, v in _dict.items():
+ self[k] = v
+
+
# don't allow to delete anything
def __delitem__(self, key):
raise AttributeError("'ManimConfig' object does not support item deletion")
@@ -192,58 +209,8 @@ class ManimConfig(MutableMapping):
return copy.deepcopy(self)
def __deepcopy__(self, memo):
- c = ManimConfig(self._parser)
-
- for opt in [
- "background_color",
- "background_opacity",
- "custom_folders",
- "disable_caching",
- "ffmpeg_loglevel",
- "flush_cache",
- "frame_height",
- "frame_rate",
- "frame_width",
- "from_animation_number",
- "js_renderer_path",
- "leave_progress_bars",
- "log_to_file",
- "max_files_cached",
- "movie_file_extension",
- "pixel_height",
- "pixel_width",
- "png_mode",
- "preview",
- "progress_bar",
- "save_as_gif",
- "save_last_frame",
- "save_pngs",
- "scene_names",
- "show_in_file_browser",
- "skip_animations",
- "sound",
- "tex_template_file",
- "upto_animation_number",
- "use_js_renderer",
- "verbosity",
- "write_all",
- "write_to_movie",
- ]:
- setattr(c, opt, getattr(self, opt))
-
- # setattr() on the following options actually returns something
- # different to their stored value, so we need to get the actual value
- for opt in [
- "media_dir",
- "input_file",
- "images_dir",
- "log_dir",
- "tex_dir",
- "text_dir",
- "video_dir",
- ]:
- c._d[opt] = self._d[opt]
-
+ c = ManimConfig()
+ c._d = copy.deepcopy(self._d, memo)
return c
# helper type-checking methods
@@ -450,11 +417,11 @@ class ManimConfig(MutableMapping):
return self
def digest_dict(self, _dict):
- return self
+ pass
def digest_file(self, filename):
if filename:
- self.digest_parser(make_config_parser(filename))
+ return self.digest_parser(make_config_parser(filename))
# config options are properties
preview = property(
diff --git a/manim/scene/scene_file_writer.py b/manim/scene/scene_file_writer.py
index b177dd2c9..874cd7fdd 100644
--- a/manim/scene/scene_file_writer.py
+++ b/manim/scene/scene_file_writer.py
@@ -59,6 +59,9 @@ class SceneFileWriter(object):
files will be written to and read from (within MEDIA_DIR).
If they don't already exist, they will be created.
"""
+ if config['dry_run']:
+ return
+
module_directory = self.get_default_module_directory()
default_name = self.get_default_scene_name(scene_name)
if config["save_last_frame"] or config["save_pngs"]:
diff --git a/tests/test_config.py b/tests/test_config.py
index a7c8437a3..c58f197db 100644
--- a/tests/test_config.py
+++ b/tests/test_config.py
@@ -37,12 +37,13 @@ def test_transparent():
orig_verbosity = config["verbosity"]
config["verbosity"] = "ERROR"
- scene = MyScene()
- scene.render()
- frame = scene.renderer.get_frame()
+ with tempconfig({'dry_run': True}):
+ scene = MyScene()
+ scene.render()
+ frame = scene.renderer.get_frame()
assert np.allclose(frame[0, 0], [0, 0, 0, 255])
- with tempconfig({"transparent": True}):
+ with tempconfig({"transparent": True, 'dry_run': True}):
scene = MyScene()
scene.render()
frame = scene.renderer.get_frame()
@@ -53,8 +54,43 @@ def test_transparent():
def test_background_color():
"""Test the 'background_color' config option."""
- with tempconfig({"background_color": WHITE, "verbosity": "ERROR"}):
+ with tempconfig({"background_color": WHITE, "verbosity": "ERROR", "dry_run": True}):
scene = MyScene()
scene.render()
frame = scene.renderer.get_frame()
assert np.allclose(frame[0, 0], [255, 255, 255, 255])
+
+
+def test_digest_file():
+ """Test that a config file can be digested programatically."""
+ assert config['media_dir'] == Path('media')
+ assert config['video_dir'] == Path('media/videos')
+
+ with tempconfig({}):
+ temp_cfg = tempfile.NamedTemporaryFile()
+ with open(temp_cfg.name, 'w') as file:
+ file.write("""
+ [CLI]
+ media_dir = this_is_my_favorite_path
+ video_dir = {media_dir}/videos
+ """
+ )
+ config.digest_file(temp_cfg.name)
+ assert config['media_dir'] == Path('this_is_my_favorite_path')
+ assert config['video_dir'] == Path('this_is_my_favorite_path/videos')
+
+ assert config['media_dir'] == Path('media')
+ assert config['video_dir'] == Path('media/videos')
+
+
+def test_temporary_dry_run():
+ """Test that tempconfig correctly restores after setting dry_run."""
+ assert config["write_to_movie"]
+ assert not config["save_last_frame"]
+
+ with tempconfig({"dry_run": True}):
+ assert not config["write_to_movie"]
+ assert not config["save_last_frame"]
+
+ assert config["write_to_movie"]
+ assert not config["save_last_frame"]
diff --git a/tests/test_container.py b/tests/test_container.py
index 5a26b3ba5..c49979981 100644
--- a/tests/test_container.py
+++ b/tests/test_container.py
@@ -1,5 +1,5 @@
import pytest
-from manim import Container, Mobject, Scene
+from manim import Container, Mobject, Scene, tempconfig
def test_ABC():
@@ -9,7 +9,9 @@ def test_ABC():
# The following should work without raising exceptions
Mobject()
- Scene()
+
+ with tempconfig({'dry_run': True}):
+ Scene()
def container_add(obj, get_submobjects):
@@ -63,13 +65,10 @@ def test_mobject_remove():
container_remove(obj, lambda: obj.submobjects)
-def test_scene_add():
- """Test Scene.add()."""
- scene = Scene()
- container_add(scene, lambda: scene.mobjects)
-
-
-def test_scene_remove():
- """Test Scene.remove()."""
- scene = Scene()
- container_remove(scene, lambda: scene.mobjects)
+def test_scene_add_remove():
+ """Test Scene.add() and Scene.remove()."""
+ with tempconfig({'dry_run': True}):
+ scene = Scene()
+ container_add(scene, lambda: scene.mobjects)
+ scene = Scene()
+ container_remove(scene, lambda: scene.mobjects)
diff --git a/tests/utils/GraphicalUnitTester.py b/tests/utils/GraphicalUnitTester.py
index 9c7682c52..8b46bffb5 100644
--- a/tests/utils/GraphicalUnitTester.py
+++ b/tests/utils/GraphicalUnitTester.py
@@ -2,7 +2,7 @@ import os
import logging
import numpy as np
-from manim import config
+from manim import config, tempconfig
class GraphicalUnitTester:
@@ -54,8 +54,6 @@ class GraphicalUnitTester:
config["tex_dir"] = os.path.join(self.path_tests_medias_cache, "Tex")
config["skip_animations"] = True
- config["save_last_frame"] = True
- config["write_to_movie"] = False
config["disable_caching"] = True
config["quality"] = "low_quality"
@@ -66,8 +64,9 @@ class GraphicalUnitTester:
]:
os.makedirs(dir_temp)
- self.scene = scene_object()
- self.scene.render()
+ with tempconfig({"dry_run": True}):
+ self.scene = scene_object()
+ self.scene.render()
def _load_data(self):
"""Load the np.array of the last frame of a pre-rendered scene. If not found, throw FileNotFoundError.