-
-
Notifications
You must be signed in to change notification settings - Fork 402
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
calc: extract .py command to its own file
- Loading branch information
Showing
2 changed files
with
48 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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__) |