2009-11-20 06:19:20 +00:00
|
|
|
"dotnetpad.py: by sklnd, because gobiner wouldn't shut up"
|
|
|
|
|
|
|
|
import urllib
|
|
|
|
import httplib
|
2009-11-21 00:11:18 +00:00
|
|
|
import socket
|
2009-11-20 06:19:20 +00:00
|
|
|
import json
|
|
|
|
|
|
|
|
from util import hook
|
|
|
|
|
2009-11-21 00:11:18 +00:00
|
|
|
|
2009-11-20 06:19:20 +00:00
|
|
|
def dotnetpad(lang, code):
|
2009-11-21 00:11:18 +00:00
|
|
|
"Posts a provided snippet of code in a provided langugage to dotnetpad.net"
|
2009-11-20 06:19:20 +00:00
|
|
|
|
2009-11-21 00:11:18 +00:00
|
|
|
params = urllib.urlencode({'language': lang, 'code': code})
|
2009-11-20 06:19:20 +00:00
|
|
|
|
|
|
|
headers = {"Content-type": "application/x-www-form-urlencoded",
|
|
|
|
"Accept": "text/plain"}
|
|
|
|
|
|
|
|
try:
|
|
|
|
conn = httplib.HTTPConnection("dotnetpad.net:80")
|
|
|
|
conn.request("POST", "/Skybot", params, headers)
|
|
|
|
response = conn.getresponse()
|
|
|
|
except httplib.HTTPException:
|
|
|
|
conn.close()
|
2009-11-21 00:11:18 +00:00
|
|
|
return 'error: dotnetpad is broken somehow'
|
|
|
|
except socket.error:
|
|
|
|
return 'error: unable to connect to dotnetpad'
|
2009-11-20 06:19:20 +00:00
|
|
|
|
|
|
|
try:
|
|
|
|
result = json.loads(response.read())
|
|
|
|
except ValueError:
|
|
|
|
conn.close()
|
2009-11-21 00:11:18 +00:00
|
|
|
return 'error: dotnetpad is broken somehow'
|
2009-11-20 06:19:20 +00:00
|
|
|
|
|
|
|
conn.close()
|
|
|
|
|
2009-11-21 00:11:18 +00:00
|
|
|
if result['Errors']:
|
2009-11-20 06:19:20 +00:00
|
|
|
return 'First error: %s' % (result['Errors'][0]['ErrorText'])
|
2009-11-21 00:11:18 +00:00
|
|
|
elif result['Output']:
|
2009-11-20 06:19:20 +00:00
|
|
|
return result['Output'].lstrip()
|
2009-11-21 00:11:18 +00:00
|
|
|
else:
|
|
|
|
return 'No output'
|
2009-11-20 06:19:20 +00:00
|
|
|
|
|
|
|
|
|
|
|
@hook.command
|
2009-11-21 00:11:18 +00:00
|
|
|
def fs(inp):
|
|
|
|
".fs -- post a F# code snippet to dotnetpad.net and print the results"
|
2009-11-20 06:19:20 +00:00
|
|
|
|
2009-11-21 00:11:18 +00:00
|
|
|
if not inp:
|
2009-11-20 06:19:20 +00:00
|
|
|
return fs.__doc__
|
|
|
|
|
2009-11-21 00:11:18 +00:00
|
|
|
return dotnetpad('fsharp', inp)
|
2009-11-20 06:19:20 +00:00
|
|
|
|
|
|
|
|
|
|
|
@hook.command
|
2009-11-21 00:11:18 +00:00
|
|
|
def cs(snippet):
|
|
|
|
".cs -- post a C# code snippet to dotnetpad.net and print the results"
|
2009-11-20 06:19:20 +00:00
|
|
|
|
2009-11-21 00:11:18 +00:00
|
|
|
if not snippet:
|
2009-11-20 06:19:20 +00:00
|
|
|
return cs.__doc__
|
|
|
|
|
2009-11-21 00:11:18 +00:00
|
|
|
file_template = ('using System; '
|
|
|
|
'using System.Linq; '
|
|
|
|
'using System.Collections.Generic; '
|
|
|
|
'using System.Text; '
|
|
|
|
'%(class)s')
|
|
|
|
|
|
|
|
class_template = ('public class Default '
|
|
|
|
'{ '
|
|
|
|
' %(main)s '
|
|
|
|
'}')
|
|
|
|
|
|
|
|
main_template = ('public static void Main(String[] args) '
|
|
|
|
'{ '
|
|
|
|
' %(snippet)s '
|
|
|
|
'}')
|
2009-11-20 06:19:20 +00:00
|
|
|
|
|
|
|
# There are probably better ways to do the following, but I'm feeling lazy
|
2009-11-21 00:11:18 +00:00
|
|
|
# if no main is found in the snippet, then we use the template with Main in it
|
|
|
|
if 'public static void Main' not in snippet:
|
|
|
|
code = main_template % { 'snippet': snippet }
|
|
|
|
code = class_template % { 'main': code }
|
|
|
|
code = file_template % { 'class' : code }
|
2009-11-20 06:19:20 +00:00
|
|
|
|
|
|
|
# if Main is found, check for class and see if we need to use the classed template
|
2009-11-21 00:11:18 +00:00
|
|
|
elif 'class' not in snippet:
|
|
|
|
code = class_template % { 'main': snippet }
|
|
|
|
code = file_template % { 'class' : code }
|
2009-11-20 06:19:20 +00:00
|
|
|
|
|
|
|
return 'Error using dotnetpad'
|
|
|
|
# if we found class, then use the barebones template
|
|
|
|
else:
|
2009-11-21 00:11:18 +00:00
|
|
|
code = file_template % { 'class' : snippet }
|
2009-11-20 06:19:20 +00:00
|
|
|
|
|
|
|
return dotnetpad('csharp', code)
|