-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
Conversation
Thanks for the contribution ! |
…o#921) Invalid video URL in docs/getting_started/compositing
@Overdrivr I added a test case in the |
Thanks for the update, I'm not certain what happened when you rebased. |
Sorry about the mess, but I think it's good to go now. |
@PaperBag42 Thanks for the heads-up, it's been fixed on master, can you try updating your branch ? |
@Overdrivr Done, the tests are passing! |
There was a problem hiding this 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 |
There was a problem hiding this comment.
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") |
There was a problem hiding this comment.
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
moviepy/compat.py
Outdated
|
||
def fspath(path): # Python 3.4-3.5 | ||
if isinstance(path, (string_types, pathlib.Path)): | ||
return str(path) |
There was a problem hiding this comment.
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
I hadn't heard about pathlib2 before, good to know it exists. |
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:
|
Sorry for disappearing for so long! |
|
||
try: | ||
from os import fspath # Python 3.6+ | ||
except ImportError: |
There was a problem hiding this comment.
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'))) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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'))) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
video.write_videofile(Path(os.path.join(TMP_DIR, 'pathlike.mp4'))) | |
video.write_videofile(Path(TMP_DIR) / 'pathlike.mp4') |
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 |
Wow, I'm glad I was able to help after all! |
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.