Skip to content

Commit b27a416

Browse files
committed
Revert "Use file_writer_config instead of config to get text_dir for Text() (ManimCommunity#220)"
This reverts commit 7f28769.
1 parent d497e1c commit b27a416

File tree

10 files changed

+173
-413
lines changed

10 files changed

+173
-413
lines changed

manim/__main__.py

Lines changed: 19 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@
88
import importlib.util
99
import types
1010

11-
from .config import file_writer_config,args
12-
from .utils import cfg_subcmds
11+
from .config import file_writer_config
1312
from .scene.scene import Scene
1413
from .utils.sounds import play_error_sound
1514
from .utils.sounds import play_finish_sound
@@ -128,7 +127,7 @@ def get_module(file_name):
128127
logger.info("Enter the animation's code & end with an EOF (CTRL+D on Linux/Unix, CTRL+Z on Windows):")
129128
code = sys.stdin.read()
130129
if not code.startswith("from manim import"):
131-
logger.warning("Didn't find an import statement for Manim. Importing automatically...")
130+
logger.warn("Didn't find an import statement for Manim. Importing automatically...")
132131
code="from manim import *\n"+code
133132
logger.info("Rendering animation from typed code...")
134133
try:
@@ -151,38 +150,23 @@ def get_module(file_name):
151150

152151

153152
def main():
154-
if hasattr(args,"subcommands"):
155-
if "cfg" in args.subcommands:
156-
if args.cfg_subcommand is not None:
157-
subcommand=args.cfg_subcommand
158-
if subcommand == "write":
159-
cfg_subcmds.write(args.level,args.open)
160-
elif subcommand == "show":
161-
cfg_subcmds.show()
162-
elif subcommand == "export":
163-
cfg_subcmds.export(args.dir)
164-
else:
165-
logger.error("No argument provided; Exiting...")
166-
167-
168-
else:
169-
module = get_module(file_writer_config["input_file"])
170-
all_scene_classes = get_scene_classes_from_module(module)
171-
scene_classes_to_render = get_scenes_to_render(all_scene_classes)
172-
sound_on = file_writer_config["sound"]
173-
for SceneClass in scene_classes_to_render:
174-
try:
175-
# By invoking, this renders the full scene
176-
scene = SceneClass()
177-
open_file_if_needed(scene.file_writer)
178-
if sound_on:
179-
play_finish_sound()
180-
except Exception:
181-
print("\n\n")
182-
traceback.print_exc()
183-
print("\n\n")
184-
if sound_on:
185-
play_error_sound()
153+
module = get_module(file_writer_config["input_file"])
154+
all_scene_classes = get_scene_classes_from_module(module)
155+
scene_classes_to_render = get_scenes_to_render(all_scene_classes)
156+
sound_on = file_writer_config["sound"]
157+
for SceneClass in scene_classes_to_render:
158+
try:
159+
# By invoking, this renders the full scene
160+
scene = SceneClass()
161+
open_file_if_needed(scene.file_writer)
162+
if sound_on:
163+
play_finish_sound()
164+
except Exception:
165+
print("\n\n")
166+
traceback.print_exc()
167+
print("\n\n")
168+
if sound_on:
169+
play_error_sound()
186170

187171

188172
if __name__ == "__main__":

manim/config.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,7 @@ def _parse_config(config_parser, args):
8989

9090
args, config_parser, file_writer_config, successfully_read_files = _run_config()
9191
if _from_command_line():
92-
logger.info(f"Read configuration files: {[os.path.abspath(cfgfile) for cfgfile in successfully_read_files]}")
93-
if not(hasattr(args,"subcommands")):
94-
_init_dirs(file_writer_config)
92+
logger.info(f"Read configuration files: {os.path.abspath(successfully_read_files[-1])}")
93+
_init_dirs(file_writer_config)
9594
config = _parse_config(config_parser, args)
9695
camera_config = config

manim/utils/cfg_subcmds.py

Lines changed: 0 additions & 187 deletions
This file was deleted.

