Skip to content

Implement a basic Subcommand structure. #212

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 43 commits into from
Aug 2, 2020
Merged

Implement a basic Subcommand structure. #212

merged 43 commits into from
Aug 2, 2020

Conversation

Aathish04
Copy link
Member

@Aathish04 Aathish04 commented Jul 22, 2020

List of Changes

  • Add a subparser for commands related to modifying the config files manim uses and creates.
  • Implement write, show and export commands with said subparser.
    • Write
      Writes a custom config file based off of default.cfg and saves it to the user config location if the level passed is user or to the current working directory if the level passed is cwd. If no level is passed, the config file writer will ask the user where to save the config.
      manim cfg write --level cwd
      manim cfg write --level user
      manim cfg write
    • Show
      Prints the Final Configuration at the current working directory to the Terminal.
      manim cfg show
    • Export
      Exports the Final Configuration at the current working directory to --dir.
      manim cfg export
      manim cfg export --dir ./CustomConfigs/
  • Remove the manim-cfg command.

Motivation

The introduction of the Config File writer opened up a lot of possibilities for config related utilities as discussed in #201 . These utilities would go well under the manim command, under the cfg subcommand, as suggested by @leotrs, and that is what this PR addresses.

Explanation for Changes

A subparser for commands related to modifying the config files manim uses and creates has been added, and to this, 3 more sub-subparsers, for the write, show and export subcommands have been added.

The manim-cfg command has been removed, as it only linked to a single function from a single file, and implementing subcommands for every variant of manim-cfg and manimcm-cfg would get very tedious. This command has been wholly replaced by the subcommands to manim mentioned previously.

Testing Status

Tested with no errors, and outputs as expected.

Page1
Page2

Further Comments

I'm not entirely satisfied with the way I've implemented this. Currently, the __main__ function in main.py has to be modified to not ask for a default file if the cfg subcommand is invoked, and I feel like my solution was a tad too hacky. If a better way exists, don't hesitate to review this PR to shreds.

Acknowledgement

Aathish Sivasubrahmanian added 6 commits July 22, 2020 16:39
Added show method to show config at cwd.

Added export method to export config at cwd.
Added actions for subcommands.
Set cfg write default value to None.

This means that if no argument is provided, the configwriter will ask for location.
Make `export`'s log a bit clearer.
@Aathish04 Aathish04 requested review from leotrs, eulertour, kilacoda-old and XorUnison and removed request for leotrs and eulertour July 22, 2020 17:13
Aathish Sivasubrahmanian added 3 commits July 22, 2020 23:05
@Aathish04 Aathish04 linked an issue Jul 22, 2020 that may be closed by this pull request
@Aathish04
Copy link
Member Author

Aathish04 commented Jul 23, 2020

For the benefit of those who want to review this, I'd like to give a rundown of how the subcommand system will work.

  • In the beginning, calling manim from the command line will run manim/main.py:__main__.
    In main.py, utils/cfg_subcmds is imported, which has the functions that all the subcommands will execute.
    Before the __main__ function is executed, file_writer_config and args are imported from config.py.

    • In config.py, _run_config() is imported from config_utils.py.

    • In _run_config(), _parse_cli() is called.

      • Here, an explicit check is performed to see if a subcommand has been executed along with the main manim command.
      • If it does find a subcommand, the normal parser gets a shiny new subparser, within which a sub-subparser for cfg related commands is initialised and arguments for those added. Note that if it finds a subcommand, file and other compulsory arguments won't be added to the main Argument Parser. If it doesn't find a subcommand, it realises that this is just to render an animation, and everything is initialised "normally".
      • Now, the cfg sub-subparser parses the subcommand for cfg to determine what to do, and manually adds it to the main parser's Namespace. _run_config() then returns the main parser's Namespace.
    • Now that _run_config() has the required namespace (called args here, for brevity), it will check if args has a subcommands attribute.
      If it does, it gets the cwd's manim.cfg if it's present, and adds it to the list of config_files. If it doesn't, the usual steps that were taken before this PR are followed.

    • _run_config then calls _parse_file_writer_config to get file_writer_config.

      • If args has a subcommands attribute, _parse_file_writer_config won't set input_file and other parameters that don't matter in a subcommand in fw_config, and returns the file_writer_config.
      • If args doesn't have such an attribute, _parse_file_writer_config proceeds as normal and returns the file_writer_config.
    • All of these configs, along with a list of the parsed cfg files returned by _run_config is unpacked in config.py.

  • Back in __main__.py, if args has any subcommands, those subcommands are given top priority and the functions corresponding to them are called. If they don't, then manim assumes it's just being asked to render an animation and does that.

And that's how this subcommand structure works.

@PgBiel PgBiel added enhancement Additions and improvements in general refactor Refactor or redesign of existing code labels Jul 24, 2020
Copy link
Member

