Fixed code to parse bindings for webconfig

This commit is contained in:
Siteshwar Vashisht 2014-06-08 16:02:05 +05:30
parent 8345fc2b9e
commit 7b3132d39d

View file

@ -29,6 +29,8 @@ try:
except ImportError: except ImportError:
import simplejson as json import simplejson as json
from optparse import OptionParser
FISH_BIN_PATH = False # will be set later FISH_BIN_PATH = False # will be set later
def run_fish_cmd(text): def run_fish_cmd(text):
from subprocess import PIPE from subprocess import PIPE
@ -282,11 +284,10 @@ class BindingParser:
"sleft": "Shift Left", "sright": "Shift Right" "sleft": "Shift Left", "sright": "Shift Right"
} }
def set_buffer(self, buffer, is_key=False): def set_buffer(self, buffer):
""" Sets code to parse """ """ Sets code to parse """
self.buffer = buffer self.buffer = buffer
self.is_key = is_key
self.index = 0 self.index = 0
def get_char(self): def get_char(self):
@ -356,12 +357,9 @@ class BindingParser:
def get_readable_binding(self): def get_readable_binding(self):
""" Gets a readable representation of binding """ """ Gets a readable representation of binding """
if self.is_key: try:
try: result = BindingParser.readable_keys[self.buffer]
result = BindingParser.readable_keys[self.buffer] except KeyError:
except KeyError:
result = self.buffer.title()
else:
result = self.parse_binding() result = self.parse_binding()
return result return result
@ -547,18 +545,29 @@ class FishConfigHTTPRequestHandler(SimpleHTTPServer.SimpleHTTPRequestHandler):
bindings = [] bindings = []
binding_parser = BindingParser() binding_parser = BindingParser()
for line in out.split('\n'): parser = OptionParser()
comps = line.split(' ', 2) parser.add_option("-k")
if len(comps) < 3: parser.add_option("-M")
continue parser.add_option("-m")
if comps[1] == '-k':
key_name, command = comps[2].split(' ', 1)
binding_parser.set_buffer(key_name, True)
fish_binding = FishBinding(command=command, binding=key_name, readable_binding=binding_parser.get_readable_binding())
else:
binding_parser.set_buffer(comps[1])
fish_binding = FishBinding(command=comps[2], binding=comps[1], readable_binding=binding_parser.get_readable_binding())
# Ignore any parsing errors
parser.error = lambda x: None
for line in out.split('\n'):
comps = line.split(' ', 1)
if len(comps) < 2:
continue
# parse arguments passed to bind command
bind_args_list = comps[1].split(' ', 6)
(options, args) = parser.parse_args(bind_args_list)
key_name= options.k
command = args[0]
binding_parser.set_buffer(key_name)
fish_binding = FishBinding(command=command, binding=key_name, readable_binding=binding_parser.get_readable_binding())
bindings.append(fish_binding) bindings.append(fish_binding)
return [ binding.get_json_obj() for binding in bindings ] return [ binding.get_json_obj() for binding in bindings ]