Skip to content

Commit

Permalink
calc: extract .py command to its own file
Browse files Browse the repository at this point in the history
  • Loading branch information
dgw committed Oct 14, 2019
1 parent 9183d86 commit c07bf12
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 35 deletions.
35 changes: 0 additions & 35 deletions sopel/modules/calc.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,27 +8,9 @@
"""
from __future__ import unicode_literals, absolute_import, print_function, division

import sys

from requests import get

from sopel.module import commands, example
from sopel.tools.calculation import eval_equation

if sys.version_info.major < 3:
from urllib import quote as _quote

def quote(s):
return _quote(s.encode('utf-8')).decode('utf-8')
else:
from urllib.parse import quote

if sys.version_info.major >= 3:
unichr = chr


BASE_TUMBOLIA_URI = 'https://tumbolia-sopel.appspot.com/'


@commands('c', 'calc')
@example('.c 5 + 3', '8')
Expand All @@ -54,23 +36,6 @@ def c(bot, trigger):
bot.reply(result)


@commands('py')
@example('.py len([1,2,3])', '3', online=True)
def py(bot, trigger):
"""Evaluate a Python expression."""
if not trigger.group(2):
return bot.say("Need an expression to evaluate")

query = trigger.group(2)
uri = BASE_TUMBOLIA_URI + 'py/'
answer = get(uri + quote(query)).content.decode('utf-8')
if answer:
# bot.say can potentially lead to 3rd party commands triggering.
bot.reply(answer)
else:
bot.reply('Sorry, no result.')


if __name__ == "__main__":
from sopel.test_tools import run_example_tests
run_example_tests(__file__)
48 changes: 48 additions & 0 deletions sopel/modules/py.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# coding=utf-8
"""
py.py - Sopel Python Eval Module
Copyright 2008, Sean B. Palmer, inamidst.com
Licensed under the Eiffel Forum License 2.
https://sopel.chat
"""
from __future__ import unicode_literals, absolute_import, print_function, division

import sys

from requests import get

from sopel.module import commands, example

if sys.version_info.major < 3:
from urllib import quote as _quote

def quote(s):
return _quote(s.encode('utf-8')).decode('utf-8')
else:
from urllib.parse import quote


BASE_TUMBOLIA_URI = 'https://tumbolia-sopel.appspot.com/'


@commands('py')
@example('.py len([1,2,3])', '3', online=True)
def py(bot, trigger):
"""Evaluate a Python expression."""
if not trigger.group(2):
return bot.say("Need an expression to evaluate")

query = trigger.group(2)
uri = BASE_TUMBOLIA_URI + 'py/'
answer = get(uri + quote(query)).content.decode('utf-8')
if answer:
# bot.say can potentially lead to 3rd party commands triggering.
bot.reply(answer)
else:
bot.reply('Sorry, no result.')


if __name__ == "__main__":
from sopel.test_tools import run_example_tests
run_example_tests(__file__)

0 comments on commit c07bf12

Please sign in to comment.