Skip to content

Migrated file_ops.py and scene_file_writer.py from os.path to Pathlib #2642

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 14 commits into from
Apr 5, 2022

Conversation

WillSoltas
Copy link
Contributor

@WillSoltas WillSoltas commented Mar 28, 2022

Overview: What does this pull request change?

In file_ops.py and scene_file_writer.py: Uses of str type file names have been mostly (see further information) converted to pathlib's Path objects. Uses of os.path methods have been converted to equivalent pathlib methods.

Motivation and Explanation: Why and how do your changes improve the library?

We focused on the Issue/enhancement/feature request here

Migrating to Pathlib objects and methods conforms to the maintainability and "built-in" standards of the project. Generally, Pathlib tends to conform better to OOP standards in python than os.path. This refactoring contributes to making the project—and its path types—consistent.

Links to added or changed documentation pages

N/A

Further Information and Comments

There is still a single use of str-based path objects with self.sections_output_dir = "" (scene_file_writer.py:128). We considered /dev/null or NUL as a default path. We ultimately left it as a str object as the Path() default constructor creates a path to the current directory (instead of ""), which might cause unwanted issues with path concatenation.

Additionally, we ran pytest on the entire suite to ensure our changes did not introduce any unwanted I/O behavior.

Reviewer Checklist

  • The PR title is descriptive enough for the changelog, and the PR is labeled correctly
  • If applicable: newly added non-private functions and classes have a docstring including a short summary and a PARAMETERS section
  • If applicable: newly added functions and classes are tested

@WillSoltas
Copy link
Contributor Author

Multiple commits are from differences between Path objects and WindowsPath objects and me not being able to test this on a Windows file system.

@ad-chaos ad-chaos added the enhancement Additions and improvements in general label Mar 29, 2022
Copy link
Collaborator

@ad-chaos ad-chaos left a comment

Choose a reason for hiding this comment

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

Thank you for your work! This looks good to me 👍

@Darylgolden
Copy link
Member

Thank you! Can you help resolve the merge conflict?

@ad-chaos
Copy link
Collaborator

ad-chaos commented Apr 1, 2022

Looking at the merge conflicts, I think it would be nice to have the guarantee_empty_existence and guarantee_existence methods to only accept paths. Most methods in the library do pass a Path object to it only two tests in tests/test_file_ops.py pass strings.

Would you change those tests to pass paths as well?

@ad-chaos
Copy link
Collaborator

ad-chaos commented Apr 1, 2022

So then the methods would look like, with type hints as:

def guarantee_existence(path: Path) -> Path:
    if not path.exists():
        path.mkdir(parents=True)
    return path.resolve(strict=True)


def guarantee_empty_existence(path: Path) -> Path:
    if path.exists():
        shutil.rmtree(str(path))
    path.mkdir(parents=True)
    return path.resolve(strict=True)


def seek_full_path_from_defaults(file_name: str, default_dir: Path, extensions: list[str]) -> Path:

@WillSoltas
Copy link
Contributor Author

Yep I’ll work on it today. Thanks for the feedback!

@WillSoltas
Copy link
Contributor Author

Added the type hints to the methods in file_ops.py and changed the file_ops tests to use path lib

Copy link
Member

@Darylgolden Darylgolden left a comment

Choose a reason for hiding this comment

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

LGTM!

@behackl behackl enabled auto-merge (squash) April 5, 2022 08:07
@behackl behackl merged commit b692589 into ManimCommunity:main Apr 5, 2022
@naveen521kk naveen521kk changed the title Migrated file_ops.py and scene_file_writer.py from os.path to Pathlib Migrated file_ops.py and scene_file_writer.py from os.path to Pathlib Apr 24, 2022
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
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants