Fix setting config values in the console ui including setting paths with

spaces in them
This commit is contained in:
Andrew Resch 2010-04-18 18:07:26 -07:00
parent 9a632fc3d3
commit 61dd9a5589
4 changed files with 48 additions and 79 deletions

View File

@ -58,32 +58,18 @@ def atom(next, token):
if token[1] == ",":
token = next()
return tuple(out)
elif token[0] is tokenize.STRING:
return token[1][1:-1].decode("string-escape")
elif token[1] == "/":
count = token[-1].count("/")
# Check for a trailing / since it messes things up
trail = False
if token[-1][-1] == "/":
count -= 1
trail = True
for i in xrange(count * 2 - 1):
token = next()
# Check for trailing / and remove it
path = token[-1].decode("string-escape")
if trail:
path = path[0:-1]
token = next()
return path
elif token[0] is tokenize.NUMBER:
elif token[0] is tokenize.NUMBER or token[1] == "-":
try:
return int(token[1], 0)
return int(token[-1], 0)
except ValueError:
return float(token[1])
return float(token[-1])
elif token[1].lower() == 'true':
return True
elif token[1].lower() == 'false':
return False
elif token[0] is tokenize.STRING or token[1] == "/":
return token[-1].decode("string-escape")
raise SyntaxError("malformed expression (%s)" % token[1])
def simple_eval(source):
@ -93,8 +79,6 @@ def simple_eval(source):
src = tokenize.generate_tokens(src)
src = (token for token in src if token[0] is not tokenize.NL)
res = atom(src.next, src.next())
if src.next()[0] is not tokenize.ENDMARKER:
raise SyntaxError("bogus data after expression")
return res
@ -117,44 +101,38 @@ class Command(BaseCommand):
def _get_config(self, *args, **options):
deferred = defer.Deferred()
def on_get_config(result):
config = component.get("CoreConfig")
keys = config.keys()
keys.sort()
s = ""
for key in keys:
if args and key not in args:
continue
color = "{!white,black,bold!}"
value = config[key]
if type(value) in colors.type_color:
color = colors.type_color[type(value)]
config = component.get("CoreConfig")
keys = config.keys()
keys.sort()
s = ""
for key in keys:
if args and key not in args:
continue
color = "{!white,black,bold!}"
value = config[key]
if type(value) in colors.type_color:
color = colors.type_color[type(value)]
# We need to format dicts for printing
if isinstance(value, dict):
import pprint
value = pprint.pformat(value, 2, 80)
new_value = []
for line in value.splitlines():
new_value.append("%s%s" % (color, line))
value = "\n".join(new_value)
# We need to format dicts for printing
if isinstance(value, dict):
import pprint
value = pprint.pformat(value, 2, 80)
new_value = []
for line in value.splitlines():
new_value.append("%s%s" % (color, line))
value = "\n".join(new_value)
s += " %s: %s%s\n" % (key, color, value)
s += " %s: %s%s\n" % (key, color, value)
self.console.write(s)
deferred.callback(True)
return config
# We need to ensure the config dict has been received first
component.get("CoreConfig").start_defer.addCallback(on_get_config)
return deferred
self.console.write(s)
return config
def _set_config(self, *args, **options):
deferred = defer.Deferred()
config = component.get("CoreConfig")
key = options["set"][0]
val = options["set"][1]
val = simple_eval(options["set"][1] + " " + " ".join(args))
if key not in config.keys():
self.console.write("{!error!}The key '%s' is invalid!" % key)
return
@ -170,6 +148,7 @@ class Command(BaseCommand):
self.console.write("{!success!}Configuration value successfully updated.")
deferred.callback(True)
self.console.write("Setting %s to %s.." % (key, val))
client.core.set_config({key: val}).addCallback(on_set_config)
return deferred

View File

@ -163,23 +163,24 @@ class ConsoleUI(component.Component):
# Try to connect to the localhost daemon
def on_connect(result):
component.start()
if not self.interactive:
def on_started(result):
deferreds = []
# If we have args, lets process them and quit
# allow multiple commands split by ";"
for arg in args.split(";"):
deferreds.append(defer.maybeDeferred(self.do_command, arg.strip()))
def on_started(result):
if not self.interactive:
def on_started(result):
deferreds = []
# If we have args, lets process them and quit
# allow multiple commands split by ";"
for arg in args.split(";"):
deferreds.append(defer.maybeDeferred(self.do_command, arg.strip()))
def on_complete(result):
self.do_command("quit")
def on_complete(result):
self.do_command("quit")
dl = defer.DeferredList(deferreds).addCallback(on_complete)
dl = defer.DeferredList(deferreds).addCallback(on_complete)
# We need to wait for the rpcs in start() to finish before processing
# any of the commands.
self.started_deferred.addCallback(on_started)
# We need to wait for the rpcs in start() to finish before processing
# any of the commands.
self.started_deferred.addCallback(on_started)
component.start().addCallback(on_started)
d = client.connect()
d.addCallback(on_connect)

View File

@ -49,21 +49,10 @@ class StatusBars(component.Component):
self.upload = ""
self.dht = 0
# This lets us know when the CoreConfig component is ready
self.__core_config_ready = False
def start(self):
def on_coreconfig_ready(result):
self.__core_config_ready = True
self.update()
# We need to add a callback to wait for the CoreConfig to be ready
self.config.start_defer.addCallback(on_coreconfig_ready)
self.update()
def update(self):
if not self.__core_config_ready:
return
def on_get_num_connections(result):
self.connections = result
client.core.get_num_connections().addCallback(on_get_num_connections)

View File

@ -52,7 +52,7 @@ class CoreConfig(component.Component):
self.config = config
return config
self.start_defer = client.core.get_config().addCallback(on_get_config)
return client.core.get_config().addCallback(on_get_config)
def stop(self):
self.config = {}