manim/utils/cfgwriter.py

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
"""
2+
cfgwriter.py
3+
------------
4+
5+
Inputs the configuration files while checking it is valid. Can be executed by `manim-cfg` command.
6+
7+
"""
8+
import os
9+
import configparser
10+
11+
from .config_utils import _run_config, _paths_config_file
12+
13+
from rich.console import Console
14+
from rich.progress import track
15+
from rich.style import Style
16+
from rich.errors import StyleSyntaxError
17+
18+
__all__ = ["main"]
19+
20+
INVALID_STYLE_MSG = "[red bold]Your Style is not valid. Try again.[/red bold]"
21+
INTRO_INSTRUCTIONS = """[red]The default colour is used by the input statement.
22+
If left empty, the default colour will be used.[/red]
23+
[magenta] For a full list of styles, visit[/magenta] https://rich.readthedocs.io/en/latest/style.html"""
24+
TITLE_TEXT = "[yellow bold]Manim Configuration File Writer[/yellow bold]"
25+
26+
27+
def is_valid_style(style):
28+
"""Checks whether the entered color is a valid color according to rich
29+
Parameters
30+
----------
31+
style : :class:`str`
32+
The style to check whether it is valid.
33+
Returns
34+
-------
35+
Boolean
36+
Returns whether it is valid style or not according to rich.
37+
"""
38+
try:
39+
Style.parse(style)
40+
return True
41+
except StyleSyntaxError:
42+
return False
43+
44+
45+
def replace_keys(default):
46+
"""Replaces _ to . and viceversa in a dictionary for rich
47+
Parameters
48+
----------
49+
default : :class:`dict`
50+
The dictionary to check and replace
51+
Returns
52+
-------
53+
:class:`dict`
54+
The dictionary which is modified by replcaing _ with . and viceversa
55+
"""
56+
for key in default:
57+
if "_" in key:
58+
temp = default[key]
59+
del default[key]
60+
key = key.replace("_", ".")
61+
default[key] = temp
62+
else:
63+
temp = default[key]
64+
del default[key]
65+
key = key.replace(".", "_")
66+
default[key] = temp
67+
return default
68+
69+
70+
def main():
71+
config = _run_config()[1]
72+
console = Console()
73+
default = config["logger"]
74+
console.print(TITLE_TEXT, justify="center")
75+
console.print(INTRO_INSTRUCTIONS)
76+
default = replace_keys(default)
77+
for key in default:
78+
console.print("Enter the Style for %s" % key + ":", style=key, end="")
79+
temp = input()
80+
if temp:
81+
while not is_valid_style(temp):
82+
console.print(INVALID_STYLE_MSG)
83+
console.print("Enter the Style for %s" % key + ":", style=key, end="")
84+
temp = input()
85+
else:
86+
default[key] = temp
87+
default = replace_keys(default)
88+
config["logger"] = default
89+
console.print(
90+
"Do you want to save this as the default for this User?(y/n)[[n]]",
91+
style="dim purple",
92+
end="",
93+
)
94+
save_to_userpath = input()
95+
config_paths = _paths_config_file() + [os.path.abspath("manim.cfg")]
96+
if save_to_userpath.lower() == "y":
97+
if not os.path.exists(os.path.abspath(os.path.join(config_paths[1], ".."))):
98+
os.makedirs(os.path.abspath(os.path.join(config_paths[1], "..")))
99+
with open(config_paths[1], "w") as fp:
100+
config.write(fp)
101+
console.print(
102+
f"""A configuration file called [yellow]{config_paths[1]}[/yellow] has been created with your required changes.
103+
This will be used when running the manim command. If you want to override this config,
104+
you will have to create a manim.cfg in the local directory, where you want those changes to be overridden."""
105+
)
106+
else:
107+
with open(config_paths[2], "w") as fp:
108+
config.write(fp)
109+
console.print(
110+
f"""A configuration file called [yellow]{config_paths[2]}[/yellow] has been created.
111+
To save your theme please save that file and place it in your current working directory, from where you run the manim command."""
112+
)
113+
114+
115+
if __name__ == "__main__":
116+
main()

0 commit comments

Comments
 (0)