diff --git a/sopel/modules/py.py b/sopel/modules/py.py index 996ecba560..e5bb26df54 100644 --- a/sopel/modules/py.py +++ b/sopel/modules/py.py @@ -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 ) | + """ + 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') @@ -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.