|
| 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