Skip to content
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

Add support for path-like objects #924

Closed
wants to merge 18 commits into from

Conversation

PaperBag42
Copy link

  • If this is a bugfix, I have provided code that clearly demonstrates the problem and that works when used with this PR
  • I have added a test to the test suite, if necessary
  • I have properly documented new or changed features in the documention, or the docstrings
  • I have properly documented unusual changes to the code in the comments around it
  • I have made note of any breaking/backwards incompatible changes

Path-like objects are a new type of objects which are meant to represent file system paths. The concept was introduced in Python 3.4 as the pathlib.Path class, and later expanded to a full object protocol in Python 3.6. It is already supported by imageio, and I have been using it heavily myself.

I added a function that converts a path-like object to a regular string to the compat module, and used it in each method I found in the docs which requires a file name. I hope I didn't miss any.

@Overdrivr
Copy link
Collaborator

Thanks for the contribution !
Could you please add test cases for the different modified classes to ensure the fix works as expected ?

@Overdrivr Overdrivr added the tests-needed PRs/code submissions which need test cases added to them. label Mar 28, 2019
@PaperBag42
Copy link
Author

@Overdrivr I added a test case in the test_PR module, I hope it's good.
I also think I messed up while rebasing my branch, I hope it's not too big of a deal 😅

@Overdrivr
Copy link
Collaborator

Thanks for the update, I'm not certain what happened when you rebased.
Could you merge Zulko:master into your branch to see if it fixes it ?

@PaperBag42
Copy link
Author

Sorry about the mess, but I think it's good to go now.
AppVeyor still fails, but this time it's because the latest ImageMagick version is missing from the server.

@Overdrivr
Copy link
Collaborator

@PaperBag42 Thanks for the heads-up, it's been fixed on master, can you try updating your branch ?

@PaperBag42
Copy link
Author

@Overdrivr Done, the tests are passing!

Copy link
Collaborator

@Overdrivr Overdrivr left a comment

Choose a reason for hiding this comment

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

Hi, I've made a few comments, thanks again for your contribution. I haven't personnally used pathlib2 so feel free to let me know if something doesn't make sense to you.

tests/test_PR.py Outdated
try:
from pathlib import Path
except ImportError:
Path = str
Copy link
Collaborator

Choose a reason for hiding this comment

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

There is a backport of Python3 pathlib for python 2. Could you remove this (line 16 to 19) and add pathlib2 as dependency ? This way your code will be active even on Python2

tests/test_PR.py Outdated
except ImportError:
Path = str

sys.path.append("tests")
Copy link
Collaborator

Choose a reason for hiding this comment

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

Please remove this, it should not be needed


def fspath(path): # Python 3.4-3.5
if isinstance(path, (string_types, pathlib.Path)):
return str(path)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Please see comment below regarding using pathlib2 instead of bypassing

@PaperBag42
Copy link
Author

I hadn't heard about pathlib2 before, good to know it exists.
I've made the changes, looks like it's working. (AppVeyor fails this time around because one of the tests took a microsecond too long to complete...)

@Overdrivr
Copy link
Collaborator

Thanks for the changes. I've noticed this test, it's randomly failing (I've tracked this issue with #943 and will try to fix it when I have some free time).

Some final feedback:

  • Could you split your test into multiple tests, so that if one fails it does not affect the others ?
  • Could you document this new feature into documentation ?

@Overdrivr Overdrivr added the documentation-needed Docs & docstrings which need adding or updating to rectify missing, incorrect or misleading info. label Apr 12, 2019
@PaperBag42
Copy link
Author

Sorry for disappearing for so long!
Anyway, I split the test function into smaller functions, and it looks like I had already added documentation in 17e7748. Have I missed a detail?

@tburrows13 tburrows13 mentioned this pull request Feb 23, 2020
7 tasks
@coveralls
Copy link

Coverage Status

Coverage increased (+0.3%) to 65.256% when pulling b80348d on PaperBag42:feature/pathlike-support into a16a107 on Zulko:master.

@tburrows13 tburrows13 added this to the Release v2.0.0 milestone Feb 24, 2020
@tburrows13 tburrows13 added the feature New addition to the API i.e. a new class, method or parameter. label Mar 10, 2020

try:
from os import fspath # Python 3.6+
except ImportError:
Copy link
Collaborator

Choose a reason for hiding this comment

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

We agreed to implement #1081, so this compat function won't be necessary.

Test support for path-like objects as arguments for AudioFileClip.
"""
with AudioFileClip(Path('media/crunching.mp3')) as audio:
audio.write_audiofile(Path(os.path.join(TMP_DIR, 'pathlike.mp3')))
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
audio.write_audiofile(Path(os.path.join(TMP_DIR, 'pathlike.mp3')))
audio.write_audiofile(Path(TMP_DIR) / 'pathlike.mp3')

Test support for path-like objects as arguments for VideoFileClip.
"""
with VideoFileClip(Path('media/big_buck_bunny_432_433.webm')) as video:
video.write_videofile(Path(os.path.join(TMP_DIR, 'pathlike.mp4')))
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
video.write_videofile(Path(os.path.join(TMP_DIR, 'pathlike.mp4')))
video.write_videofile(Path(TMP_DIR) / 'pathlike.mp4')

@tburrows13
Copy link
Collaborator

Hey @PaperBag42. You'd done everything necessary to get this merged at the time, but it never was. I tried to get this into a mergeable state today, but the merging process was a giant mess that I wasn't confident that I'd done it correctly (partially because we've reformatted the entire codebase (#1097) since this was written). We've also dropped support for Python 2, so all the compatibility stuff is now unnecessary.

I also thought that this would be a good place to instead use a decorator to automatically and consistently apply os.fspath() to the correct parameters. Given that all these combine to make some quite large changes, I thought it easier to create a new PR from scratch: #1137. Big thank you for doing the bulk of the work tracing down each place fspath() was needed and writing the tests, but, assuming that #1137 is merged, this PR can be closed.

@PaperBag42
Copy link
Author

Wow, I'm glad I was able to help after all!
Since the day I had opened this PR I moved on to other projects (and learned how to do a git rebase correctly). But at the time, Moviepy was of great help to me. So thank you for your work on this package!

@PaperBag42 PaperBag42 closed this Apr 7, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation-needed Docs & docstrings which need adding or updating to rectify missing, incorrect or misleading info. feature New addition to the API i.e. a new class, method or parameter. tests-needed PRs/code submissions which need test cases added to them.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

7 participants