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

DefaultConfigMixin: Modify implementation #597

Merged
merged 3 commits into from
Aug 12, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions config.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,10 +113,16 @@
AUTOINSTALL_DEPS = True

DEFAULT_CONFIG = {
'answer': {
'ANSWER_END': os.environ.get('ANSWER_END'),
},
'LabHub': {
'GH_TOKEN': os.environ.get('GH_TOKEN'),
'GL_TOKEN': os.environ.get('GL_TOKEN'),
'GH_ORG_NAME': os.environ.get('GH_ORG_NAME', 'coala'),
'GL_ORG_NAME': os.environ.get('GL_ORG_NAME', 'coala'),
},
'WolframAlpha': {
'WA_TOKEN': os.environ.get('WA_TOKEN'),
},
}
9 changes: 6 additions & 3 deletions plugins/answer.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
import json
import os
from urllib.parse import quote_plus, urljoin

from errbot import BotPlugin, botcmd
from utils.mixin import DefaultConfigMixin
import requests


class Answer(BotPlugin):
class Answer(DefaultConfigMixin, BotPlugin):

CONFIG_TEMPLATE = {
'ANSWER_END': None,
}
# Ignore LineLengthBear, PyCodestyleBear
SURVEY_LINK = 'https://docs.google.com/forms/d/e/1FAIpQLSeD8lqMWAwJx0Mewlpc5Sbeo3MH5Yi9fSfXA6jnk07-aIURSA/viewform?usp=pp_url&entry.1236347280={question}&entry.1734934116={response}&entry.75323266={message_link}'
MESSAGE_LINK = 'https://gitter.im/{uri}?at={idd}'
Expand All @@ -26,7 +29,7 @@ def construct_link(text):
@botcmd
def answer(self, msg, arg):
try:
answers = requests.get(urljoin(os.environ['ANSWER_END'], 'answer'),
answers = requests.get(urljoin(self.config['ANSWER_END'], 'answer'),
params={'question': arg}).json()
except json.JSONDecodeError:
self.log.exception('something went wrong while fetching answer for'
Expand Down
2 changes: 1 addition & 1 deletion plugins/wolfram_alpha.plug
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[Core]
name = wolfram alpha
name = WolframAlpha
module = wolfram_alpha

[Documentation]
Expand Down
11 changes: 7 additions & 4 deletions plugins/wolfram_alpha.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
import os

import wolframalpha

from errbot import BotPlugin, botcmd
from utils.mixin import DefaultConfigMixin


class WolframAlpha(BotPlugin):
class WolframAlpha(DefaultConfigMixin, BotPlugin):
"""
Query the Computational Knowledge Engine
"""

CONFIG_TEMPLATE = {
'WA_TOKEN': None,
}

def activate(self):
super().activate()
self.client = wolframalpha.Client(os.environ.get('WA_TOKEN'))
self.client = wolframalpha.Client(self.config['WA_TOKEN'])

@botcmd
def wa(self, msg, arg):
Expand Down
1 change: 0 additions & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,5 @@ exclude_lines =
pragma ${OS_NAME}: no cover
pragma Python [0-9.,]*${PYTHON_VERSION}[0-9.,]*: no cover
def message_link
def get_configuration_template

[coverage:force_end_of_section]
16 changes: 10 additions & 6 deletions tests/answer_test.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import os
import vcr
import requests_mock

Expand All @@ -9,13 +8,16 @@
class TestAnswer(IsolatedTestCase):

def setUp(self):
super().setUp()
# Ignore InvalidLinkBear
self.answer_end_point = 'http://0.0.0.0:8000'
os.environ['ANSWER_END'] = self.answer_end_point

def tearDown(self):
del os.environ['ANSWER_END']
extra_config = {
'DEFAULT_CONFIG': {
'answer': {
'ANSWER_END': self.answer_end_point,
}
}
}
super().setUp(extra_config=extra_config)

@vcr.use_cassette('tests/cassettes/answer.yaml')
def test_answer(self):
Expand All @@ -24,6 +26,8 @@ def test_answer(self):
self.assertIn('Please checkout the following links', self.pop_message())
self.push_message('!answer shell autocompletion')
self.assertIn('Please checkout the following links', self.pop_message())
self.assertCommand('!plugin config answer',
str({'ANSWER_END': self.answer_end_point}))

def test_invalid_json(self):
with requests_mock.Mocker() as m:
Expand Down
5 changes: 3 additions & 2 deletions tests/spam_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ def setUp(self):
'DEFAULT_CONFIG': {
'SpammingAlert': {
'MAX_MSG_LEN': 500,
'MAX_LINES': 5,
}
}
}
Expand All @@ -42,13 +41,15 @@ def setUp(self):

def test_spam_extra_config_callback(self):
self.testbot.assertCommand('c'*501, 'you\'re spamming')
self.testbot.assertCommand('\n'*6, 'you\'re spamming')
self.testbot.assertCommand('\n\n'*11, 'you\'re spamming')
# Since the message is not a spam, testbot will timeout
# waiting for a response
with self.assertRaises(queue.Empty):
self.testbot.assertCommand('Not a spam', 'you\'re spamming')

def test_spam_extra_config_configuration(self):
self.testbot.assertCommand('!plugin config SpammingAlert',
'{\'MAX_LINES\': 20, \'MAX_MSG_LEN\': 500}')
self.testbot.assertCommand('!plugin config SpammingAlert '
'{\'MAX_LINES\': 10}',
'configuration done')
Expand Down
7 changes: 6 additions & 1 deletion tests/wolfram_alpha_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,17 @@

class WolframAlphaTest(IsolatedTestCase):

def setUp(self):
super().setUp()

@my_vcr.use_cassette('tests/cassettes/wa.yaml')
def test_wa(self):
self.push_message('!wa 2^6')
self.assertIn('64', self.pop_message())
with self.assertLogs() as cm:
self.assertCommand('!wa this is a sentence',
'Dunno')
self.assertIn('INFO:errbot.plugins.wolfram alpha:KeyError triggered on '
self.assertIn('INFO:errbot.plugins.WolframAlpha:KeyError triggered on '
'retrieving pods.', cm.output)
self.assertCommand('!plugin config WolframAlpha',
'{\'WA_TOKEN\': None}')
36 changes: 17 additions & 19 deletions utils/mixin.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,31 +10,29 @@ def _default_config(self):

def __init__(self, bot, name=None):
super().__init__(bot, name=name)
default_config = self._default_config
if default_config and not hasattr(self, 'config'):
self.configure(default_config)
if not hasattr(self, 'CONFIG_TEMPLATE'): # pragma: no cover
self.log.error('CONFIG_TEMPLATE for plugin {} is missing.'
.format(self.name))

def get_configuration_template(self):
default_config = self._default_config
config_template = self.CONFIG_TEMPLATE

if default_config:
return default_config
elif self.CONFIG_TEMPLATE:
return self.CONFIG_TEMPLATE
else: # pragma: no cover
return
config = dict(chain(config_template.items(),
default_config.items()))
else:
config = config_template

return config

def configure(self, configuration):
default_config = self._default_config
if configuration and default_config:
config = dict(chain(
default_config.items(),
configuration.items()))
elif configuration:
config = dict(chain(self.CONFIG_TEMPLATE.items(),
configuration.items()))
elif default_config:
config = default_config
config_template = self.get_configuration_template()

if configuration is not None and configuration != {}:
config = dict(chain(config_template.items(),
configuration.items()))
else:
config = self.CONFIG_TEMPLATE
config = config_template

self.config = config