@naveen521kk naveen521kk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. Issue: This should not happen we need a error message here.
    err
  2. Suggestion: Move the ones, like the CLI in Green to centre
    sugg
  3. Issue: Change the colour of the link to something else other then blue.
    powershell
  4. Suggestion: Read Configuration Files should not be shown while writing cfg files
  5. Issue: Export command doesn't work
  6. Issue: Write Only what is edited into manim.cfg instead of writing everything.
  7. Nitpick:Create the directory before writing the file. PS> He told me correct command later.

@Aathish04
Copy link
Member Author

Aathish04 commented Jul 24, 2020

@naveen521kk

  1. Issue: This should not happen we need a error message here.
    err

Instead, the program now asks the user if they want to export to the same directory they are reading from.

  1. Suggestion: Move the ones, like the CLI in Green to centre

Eh, it doesn't look very nice. I'll let you be the judge:

Untitled 2

I haven't committed that change.

  1. Issue: Change the colour of the link to something else other then blue.

Yeah, shame powershell has a blue background. I have changed the link to green in the latest commit.

  1. Suggestion: Read Configuration Files should not be shown while writing cfg files

This is a byproduct of the fact that the config files are read before anything else happens when the command manim cfg is executed. Also, I don't see why this will pose a problem, as the config utilities can know the current config preferences.

  1. Issue: Export command doesn't work

The command manim cfg export does work. I had mistakenly left out the cfg in the main comment for this PR. This has since been rectified.

  1. Issue: Write Only what is edited into manim.cfg instead of writing everything.

I have not touched the configwriter function in this PR. It, by default , writes the entire contents of default.cfg , and just changes the values as required.

  1. Nitpick:Create the directory before writing the file.

I have added a warning and made sure the directory is created before writing the file, in the latest commit.

@leotrs
Copy link
Contributor

leotrs commented Jul 24, 2020

Suggestion: Move the ones, like the CLI in Green to centre

Agree with @Aathish04, centered titles don't look great.

Suggestion: Read Configuration Files should not be shown while writing cfg files

I think they should be shown.

Issue: Write Only what is edited into manim.cfg instead of writing everything.

This is an interesting suggestion, and I'm on the fence about it. If you feel strongly about it @naveen521kk please open a new issue and let's keep it separate from this PR.

Aathish04 and others added 5 commits July 28, 2020 08:36
As suggested by @huguesdevimeux

Co-authored-by: Hugues Devimeux <36239975+huguesdevimeux@users.noreply.github.com>
Change cwd and change back in test_cfg_subcmd itself.
Copy link
Member

@huguesdevimeux huguesdevimeux left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I left some comments.
It's perfectly ok if you don't answer to all of them, as a lot of them are non-blocking and pure nitpicks :D)

Aathish04 and others added 8 commits July 29, 2020 09:56
Thank you @huguesdevimeux !

Co-authored-by: Hugues Devimeux <36239975+huguesdevimeux@users.noreply.github.com>
Changed a misleading exit message.

As suggested by @huguesdevimeux :)
Thank you @huguesdevimeux !

Co-authored-by: Hugues Devimeux <36239975+huguesdevimeux@users.noreply.github.com>
@Aathish04
Copy link
Member Author

@leotrs @naveen521kk @huguesdevimeux @XorUnison

I think I've addressed all your recommendations. Feel free to tell me if there's anything else!

@Aathish04 Aathish04 requested a review from huguesdevimeux July 30, 2020 04:15
Copy link
Member

@huguesdevimeux huguesdevimeux left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SGTM !

@leotrs
Copy link
Contributor

leotrs commented Aug 2, 2020

On my first review, I asked how can we add tests for this. Were those tests added? If not, please open a separate issue to that effect. Merging now!

EDIT: tried to merge but there are conflicts. @Aathish04 please resolve conflicts and feel free to merge.

@huguesdevimeux
Copy link
Member

On my first review, I asked how can we add tests for this. Were those tests added? If not, please open a separate issue to that effect. Merging now!

EDIT: tried to merge but there are conflicts. @Aathish04 please resolve conflicts and feel free to merge.

no need to create an issue. Add the test required in #193

@Aathish04 Aathish04 merged commit b431559 into ManimCommunity:master Aug 2, 2020
@Aathish04
Copy link
Member Author

On my first review, I asked how can we add tests for this. Were those tests added? If not, please open a separate issue to that effect. Merging now!

EDIT: tried to merge but there are conflicts. @Aathish04 please resolve conflicts and feel free to merge.

Yes, the tests mentioned were added.

@Aathish04 Aathish04 deleted the cfg-subcommand branch August 3, 2020 05:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement Additions and improvements in general refactor Refactor or redesign of existing code
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add a manim cfg-show sub command
6 participants