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