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

plugin: add support for named plugin matchers #5103

Merged
merged 1 commit into from
Jan 20, 2023

Conversation

bastimeyer
Copy link
Member

@bastimeyer bastimeyer commented Jan 20, 2023

This adds support for optionally named plugin matchers, e.g.:

@pluginmatcher(re.compile("http://foo"), name="foo")
@pluginmatcher(re.compile("http://bar"), name="bar")
class MyPlugin(Plugin):
    def _get_streams():
        if self.matches["foo"]:
            return self._get_streams_foo()
        else:
            return self._get_streams_bar()

Both Plugin.matchers and Plugin.matches can now be referenced by matcher index, like before, as well as matcher name, if it was set. Invalid values will raise an IndexError and KeyError respectively.

This is useful if multiple matchers are defined, so match results don't have to be referenced by matcher index, which depends on the (reverse) order of the pluginmatcher decorators.

@gravyboat gravyboat merged commit 0c8375b into streamlink:master Jan 20, 2023
@bastimeyer bastimeyer deleted the plugin/named-matchers branch January 20, 2023 03:16
@mkbloke
Copy link
Member

mkbloke commented Jan 20, 2023

Just a thought on this: should/does there need to be a test for this in tests.plugins.PluginCanHandleUrl? should_match_matcher_name[]?

@bastimeyer
Copy link
Member Author

True, I forgot about that. But thinking about it, it would result in lots of duplicates if we'd add should_match_matcher_name because of the regular should_match and should_match_groups tests. The groups tests is already a superset of the regular tests, so a third one doesn't really fit.

It would probably make more sense changing the str types of those two tests to Union[str,Tuple[str,str]], so we can have the optional name attribute included. Then the test methods need to check for that tuple type and compare the first item with the matcher's name attribute. Maybe also let them fail if there's a name but it's not tested.

I wanted to update the URL matcher tests at some point anyway, because error messages are currently not particularly nice due to how the parametrization is done. I also had the idea of making the URL matcher tests more "automagical" with automatic imports of the plugin classes (derived from the test module name, since the __plugin__ export is required), but that would result in a lack of plugin class references during static analysis, e.g. in IDEs or on GitHub's new code viewer, so jumping from the plugin definition to its tests would require finding it manually, which might be annoying.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants