Add support for setting config values

This commit is contained in:
Andrew Resch 2009-04-25 21:09:07 +00:00
commit a1038203c1
3 changed files with 38 additions and 31 deletions

View file

@ -60,6 +60,15 @@ state_color = {
"Error": "{!red,black,bold!}" "Error": "{!red,black,bold!}"
} }
type_color = {
bool: "{!yellow,black,bold!}",
int: "{!green,black,bold!}",
float: "{!green,black,bold!}",
str: "{!cyan,black,bold!}",
list: "{!magenta,black,bold!}",
dict: "{!white,black,bold!}"
}
def init_colors(): def init_colors():
# Create the color_pairs dict # Create the color_pairs dict
counter = 1 counter = 1

View file

@ -96,51 +96,45 @@ class Command(BaseCommand):
for key in keys: for key in keys:
if args and key not in args: if args and key not in args:
continue continue
color = 'white' color = "{!white,black,bold!}"
value = config[key] value = config[key]
if isinstance(value, bool): if type(value) in colors.type_color:
color = 'yellow' color = colors.type_color[type(value)]
elif isinstance(value, int) or isinstance(value, float):
color = 'green' # We need to format dicts for printing
elif isinstance(value, str): if isinstance(value, dict):
color = 'cyan'
elif isinstance(value, list):
color = 'magenta'
elif isinstance(value, dict):
import pprint import pprint
value = pprint.pformat(value, 2, 80) value = pprint.pformat(value, 2, 80)
new_value = [] new_value = []
for line in value.splitlines(): for line in value.splitlines():
new_value.append("{!%s,black,bold!}%s" % (color, line)) new_value.append("%s%s" % (color, line))
value = "\n".join(new_value) value = "\n".join(new_value)
s += " %s: {!%s,black,bold!}%s\n" % (key, color, value) s += " %s: %s%s\n" % (key, color, value)
self.console.write(s) self.console.write(s)
def _set_config(self, *args, **options): def _set_config(self, *args, **options):
pass config = component.get("CoreConfig")
""" def _got_config_value(config_val):
global c_val
c_val = config_val
key = args[0] key = args[0]
if key not in config.keys():
self.console.write("{!error!}The key '%s' is invalid!" % key)
return
try: try:
val = simple_eval(' '.join(args[1:])) val = simple_eval(' '.join(args[1:]))
except SyntaxError,e: except SyntaxError, e:
print templates.ERROR(str(e)) self.console.write("{!error!}%s" % e)
return return
client.get_config_value(_got_config_value, key)
client.force_call()
if c_val is None:
print templates.ERROR("Invalid configuration name '%s'" % key)
return
if type(c_val) != type(val):
print templates.ERROR("Configuration value provided has incorrect type.")
return
client.set_config({key: val})
client.force_call()
print templates.SUCCESS("Configuration value successfully updated.")
if type(config[key]) != type(val):
self.config.write("{!error!}Configuration value provided has incorrect type.")
return
def on_set_config(result):
self.console.write("{!success!}Configuration value successfully updated.")
client.core.set_config({key: val}).addCallback(on_set_config)
"""
def complete(self, text, *args): def complete(self, text, *args):
keys = [] keys = []
def _on_get_config(config): def _on_get_config(config):

View file

@ -84,5 +84,9 @@ class EventLog(component.Component):
self.console.write("{!event!}* SessionResumed") self.console.write("{!event!}* SessionResumed")
def on_config_value_changed_event(self, key, value): def on_config_value_changed_event(self, key, value):
self.console.write("{!event!}* ConfigValueChanged: %s: %s" % color = "{!white,black,bold!}"
(key, value)) if type(value) in colors.type_color:
color = colors.type_color[type(value)]
self.console.write("{!event!}* ConfigValueChanged: {!input!}%s: %s%s" %
(key, color, value))