Passed
Pull Request — master (#1711)
by dgw
02:00
created

sopel.modules.py.py()   A

Complexity

Conditions 3

Size

Total Lines 15
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 12
dl 0
loc 15
rs 9.8
c 0
b 0
f 0
cc 3
nop 2
1
# coding=utf-8
2
"""
3
py.py - Sopel Python Eval Module
4
Copyright 2008, Sean B. Palmer, inamidst.com
5
Licensed under the Eiffel Forum License 2.
6
7
https://sopel.chat
8
"""
9
from __future__ import unicode_literals, absolute_import, print_function, division
10
11
import sys
12
13
from requests import get
14
15
from sopel import module
16
17
if sys.version_info.major < 3:
18
    from urllib import quote as _quote
19
20
    def quote(s):
21
        return _quote(s.encode('utf-8')).decode('utf-8')
22
else:
23
    from urllib.parse import quote
24
25
26
BASE_TUMBOLIA_URI = 'https://oblique.sopel.chat/'
27
28
29
@module.commands('py')
30
@module.output_prefix('[py] ')
31
@module.example('.py len([1,2,3])', '3', online=True)
32
def py(bot, trigger):
33
    """Evaluate a Python expression."""
34
    if not trigger.group(2):
35
        return bot.reply('I need an expression to evaluate.')
36
37
    query = trigger.group(2)
38
    uri = BASE_TUMBOLIA_URI + 'py/'
39
    answer = get(uri + quote(query)).content.decode('utf-8')
40
    if answer:
41
        bot.say(answer)
42
    else:
43
        bot.reply('Sorry, no result.')
44
45
46
if __name__ == "__main__":
47
    from sopel.test_tools import run_example_tests
48
    run_example_tests(__file__)
49