stylistic cleanup of dotnetpad.py, untested because dotnetpad was down

This commit is contained in:
Ryan Hitchman 2009-11-20 17:11:18 -07:00
parent 90cb5bf47d
commit c04387d180
1 changed files with 42 additions and 48 deletions

View File

@ -2,15 +2,16 @@
import urllib import urllib
import httplib import httplib
import socket
import json import json
from util import hook 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 def dotnetpad(lang, code):
,'code': 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", headers = {"Content-type": "application/x-www-form-urlencoded",
"Accept": "text/plain"} "Accept": "text/plain"}
@ -21,81 +22,74 @@ def dotnetpad(lang, code):
response = conn.getresponse() response = conn.getresponse()
except httplib.HTTPException: except httplib.HTTPException:
conn.close() 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: try:
result = json.loads(response.read()) result = json.loads(response.read())
except ValueError: except ValueError:
conn.close() conn.close()
return 'dotnetpad is broken somehow, and returned an error' return 'error: dotnetpad is broken somehow'
conn.close() conn.close()
if len(result['Errors']) > 0: if result['Errors']:
return 'First error: %s' % (result['Errors'][0]['ErrorText']) return 'First error: %s' % (result['Errors'][0]['ErrorText'])
elif len(result['Output']) == 0: elif result['Output']:
return 'No output'
else:
return result['Output'].lstrip() return result['Output'].lstrip()
else:
return 'No output'
@hook.command @hook.command
def fs(bot, input): def fs(inp):
".fs -- post a F# code snippit to dotnetpad.net and print the results" ".fs -- post a F# code snippet to dotnetpad.net and print the results"
if len(input.msg) < 4: if not inp:
return fs.__doc__ return fs.__doc__
snipit = input.msg[3:] return dotnetpad('fsharp', inp)
return dotnetpad('fsharp', snipit)
@hook.command @hook.command
def cs(bot, input): def cs(snippet):
".cs -- post a C# code snippit to dotnetpad.net and print the results" ".cs -- post a C# code snippet to dotnetpad.net and print the results"
if len(input.msg) < 4: if not snippet:
return cs.__doc__ return cs.__doc__
snipit = input.msg[3:]
code = ''
fileTemplate = 'using System; ' \ file_template = ('using System; '
'using System.Linq; ' \ 'using System.Linq; '
'using System.Collections.Generic; ' \ 'using System.Collections.Generic; '
'using System.Text; ' \ 'using System.Text; '
'%(class)s' '%(class)s')
classTemplate = 'public class Default ' \ class_template = ('public class Default '
'{ ' \ '{ '
' %(main)s ' \ ' %(main)s '
'}' '}')
mainTemplate = 'public static void Main(String[] args) ' \ main_template = ('public static void Main(String[] args) '
'{ ' \ '{ '
' %(snipit)s ' \ ' %(snippet)s '
'}' '}')
# There are probably better ways to do the following, but I'm feeling lazy # 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 no main is found in the snippet, then we use the template with Main in it
if snipit.find('public static void Main') == -1: if 'public static void Main' not in snippet:
code = mainTemplate % { 'snipit': snipit} code = main_template % { 'snippet': snippet }
code = classTemplate % { 'main': code } code = class_template % { 'main': code }
code = fileTemplate % { 'class' : code } code = file_template % { 'class' : code }
# if Main is found, check for class and see if we need to use the classed template # if Main is found, check for class and see if we need to use the classed template
elif snipit.find('class') == -1: elif 'class' not in snippet:
code = classTemplate % { 'main': snipit } code = class_template % { 'main': snippet }
code = fileTemplate % { 'class' : code } code = file_template % { 'class' : code }
return 'Error using dotnetpad' return 'Error using dotnetpad'
# if we found class, then use the barebones template # if we found class, then use the barebones template
else: else:
code = fileTemplate % { 'class' : snipit } code = file_template % { 'class' : snippet }
return dotnetpad('csharp', code) return dotnetpad('csharp', code)