Added (working) first version of new python thingy
This commit is contained in:
parent
2871bd3211
commit
30f1c18d94
3 changed files with 33 additions and 18 deletions
|
@ -1,7 +1,7 @@
|
|||
# Written by Scaevolus 2010
|
||||
from util import hook, http
|
||||
from util.text import multiword_replace
|
||||
from util.eval import execute_eval
|
||||
from util.execute import eval_py
|
||||
import string
|
||||
import sqlite3
|
||||
import re
|
||||
|
@ -136,9 +136,9 @@ def factoid(inp, say=None, db=None, bot=None, me=None, conn=None, input=None):
|
|||
# if the factoid starts with <py>, its a dynamic one
|
||||
if data.startswith("<py>"):
|
||||
data = data[4:].strip()
|
||||
variables = "input='%s'; nick='%s'; chan='%s'; bot_nick='%s';" % (args,
|
||||
variables = "input='%s'; nick='%s'; chan='%s'; bot_nick='%s';" % (arguments,
|
||||
input.nick, input.chan, input.conn.nick)
|
||||
result = execute_eval(variables + data)
|
||||
result = eval_py(variables + data)
|
||||
|
||||
else:
|
||||
result = data
|
||||
|
|
|
@ -1,23 +1,10 @@
|
|||
import re
|
||||
|
||||
from util import hook, http
|
||||
|
||||
|
||||
re_lineends = re.compile(r'[\r\n]*')
|
||||
|
||||
from util.execute import eval_py
|
||||
|
||||
@hook.command
|
||||
def python(inp):
|
||||
"python <prog> -- Executes <prog> as Python code."
|
||||
|
||||
inp = inp.replace("~n", "\n")
|
||||
|
||||
res = http.get("http://eval.appspot.com/eval", statement=inp).splitlines()
|
||||
|
||||
if len(res) == 0:
|
||||
return
|
||||
res[0] = re_lineends.split(res[0])[0]
|
||||
if not res[0] == 'Traceback (most recent call last):':
|
||||
return res[0].decode('utf8', 'ignore')
|
||||
else:
|
||||
return res[-1].decode('utf8', 'ignore')
|
||||
return eval_py(inp)
|
||||
|
|
28
plugins/util/execute.py
Normal file
28
plugins/util/execute.py
Normal file
|
@ -0,0 +1,28 @@
|
|||
import http
|
||||
import json, urllib2, sys
|
||||
|
||||
|
||||
def haste(data):
|
||||
URL = "http://paste.dmptr.com"
|
||||
request = urllib2.Request(URL + "/documents", data)
|
||||
response = urllib2.urlopen(request)
|
||||
return("%s/%s" % (URL, json.loads(response.read())['key']))
|
||||
|
||||
|
||||
def eval_py(code, paste_multiline=True):
|
||||
while True:
|
||||
output = http.get("http://eval.appspot.com/eval", statement=code).rstrip('\n')
|
||||
if output:
|
||||
break
|
||||
else:
|
||||
pass
|
||||
|
||||
if "Traceback (most recent call last):" in output:
|
||||
status = "Python error: "
|
||||
else:
|
||||
status = "Code executed sucessfully: "
|
||||
|
||||
if "\n" in output and paste_multiline:
|
||||
return status + haste(output)
|
||||
else:
|
||||
return output
|
Reference in a new issue