Skip to content

Commit d5162f5

Browse files
committed
Imporve cfgwriter.py and changed . to _ as asked 👌
1 parent 4afd1f5 commit d5162f5

File tree

6 files changed

+195
-58
lines changed

6 files changed

+195
-58
lines changed

example_scenes/manim.cfg

Lines changed: 82 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,82 @@
1-
[log.color]
2-
# The colors here are parsed by rich.color , and are not related
3-
# to the colours defined in constants.py's COLOR_MAP
4-
logging.keyword = magenta
5-
logging.level.notset = dim
6-
logging.level.debug = yellow
7-
logging.level.info = dim purple
8-
logging.level.warning = dim red
9-
logging.level.error = red
10-
logging.level.critical = red
11-
log.level =
12-
log.time = dim yellow
13-
log.message = green
14-
log.path = dim blue
1+
[CLI]
2+
write_to_movie = True
3+
save_last_frame = False
4+
write_all = False
5+
save_pngs = False
6+
save_as_gif = False
7+
preview = False
8+
show_file_in_finder = False
9+
quiet = False
10+
sound = False
11+
output_file =
12+
leave_progress_bars = False
13+
background_color = BLACK
14+
background_opacity = 1
15+
from_animation_number = 0
16+
upto_animation_number = -1
17+
media_dir = ./media
18+
png_mode = RGB
19+
movie_file_extension = .mp4
20+
frame_rate = 60
21+
pixel_height = 1440
22+
pixel_width = 2560
23+
24+
[transparent]
25+
png_mode = RGBA
26+
movie_file_extension = .mov
27+
background_opacity = 0
28+
29+
[fourk_quality]
30+
pixel_height = 2160
31+
pixel_width = 3840
32+
frame_rate = 60
33+
34+
[high_quality]
35+
pixel_height = 1080
36+
pixel_width = 1920
37+
frame_rate = 60
38+
39+
[medium_quality]
40+
pixel_height = 720
41+
pixel_width = 1280
42+
frame_rate = 30
43+
44+
[low_quality]
45+
pixel_height = 480
46+
pixel_width = 854
47+
frame_rate = 15
48+
49+
[dry_run]
50+
write_to_movie = False
51+
write_all = False
52+
save_last_frame = False
53+
save_pngs = False
54+
save_as_gif = False
55+
56+
[streaming]
57+
live_stream_name = LiveStream
58+
twitch_stream_key = YOUR_STREAM_KEY
59+
streaming_protocol = tcp
60+
streaming_ip = 127.0.0.1
61+
streaming_port = 2000
62+
streaming_client = ffplay
63+
streaming_url = %(STREAMING_PROTOCOL)s://%(STREAMING_IP)s:%(STREAMING_PORT)s?listen
64+
streaming_console_banner = Manim is now running in streaming mode.
65+
Stream animations by passing them to manim.play(), e.g.
66+
>>> c = Circle()
67+
>>> manim.play(ShowCreation(c))
68+
69+
#This is some custom configuration just as an example
70+
[logger]
71+
logging_keyword = magenta
72+
logging_level_notset = dim
73+
logging_level_debug = yellow
74+
logging_level_info = dim purple
75+
logging_level_warning = dim red
76+
logging_level_error = red
77+
logging_level_critical = red
78+
log_level =
79+
log_time = dim yellow
80+
log_message = green
81+
log_path = dim blue
82+

manim/config.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
import colour
1111

1212
from . import constants
13-
from .utils.config_utils import _run_config, _init_dirs
13+
from .utils.config_utils import _run_config, _init_dirs, _from_command_line
1414

1515
from .logger import logger
1616
from .utils.tex import TexTemplate, TexTemplateFromFile
@@ -90,8 +90,8 @@ def _parse_config(config_parser, args):
9090

9191

9292
args, config_parser, file_writer_config, successfully_read_files = _run_config()
93-
logger.info(f"Read configuration files: {successfully_read_files}")
93+
if _from_command_line():
94+
logger.info(f"Read configuration files: {successfully_read_files}")
95+
_init_dirs(file_writer_config)
9496
config = _parse_config(config_parser, args)
9597
camera_config = config
96-
97-
_init_dirs(file_writer_config)

