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

[utils] Backport traverse_obj (etc) from yt-dlp #31156

Merged
merged 16 commits into from
Nov 3, 2022

Conversation

lebdron
Copy link
Contributor

@lebdron lebdron commented Aug 10, 2022

Please follow the guide below

  • You will be asked some questions, please read them carefully and answer honestly
  • Put an x into all the boxes [ ] relevant to your pull request (like that [x])
  • Use Preview tab to see how your pull request will actually look like

Before submitting a pull request make sure you have:

In order to be accepted and merged into youtube-dl each piece of code must be in public domain or released under Unlicense. Check one of the following options:

  • I am the original author of this code and I am willing to release it under Unlicense, except for code from yt-dlp for which this or the below has been asserted
  • I am not the original author of this code but it is in public domain or released under Unlicense (provide reliable evidence)

What is the purpose of your pull request?

  • Bug fix
  • Improvement
  • New extractor
  • New feature

Description of your pull request and other information

Backport traverse_obj from yt-dlp required for #31097.

Thanks @pukkandan (orignal code), @Grub4K (rewrite & tests).

@dirkf
Copy link
Contributor

dirkf commented Aug 11, 2022

Please use my full port in PR #30713 as a basis. Feel free to define variadic() in this PR too, instead of locally within traverse_obj(), and to define IDENTITY(), get_first() and join_nonempty() as well.

The from_iterable() function in my code is needed because the library function used in yt-dlp isn't always available.

The Nonlocal thing is to simulate the nonlocal syntax for older Pythons. As you know, assigning to a previously undefined variable creates a variable binding in the current scope, unless the variable has been defined global. nonlocal tells Python to look for the variable on the call stack too, so that a new binding is not created when assigning to the variable. By making the variable that is to be nonlocal an instance value of an object, an assignment to the variable doesn't create a new binding, but updates the existing value.

listish() defines an expression used in several places so that the list of listish things can be updated with one change.

As you've done, ... has to be replaced with Ellipsis as the ... syntax is only in recent Pythons.

Finally, any str should be compat_str.

Even more finally, should we ask @pukkandan to write tests for it?

The code in PR #30713 was correct enough for that extractor, but as was commented extractors only use a subset of traverse_obj() functionality.

@lebdron
Copy link
Contributor Author

lebdron commented Aug 12, 2022

@dirkf Thank you, done. I looked at your commit, ported some features which improve readability, like Nonlocal. I also used the new code from yt-dlp/yt-dlp@1797b07 and yt-dlp/yt-dlp@e6f868a .

I would suggest to merge this PR as is, without extensive test coverage, as it will be useful to port Panopto sooner, and it does not break any existing code. Tests can be added later in a separate pull request.

@pukkandan
Copy link
Contributor

pukkandan commented Sep 3, 2022

Even more finally, should we ask @pukkandan to write tests for it?

I honestly don't have too much incentive to write tests for it. Because of yt-dlp's extensive use of traverse_obj in "output template", test_prepare_outtmpl_and_filename kinda acts like a decent test suite for traverse_obj. But if anyone wants to contribute tests, it is welcome.

@pukkandan
Copy link
Contributor

Complete rewrite and tests: yt-dlp/yt-dlp#5024

@dirkf
Copy link
Contributor

dirkf commented Sep 27, 2022

Good news: there are tests
Bad news: the back-ported code should match the new code

Copy link
Contributor

@dirkf dirkf left a comment

Choose a reason for hiding this comment

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

Recast with yt-dlp's new traverse_obj() implementation and tests.

youtube_dl/utils.py Show resolved Hide resolved
test/test_utils.py Show resolved Hide resolved
test/test_utils.py Outdated Show resolved Hide resolved
youtube_dl/utils.py Outdated Show resolved Hide resolved
youtube_dl/utils.py Outdated Show resolved Hide resolved
youtube_dl/utils.py Outdated Show resolved Hide resolved
test/test_utils.py Outdated Show resolved Hide resolved
test/test_utils.py Outdated Show resolved Hide resolved
test/test_utils.py Outdated Show resolved Hide resolved
test/test_utils.py Outdated Show resolved Hide resolved
test/test_utils.py Outdated Show resolved Hide resolved
test/test_utils.py Outdated Show resolved Hide resolved
test/test_utils.py Outdated Show resolved Hide resolved
test/test_utils.py Outdated Show resolved Hide resolved
@dirkf dirkf changed the title [utils] Backport traverse_obj from yt-dlp [utils] Backport traverse_obj (etc) from yt-dlp Nov 2, 2022
@dirkf dirkf merged commit 27ed77a into ytdl-org:master Nov 3, 2022
github-actions bot added a commit to hellopony/youtube-dl that referenced this pull request Nov 3, 2022
gaming-hacker added a commit to gaming-hacker/youtube-dl that referenced this pull request Nov 4, 2022
* commit 'de39d1281cea499cb1adfce5ff7e0a56f1bad5fe':
  [extractor/ceskatelevize] Back-port extractor from yt-dlp, etc (ytdl-org#30713)
  [utils] Backport traverse_obj (etc) from yt-dlp (ytdl-org#31156)
  [compat] Work around in case folding for narrow Python build
  [compat] Add test for compat_casefold()
  [compat] Add test for compat_casefold()
  [compat] Reformat casefold.py for easier updating
  [compat] Unify unicode/str compat and move up
  [compat] Add compat_casefold and compat_re_Match, for traverse_obj() port
  [compat] Add Python 2 Unicode casefold using a trivial wrapper around icu/CaseFolding.txt
  [netease] Support urls shared from mobile app (ytdl-org#31304)
  [netease] Impove error handling (ytdl-org#31303)
  [Vimeo] Update variable name in hydration JSON pattern

# Conflicts:
#	youtube_dl/extractor/ceskatelevize.py
alxlive pushed a commit to alxlive/youtube-dl that referenced this pull request Feb 27, 2023
* Backport traverse_obj and closely related function from yt-dlp (code by pukkandan)
* Backport LazyList, variadic(), try_call (code by pukkandan)
* Recast using yt-dlp's newer traverse_obj() implementation and tests (code by grub4k)
* Add tests for Unicode case folding support matching Py3.5+ (requires f102e3d)
* Improve/add tests for variadic, try_call, join_nonempty

Co-authored-by: dirkf <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants