From c04387d180ed0e94fa18fbde6db257dfa033c148 Mon Sep 17 00:00:00 2001 From: Ryan Hitchman Date: Fri, 20 Nov 2009 17:11:18 -0700 Subject: [PATCH] stylistic cleanup of dotnetpad.py, untested because dotnetpad was down --- plugins/dotnetpad.py | 90 +++++++++++++++++++++----------------------- 1 file changed, 42 insertions(+), 48 deletions(-) diff --git a/plugins/dotnetpad.py b/plugins/dotnetpad.py index fcd345d..95c7fd5 100644 --- a/plugins/dotnetpad.py +++ b/plugins/dotnetpad.py @@ -2,15 +2,16 @@ import urllib import httplib +import socket 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}) +def dotnetpad(lang, code): + "Posts a provided snippet 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"} @@ -21,81 +22,74 @@ def dotnetpad(lang, code): response = conn.getresponse() except httplib.HTTPException: conn.close() - return 'dotnetpad is broken somehow, and returned an error' + 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 'dotnetpad is broken somehow, and returned an error' + return 'error: dotnetpad is broken somehow' conn.close() - if len(result['Errors']) > 0: + if result['Errors']: return 'First error: %s' % (result['Errors'][0]['ErrorText']) - elif len(result['Output']) == 0: - return 'No output' - else: + elif result['Output']: return result['Output'].lstrip() + else: + return 'No output' @hook.command -def fs(bot, input): - ".fs -- post a F# code snippit to dotnetpad.net and print the results" +def fs(inp): + ".fs -- post a F# code snippet to dotnetpad.net and print the results" - if len(input.msg) < 4: + if not inp: return fs.__doc__ - snipit = input.msg[3:] + return dotnetpad('fsharp', inp) - return dotnetpad('fsharp', snipit) @hook.command -def cs(bot, input): - ".cs -- post a C# code snippit to dotnetpad.net and print the results" +def cs(snippet): + ".cs -- post a C# code snippet to dotnetpad.net and print the results" - if len(input.msg) < 4: + if not snippet: return cs.__doc__ - - snipit = input.msg[3:] - - code = '' - fileTemplate = 'using System; ' \ - 'using System.Linq; ' \ - 'using System.Collections.Generic; ' \ - 'using System.Text; ' \ - '%(class)s' + file_template = ('using System; ' + 'using System.Linq; ' + 'using System.Collections.Generic; ' + 'using System.Text; ' + '%(class)s') - classTemplate = 'public class Default ' \ - '{ ' \ - ' %(main)s ' \ - '}' + class_template = ('public class Default ' + '{ ' + ' %(main)s ' + '}') - mainTemplate = 'public static void Main(String[] args) ' \ - '{ ' \ - ' %(snipit)s ' \ - '}' + main_template = ('public static void Main(String[] args) ' + '{ ' + ' %(snippet)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 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 } # 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 } + elif 'class' not in snippet: + code = class_template % { 'main': snippet } + code = file_template % { 'class' : code } return 'Error using dotnetpad' # if we found class, then use the barebones template else: - code = fileTemplate % { 'class' : snipit } + code = file_template % { 'class' : snippet } return dotnetpad('csharp', code) - - - -