manim/default.cfg

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,3 +140,16 @@ streaming_console_banner = Manim is now running in streaming mode.
140140
Stream animations by passing them to manim.play(), e.g.
141141
>>> c = Circle()
142142
>>> manim.play(ShowCreation(c))
143+
144+
[logger]
145+
logging_keyword = bold yellow
146+
logging_level_notset = dim
147+
logging_level_debug = green
148+
logging_level_info = blue
149+
logging_level_warning = red
150+
logging_level_error = red bold
151+
logging_level_critical = red bold reverse
152+
log_level =
153+
log_time = cyan dim
154+
log_message =
155+
log_path = dim

manim/logger.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
"""
2+
logger.py
3+
---------
4+
This is the logging library for manim.
5+
This library uses rich for coloured log outputs.
6+
7+
"""
18
import configparser
29
import logging
310

@@ -12,7 +19,13 @@
1219

1320
def parse_theme(fp):
1421
config_parser.read(fp)
15-
theme = dict(config_parser["log.color"])
22+
theme = dict(config_parser["logger"])
23+
# replaces `_` by `.` as rich understands it
24+
for key in theme:
25+
temp = theme[key]
26+
del theme[key]
27+
key = key.replace("_", ".")
28+
theme[key] = temp
1629
try:
1730
customTheme = Theme(theme)
1831
except (color.ColorParseError, errors.StyleSyntaxError):

manim/utils/cfgwriter.py

Lines changed: 53 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -2,54 +2,44 @@
22
cfgwriter.py
33
------------
44
5-
Inputs the configuration files while checking it is valid.
5+
Inputs the configuration files while checking it is valid. Can be executed by `manim-cfg` command.
66
77
"""
8-
from os import path
8+
import os
99
import configparser
1010

11-
from .config_utils import successfully_read_files
11+
from .config_utils import _run_config, _paths_config_file
1212

1313
from rich.console import Console
1414
from rich.progress import track
15-
from rich.color import Color
15+
from rich.style import Style
16+
from rich.errors import StyleSyntaxError
1617

1718

18-
def check_valid_colour(color):
19+
def check_valid_style(style):
1920
"""Checks whether the entered color is a valid color according to rich
2021
Parameters
2122
----------
22-
color : :class:`str`
23-
The color to check whether it is valid.
23+
style : :class:`str`
24+
The style to check whether it is valid.
2425
Returns
2526
-------
2627
Boolean
27-
Returns whether it is valid color or not.
28+
Returns whether it is valid style or not according to rich.
2829
"""
2930
try:
30-
Color.parse(color)
31+
Style.parse(style)
3132
return True
32-
except:
33+
except StyleSyntaxError:
3334
return False
3435

3536

3637
def main():
38+
successfully_read_files = _run_config()[-1]
3739
console = Console()
3840
config = configparser.ConfigParser()
39-
config.read( successfully_read_files )
40-
default = {
41-
"logging.keyword": "bold yellow",
42-
"logging.level.notset": "dim",
43-
"logging.level.debug": "green",
44-
"logging.level.info": "blue",
45-
"logging.level.warning": "red",
46-
"logging.level.error": "red bold",
47-
"logging.level.critical": "red bold reverse",
48-
"log.level": "",
49-
"log.time": "cyan dim",
50-
"log.message": "",
51-
"log.path": "dim",
52-
}
41+
config.read(successfully_read_files)
42+
default = config["logger"]
5343
console.print(
5444
"[yellow bold]Manim Logger Configuration Editor[/yellow bold]", justify="center"
5545
)
@@ -59,27 +49,58 @@ def main():
5949
console.print(
6050
"[magenta]Please follow the link for available styles.[/magenta][link=https://rich.readthedocs.io/en/latest/style.html]docs[/link]"
6151
)
52+
for key in default:
53+
temp = default[key]
54+
del default[key]
55+
key = key.replace("_", ".")
56+
default[key] = temp
6257
for key in default:
6358
console.print("Enter the Style for %s" % key + ":", style=key, end="")
6459
temp = input()
6560
if temp:
66-
while not check_valid_colour(temp):
61+
while not check_valid_style(temp):
6762
console.print(
6863
"[red bold]Your Style is not valid. Try again.[/red bold]"
6964
)
7065
console.print("Enter the Style for %s" % key + ":", style=key, end="")
7166
temp = input()
7267
else:
7368
default[key] = temp
74-
config["log.color"] = default
75-
for n in track(range(100), description="Converting to Manim.cfg"):
76-
with open("manim.cfg", "w") as fp:
77-
config.write(fp)
69+
for key in default:
70+
temp = default[key]
71+
del default[key]
72+
key = key.replace(".", "_")
73+
default[key] = temp
74+
config["logger"] = default
7875
console.print(
79-
"""A configuration file called [yellow]manim.cfg[/yellow] has been created.
80-
To save your theme please save that file and each time place it in your current working directory,
81-
(the directory where you executed the command manim)"""
76+
"Do you want it as a default for the User?(y/n)[[n]]",
77+
style="dim purple",
78+
end="",
8279
)
80+
save_to_userpath = input()
81+
config_paths = _paths_config_file()
82+
if save_to_userpath.lower() == "y":
83+
if not os.path.exists(os.path.abspath(os.path.join(config_paths[1], ".."))):
84+
os.makedirs(os.path.abspath(os.path.join(config_paths[1], "..")))
85+
with open(config_paths[1], "w") as fp:
86+
config.write(fp)
87+
console.print(
88+
"""A configuration file called [yellow]{}[/yellow] has been created with your required changes.
89+
which would be used while running manim command. If you want to overide that
90+
you would have to create a manim.cfg in local directory.""".format(
91+
config_paths[1]
92+
)
93+
)
94+
else:
95+
with open(config_paths[2], "w") as fp:
96+
config.write(fp)
97+
console.print(
98+
"""A configuration file called [yellow]{}[/yellow] has been created.
99+
To save your theme please save that file and each time place it in your current working directory,
100+
(the directory where you executed the command manim)""".format(
101+
config_paths[2]
102+
)
103+
)
83104

84105

85106
if __name__ == "__main__":

manim/utils/config_utils.py

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
from .. import constants
1717
from .tex import TexTemplate, TexTemplateFromFile
1818

19-
__all__ = ["_run_config"]
19+
__all__ = ["_run_config", "_paths_config_file", "_from_command_line"]
2020

2121

2222
def _parse_file_writer_config(config_parser, args):
@@ -345,15 +345,37 @@ def _from_command_line():
345345
return from_cli_command or from_python_m
346346

347347

348-
def _run_config():
349-
# Config files to be parsed, in ascending priority
348+
def _paths_config_file():
350349
library_wide = os.path.abspath(
351350
os.path.join(os.path.dirname(__file__), "..", "default.cfg")
352351
)
353-
config_files = [
354-
library_wide,
355-
os.path.expanduser("~/.manim.cfg"),
356-
]
352+
if sys.platform.startswith("linux"):
353+
# This is for linux users
354+
user_wide = os.path.expanduser(
355+
os.path.join("~", ".config", "Manim", "manim.cfg")
356+
)
357+
elif sys.platform.startswith("darwin"):
358+
# This is for MacOS users
359+
user_wide = os.path.expanduser(
360+
os.path.join("~", "Library", "Application Support", "Manim", "manim.cfg")
361+
)
362+
elif sys.platform.startswith("win32"):
363+
# This is for Windows users
364+
user_wide = os.path.expanduser(
365+
os.path.join("~", "AppData", "Roaming", "Manim", "manim.cfg")
366+
)
367+
else:
368+
# This is for users whose OS is unknown
369+
user_wide = os.path.expanduser(
370+
os.path.join("~", ".config", "Manim", "manim.cfg")
371+
)
372+
current_working_directory = os.path.abspath("manim.cfg")
373+
return [library_wide, user_wide, current_working_directory]
374+
375+
376+
def _run_config():
377+
# Config files to be parsed, in ascending priority
378+
config_files = _paths_config_file()
357379
if _from_command_line():
358380
args = _parse_cli(sys.argv[1:])
359381
if args.config_file is not None:

0 commit comments

Comments
 (0)