2012-03-15 10:43:45 +00:00
|
|
|
#!/usr/bin/python
|
|
|
|
|
|
|
|
import SimpleHTTPServer
|
|
|
|
import SocketServer
|
|
|
|
import webbrowser
|
|
|
|
import subprocess
|
2012-03-16 10:03:43 +00:00
|
|
|
import re, json, socket, sys
|
2012-03-15 10:43:45 +00:00
|
|
|
|
|
|
|
def run_fish_cmd(text):
|
|
|
|
from subprocess import PIPE
|
|
|
|
p = subprocess.Popen(["fish"], stdin=PIPE, stdout=PIPE, stderr=PIPE)
|
|
|
|
out, err = p.communicate(text)
|
|
|
|
return out, err
|
|
|
|
|
|
|
|
class FishConfigHTTPRequestHandler(SimpleHTTPServer.SimpleHTTPRequestHandler):
|
|
|
|
|
|
|
|
def do_get_colors(self):
|
|
|
|
"Look for fish_color_*"
|
|
|
|
result = []
|
|
|
|
out, err = run_fish_cmd('set')
|
|
|
|
for match in re.finditer(r"fish_color_(\S+) (.+)", out):
|
|
|
|
color_name, color_value = match.group(1, 2)
|
2012-03-16 10:03:43 +00:00
|
|
|
result.append([color_name.strip(), color_value.strip()])
|
2012-03-15 10:43:45 +00:00
|
|
|
print result
|
|
|
|
return result
|
2012-03-16 10:03:43 +00:00
|
|
|
|
|
|
|
def do_get_functions(self):
|
|
|
|
out, err = run_fish_cmd('functions')
|
|
|
|
out = out.strip()
|
|
|
|
|
|
|
|
# Not sure why fish sometimes returns this with newlines
|
|
|
|
if "\n" in out:
|
|
|
|
return out.split('\n')
|
|
|
|
else:
|
|
|
|
return out.strip().split(', ')
|
|
|
|
|
2012-03-15 10:43:45 +00:00
|
|
|
|
|
|
|
def do_GET(self):
|
|
|
|
p = self.path
|
|
|
|
if p == '/colors/':
|
|
|
|
output = self.do_get_colors()
|
2012-03-16 10:03:43 +00:00
|
|
|
elif p == '/functions/':
|
|
|
|
output = self.do_get_functions()
|
2012-03-15 10:43:45 +00:00
|
|
|
else:
|
|
|
|
return SimpleHTTPServer.SimpleHTTPRequestHandler.do_GET(self)
|
|
|
|
|
|
|
|
# Return valid output
|
|
|
|
self.send_response(200)
|
|
|
|
self.send_header('Content-type','text/html')
|
|
|
|
self.wfile.write('\n')
|
|
|
|
|
|
|
|
# Output JSON
|
2012-03-16 10:03:43 +00:00
|
|
|
print len(output)
|
|
|
|
print output
|
2012-03-15 10:43:45 +00:00
|
|
|
json.dump(output, self.wfile)
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-03-16 10:03:43 +00:00
|
|
|
PORT = 8000
|
|
|
|
while PORT <= 9000:
|
|
|
|
try:
|
|
|
|
Handler = FishConfigHTTPRequestHandler
|
|
|
|
httpd = SocketServer.TCPServer(("", PORT), Handler)
|
|
|
|
# Success
|
|
|
|
break;
|
|
|
|
except socket.error:
|
|
|
|
type, value = sys.exc_info()[:2]
|
|
|
|
if 'Address already in use' not in value:
|
|
|
|
break
|
|
|
|
PORT += 1
|
|
|
|
|
|
|
|
if PORT > 9000:
|
|
|
|
print "Unable to start a web server"
|
|
|
|
sys.exit(-1)
|
2012-03-15 10:43:45 +00:00
|
|
|
|
2012-03-16 10:03:43 +00:00
|
|
|
|
2012-03-15 10:43:45 +00:00
|
|
|
webbrowser.open("http://localhost:%d" % PORT)
|
|
|
|
|
|
|
|
print "serving at port", PORT
|
|
|
|
httpd.serve_forever()
|