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

core: URL Callbacks new interface #1508

Merged
merged 3 commits into from
Mar 21, 2019
Merged

Conversation

Exirel
Copy link
Contributor

@Exirel Exirel commented Mar 19, 2019

This new interface adds:

  • bot.register_url_callback(regex, callback), to register a callback to call when a URL matches regex
  • bot.unregister_url_callback(regex) to remove this regex from triggering callbacks
  • bot.search_url_callbacks(url) to get a list of (callback, match) for each pattern matched by the URL

This provides a unified interface to work with URL Callbacks, but does not change how the built-in url plugin works (yet).

So this:

if not bot.memory.contains('url_callbacks'):
    bot.memory['url_callbacks'] = tools.SopelMemory()
bot.memory['url_callbacks'][instagram_pattern] = instaparse

Become this:

bot.register_url_callback(instagram_pattern, instaparse)

It does not change the fact that the url built-in plugin is required at the moment in order to work with URL Callback (see #1489 for that).

@dgw now my question is, should I keep moving forward, and replace the url built-in plugin? Or should we merge this PR as-is, and work from there in another PR?

@dgw
Copy link
Member

dgw commented Mar 20, 2019

You can forge on ahead with a new PR branch based on this one, like you've done before, unless you need to discuss direction (in #1489) first. That way, no matter how much bikeshedding happens with replacing the url module, we'll have a clean new interface (this PR) in the bag.

I like that this really is just adding convenience methods to do what plugins already do, so nothing will break (yet) if module code isn't updated. 😁

Do you think it's worth checking in the register/unregister functions added here whether the pattern is actually compiled? I can picture users who merely skimmed the documentation (or coders drunk on lack of sleep XD) trying to pass in an uncompiled regex string…

Copy link
Member

@dgw dgw left a comment

Choose a reason for hiding this comment

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

"Request changes" because it looks like some of what you intended to do in the docstrings is broken (see line notes).

While you're looking at those, maybe also think about (as I said in a "real" comment earlier tonight) whether validating the pattern is worthwhile. I just expect that Bad Things™ might happen if some enterprising coder passes in a string instead of a compiled re object—even if said Bad Things™ are just callbacks that don't trigger.

sopel/bot.py Outdated Show resolved Hide resolved
sopel/bot.py Outdated Show resolved Hide resolved
sopel/bot.py Outdated Show resolved Hide resolved
sopel/bot.py Outdated Show resolved Hide resolved
@Exirel Exirel marked this pull request as ready for review March 20, 2019 08:31
@Exirel
Copy link
Contributor Author

Exirel commented Mar 20, 2019

I like that this really is just adding convenience methods to do what plugins already do, so nothing will break (yet) if module code isn't updated. 😁

I knew you would understand. 👍

Do you think it's worth checking in the register/unregister functions added here whether the pattern is actually compiled?

Yeah, I was wondering too. It's not that complicated, I'll do that.

@Exirel
Copy link
Contributor Author

Exirel commented Mar 20, 2019

Also: of course I failed at docstring again. x)

@Exirel Exirel force-pushed the url-callbacks-interface branch from 0685b81 to cdc48fa Compare March 20, 2019 09:41
@Exirel Exirel requested a review from dgw March 20, 2019 09:41
@Exirel
Copy link
Contributor Author

Exirel commented Mar 20, 2019

Hm. I'm not sure "re-request a review" was the right idea. Meh, I guess I need to learn what the new github has to offer with all these features.

Anyway @dgw I fixed the docstrings and added the safety measures to check if pattern is a compiled regex.

@Exirel
Copy link
Contributor Author

Exirel commented Mar 20, 2019

Ahem. Checkstyle?

Edit: also tests are broken because of course they are. Mock objects are the worst. :(

@Exirel Exirel force-pushed the url-callbacks-interface branch from cdc48fa to 7e35d5b Compare March 20, 2019 10:08
Copy link
Member

@dgw dgw left a comment

Choose a reason for hiding this comment

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

Yay, I'm not the only one who gets bitten by checkstyle after pushing! 😁

It's nitpick-docstrings-o'clock again, lol.

sopel/test_tools.py Show resolved Hide resolved
sopel/bot.py Outdated Show resolved Hide resolved
@dgw dgw removed the Needs Review label Mar 20, 2019
Exirel added 3 commits March 20, 2019 15:19
And to search for URL matching patterns to get these callbacks:

* `bot.register_url_callback(regex, callback)`, to register a
  `callback` to call when a URL matches `regex`
* `bot.unregister_url_callback(regex)` to remove this regex from
  triggering callbacks
* `bot.search_url_callbacks(url)` to get a list of (callback, match)
  for each pattern matched by the URL

This provides a unified interface to work with URL Callbacks, but does
not change how the built-in url plugin works (yet).
@Exirel Exirel force-pushed the url-callbacks-interface branch from 7e35d5b to 2c81f72 Compare March 20, 2019 14:19
Copy link
Member

@dgw dgw left a comment

Choose a reason for hiding this comment

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

Approved, with the caveat that the additions to test_tools.MockSopel will be temporary until we find a way to run tests on the "real" Sopel class instead of a mocked-up version.

@dgw dgw merged commit 4aba0f2 into sopel-irc:master Mar 21, 2019
@dgw
Copy link
Member

dgw commented Mar 21, 2019

Merging so @Exirel can properly get started on a PR to fix the mess that is url.py. 🖖

@Exirel Exirel deleted the url-callbacks-interface branch March 21, 2019 08:32
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.

2 participants