11
11
import contextlib
12
12
from ast import literal_eval
13
13
from pathlib import Path
14
+ from typing import Any , cast
14
15
15
16
import cloup
16
17
from rich .errors import StyleSyntaxError
17
18
from rich .style import Style
18
19
19
- from ... import cli_ctx_settings , console
20
- from .. ._config .utils import config_file_paths , make_config_parser
21
- from .. .constants import EPILOG
22
- from .. .utils .file_ops import guarantee_existence , open_file
20
+ from manim . _config import cli_ctx_settings , console
21
+ from manim ._config .utils import config_file_paths , make_config_parser
22
+ from manim .constants import EPILOG
23
+ from manim .utils .file_ops import guarantee_existence , open_file
23
24
24
25
RICH_COLOUR_INSTRUCTIONS : str = """
25
26
[red]The default colour is used by the input statement.
26
27
If left empty, the default colour will be used.[/red]
27
28
[magenta] For a full list of styles, visit[/magenta] [green]https://rich.readthedocs.io/en/latest/style.html[/green]
28
29
"""
29
- RICH_NON_STYLE_ENTRIES : str = ["log.width" , "log.height" , "log.timestamps" ]
30
+ RICH_NON_STYLE_ENTRIES : list [ str ] = ["log.width" , "log.height" , "log.timestamps" ]
30
31
31
32
__all__ = [
32
33
"value_from_string" ,
41
42
42
43
43
44
def value_from_string (value : str ) -> str | int | bool :
44
- """Extracts the literal of proper datatype from a string.
45
+ """Extract the literal of proper datatype from a ``value`` string.
46
+
45
47
Parameters
46
48
----------
47
49
value
48
50
The value to check get the literal from.
49
51
50
52
Returns
51
53
-------
52
- Union[ :class:`str`, :class:`int`, :class:`bool`]
53
- Returns the literal of appropriate datatype.
54
+ :class:`str` | :class:`int` | :class:`bool`
55
+ The literal of appropriate datatype.
54
56
"""
55
57
with contextlib .suppress (SyntaxError , ValueError ):
56
58
value = literal_eval (value )
57
59
return value
58
60
59
61
60
- def _is_expected_datatype (value : str , expected : str , style : bool = False ) -> bool :
61
- """Checks whether `value` is the same datatype as `expected`,
62
- and checks if it is a valid `style` if `style` is true.
62
+ def _is_expected_datatype (
63
+ value : str , expected : str , validate_style : bool = False
64
+ ) -> bool :
65
+ """Check whether the literal from ``value`` is the same datatype as the
66
+ literal from ``expected``. If ``validate_style`` is ``True``, also check if
67
+ the style given by ``value`` is valid, according to ``rich``.
63
68
64
69
Parameters
65
70
----------
66
71
value
67
- The string of the value to check ( obtained from reading the user input) .
72
+ The string of the value to check, obtained from reading the user input.
68
73
expected
69
- The string of the literal datatype must be matched by `value`. Obtained from
70
- reading the cfg file.
71
- style
72
- Whether or not to confirm if `value` is a style, by default False
74
+ The string of the literal datatype which must be matched by ``value``.
75
+ This is obtained from reading the ``cfg`` file.
76
+ validate_style
77
+ Whether or not to confirm if ``value`` is a valid style, according to
78
+ ``rich``. Default is ``False``.
73
79
74
80
Returns
75
81
-------
76
82
:class:`bool`
77
- Whether or not `value` matches the datatype of `expected`.
83
+ Whether or not the literal from ``value`` matches the datatype of the
84
+ literal from ``expected``.
78
85
"""
79
- value = value_from_string (value )
80
- expected = type (value_from_string (expected ))
86
+ value_literal = value_from_string (value )
87
+ ExpectedLiteralType = type (value_from_string (expected ))
81
88
82
- return isinstance (value , expected ) and (is_valid_style (value ) if style else True )
89
+ return isinstance (value_literal , ExpectedLiteralType ) and (
90
+ (isinstance (value_literal , str ) and is_valid_style (value_literal ))
91
+ if validate_style
92
+ else True
93
+ )
83
94
84
95
85
96
def is_valid_style (style : str ) -> bool :
86
- """Checks whether the entered color is a valid color according to rich
97
+ """Checks whether the entered color style is valid, according to ``rich``.
98
+
87
99
Parameters
88
100
----------
89
101
style
90
102
The style to check whether it is valid.
103
+
91
104
Returns
92
105
-------
93
- Boolean
94
- Returns whether it is valid style or not according to rich.
106
+ :class:`bool`
107
+ Whether the color style is valid or not, according to `` rich`` .
95
108
"""
96
109
try :
97
110
Style .parse (style )
@@ -100,16 +113,20 @@ def is_valid_style(style: str) -> bool:
100
113
return False
101
114
102
115
103
- def replace_keys (default : dict ) -> dict :
104
- """Replaces _ to . and vice versa in a dictionary for rich
116
+ def replace_keys (default : dict [str , Any ]) -> dict [str , Any ]:
117
+ """Replace ``_`` with ``.`` and vice versa in a dictionary's keys for
118
+ ``rich``.
119
+
105
120
Parameters
106
121
----------
107
122
default
108
- The dictionary to check and replace
123
+ The dictionary whose keys will be checked and replaced.
124
+
109
125
Returns
110
126
-------
111
127
:class:`dict`
112
- The dictionary which is modified by replacing _ with . and vice versa
128
+ The dictionary whose keys are modified by replacing ``_`` with ``.``
129
+ and vice versa.
113
130
"""
114
131
for key in default :
115
132
if "_" in key :
@@ -133,7 +150,7 @@ def replace_keys(default: dict) -> dict:
133
150
help = "Manages Manim configuration files." ,
134
151
)
135
152
@cloup .pass_context
136
- def cfg (ctx ) :
153
+ def cfg (ctx : cloup . Context ) -> None :
137
154
"""Responsible for the cfg subcommand."""
138
155
pass
139
156
@@ -147,7 +164,7 @@ def cfg(ctx):
147
164
help = "Specify if this config is for user or the working directory." ,
148
165
)
149
166
@cloup .option ("-o" , "--open" , "openfile" , is_flag = True )
150
- def write (level : str = None , openfile : bool = False ) -> None :
167
+ def write (level : str | None = None , openfile : bool = False ) -> None :
151
168
config_paths = config_file_paths ()
152
169
console .print (
153
170
"[yellow bold]Manim Configuration File Writer[/yellow bold]" ,
@@ -166,7 +183,7 @@ def write(level: str = None, openfile: bool = False) -> None:
166
183
action = "save this as"
167
184
for category in parser :
168
185
console .print (f"{ category } " , style = "bold green underline" )
169
- default = parser [category ]
186
+ default = cast ( dict [ str , Any ], parser [category ])
170
187
if category == "logger" :
171
188
console .print (RICH_COLOUR_INSTRUCTIONS )
172
189
default = replace_keys (default )
@@ -249,7 +266,7 @@ def write(level: str = None, openfile: bool = False) -> None:
249
266
250
267
251
268
@cfg .command (context_settings = cli_ctx_settings )
252
- def show ():
269
+ def show () -> None :
253
270
parser = make_config_parser ()
254
271
rich_non_style_entries = [a .replace ("." , "_" ) for a in RICH_NON_STYLE_ENTRIES ]
255
272
for category in parser :
@@ -269,7 +286,7 @@ def show():
269
286
@cfg .command (context_settings = cli_ctx_settings )
270
287
@cloup .option ("-d" , "--directory" , default = Path .cwd ())
271
288
@cloup .pass_context
272
- def export (ctx , directory ) :
289
+ def export (ctx : cloup . Context , directory : str ) -> None :
273
290
directory_path = Path (directory )
274
291
if directory_path .absolute == Path .cwd ().absolute :
275
292
console .print (
0 commit comments