Skip to content

Commit

Permalink
py: make Oblique endpoint configurable
Browse files Browse the repository at this point in the history
  • Loading branch information
dgw committed Nov 18, 2019
1 parent 56d117b commit 0c41751
Showing 1 changed file with 32 additions and 2 deletions.
34 changes: 32 additions & 2 deletions sopel/modules/py.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,41 @@

from requests import get

from sopel.config.types import StaticSection, ValidatedAttribute
from sopel.module import commands, example
from sopel.tools.web import quote


BASE_TUMBOLIA_URI = 'https://oblique.sopel.chat/'
class PySection(StaticSection):
oblique_instance = ValidatedAttribute('oblique_instance',
default='https://oblique.sopel.chat/')
"""The Oblique instance to use when evaluating Python expressions"""


def configure(config):
"""
| name | example | purpose |
| ---- | ------- | ------- |
| oblique_instance | https://oblique.sopel.chat/ | The Oblique instance to use when evaluating Python expressions (see <https://github.com/sopel-irc/oblique>) |
"""
config.define_section('py', PySection)
config.py.configure_setting(
'oblique_instance',
'Enter the base URL of a custom Oblique instance (optional): '
)


def setup(bot):
bot.config.define_section('py', PySection)

if not any(
bot.config.py.oblique_instance.startswith(prot)
for prot in ['http://', 'https://']
):
raise ValueError('Oblique instance URL must start with a protocol.')

if not bot.config.py.oblique_instance.endswith('/'):
bot.config.py.oblique_instance += '/'


@commands('py')
Expand All @@ -25,7 +55,7 @@ def py(bot, trigger):
return bot.reply('I need an expression to evaluate.')

query = trigger.group(2)
uri = BASE_TUMBOLIA_URI + 'py/'
uri = bot.config.py.oblique_instance + 'py/'
answer = get(uri + quote(query)).content.decode('utf-8')
if answer:
# bot.say can potentially lead to 3rd party commands triggering.
Expand Down

0 comments on commit 0c41751

Please sign in to comment.