From 8322df1c93f533ae87abb82acca8f26bf818a001 Mon Sep 17 00:00:00 2001 From: Chris Skalenda Date: Fri, 20 Nov 2009 00:19:20 -0600 Subject: [PATCH] Adding a dotnetpad plugin. This lets you eval C# and F#, via the web. --- plugins/dotnetpad.py | 101 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 101 insertions(+) create mode 100644 plugins/dotnetpad.py diff --git a/plugins/dotnetpad.py b/plugins/dotnetpad.py new file mode 100644 index 0000000..fcd345d --- /dev/null +++ b/plugins/dotnetpad.py @@ -0,0 +1,101 @@ +"dotnetpad.py: by sklnd, because gobiner wouldn't shut up" + +import urllib +import httplib +import json + +from util import hook + +def dotnetpad(lang, code): + "Posts a provided snipit of code in a provided langugage to dotnetpad.net" + + params = urllib.urlencode({ 'language': lang + ,'code': code}) + + 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() + return 'dotnetpad is broken somehow, and returned an error' + + try: + result = json.loads(response.read()) + except ValueError: + conn.close() + return 'dotnetpad is broken somehow, and returned an error' + + conn.close() + + if len(result['Errors']) > 0: + return 'First error: %s' % (result['Errors'][0]['ErrorText']) + elif len(result['Output']) == 0: + return 'No output' + else: + return result['Output'].lstrip() + + +@hook.command +def fs(bot, input): + ".fs -- post a F# code snippit to dotnetpad.net and print the results" + + if len(input.msg) < 4: + return fs.__doc__ + + snipit = input.msg[3:] + + return dotnetpad('fsharp', snipit) + +@hook.command +def cs(bot, input): + ".cs -- post a C# code snippit to dotnetpad.net and print the results" + + if len(input.msg) < 4: + return cs.__doc__ + + snipit = input.msg[3:] + + code = '' + + fileTemplate = 'using System; ' \ + 'using System.Linq; ' \ + 'using System.Collections.Generic; ' \ + 'using System.Text; ' \ + '%(class)s' + + classTemplate = 'public class Default ' \ + '{ ' \ + ' %(main)s ' \ + '}' + + mainTemplate = 'public static void Main(String[] args) ' \ + '{ ' \ + ' %(snipit)s ' \ + '}' + + # There are probably better ways to do the following, but I'm feeling lazy + # if no main is found in the snipit, then we use the template with Main in it + if snipit.find('public static void Main') == -1: + code = mainTemplate % { 'snipit': snipit} + code = classTemplate % { 'main': code } + code = fileTemplate % { 'class' : code } + + # if Main is found, check for class and see if we need to use the classed template + elif snipit.find('class') == -1: + code = classTemplate % { 'main': snipit } + code = fileTemplate % { 'class' : code } + + return 'Error using dotnetpad' + # if we found class, then use the barebones template + else: + code = fileTemplate % { 'class' : snipit } + + return dotnetpad('csharp', code) + + + +