From 0c417518373b97f6e0213db98d678f2ad2806c5d Mon Sep 17 00:00:00 2001
From: dgw <dgw@technobabbl.es>
Date: Tue, 15 Oct 2019 05:51:14 -0500
Subject: [PATCH] py: make Oblique endpoint configurable

---
 sopel/modules/py.py | 34 ++++++++++++++++++++++++++++++++--
 1 file changed, 32 insertions(+), 2 deletions(-)

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 <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')
@@ -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.