CLI: Get/set configuration values (bug 1549)
This commit is contained in:
parent
0d33abf473
commit
5ce15b38c2
47
bin/gpo
47
bin/gpo
|
@ -51,6 +51,10 @@
|
|||
pending [URL] List new episodes (all or only from URL)
|
||||
episodes [URL] List episodes (all or only from URL)
|
||||
|
||||
- Configuration -
|
||||
|
||||
set [key] [value] List one (all) keys or set to a new value
|
||||
|
||||
- Other commands -
|
||||
|
||||
youtube URL Resolve the YouTube URL to a download URL
|
||||
|
@ -130,15 +134,10 @@ interactive_console = sys.stdin.isatty() and sys.stdout.isatty()
|
|||
is_single_command = False
|
||||
|
||||
def incolor(color_id, s):
|
||||
if have_ansi and incolor.want_colors:
|
||||
if have_ansi and cli.config.ui.cli.colors:
|
||||
return '\033[9%dm%s\033[0m' % (color_id, s)
|
||||
return s
|
||||
|
||||
# Add an attribute to the function to control its behavior - this avoids
|
||||
# setting another global variable, as "want_colors" is only relevant for
|
||||
# this single function in our case.
|
||||
incolor.want_colors = True
|
||||
|
||||
# ANSI Colors: red = 1, green = 2, yellow = 3, blue = 4
|
||||
inred, ingreen, inyellow, inblue = (functools.partial(incolor, x)
|
||||
for x in range(1, 5))
|
||||
|
@ -166,9 +165,6 @@ class gPodderCli(object):
|
|||
self.client = api.PodcastClient()
|
||||
self.config = self.client._config
|
||||
|
||||
# Load configuration settings into our application state
|
||||
incolor.want_colors = self.config.ui.cli.colors
|
||||
|
||||
self._current_action = ''
|
||||
self._commands = dict((name.rstrip('_'), func)
|
||||
for name, func in inspect.getmembers(self)
|
||||
|
@ -283,6 +279,39 @@ class gPodderCli(object):
|
|||
self._info(_('Successfully added %s.' % url))
|
||||
return True
|
||||
|
||||
def _print_config(self, search_for):
|
||||
for key in self.config.all_keys():
|
||||
if search_for is None or search_for in key:
|
||||
print key, '=', repr(self.config._lookup(key))
|
||||
|
||||
def set(self, key=None, value=None):
|
||||
if value is None:
|
||||
self._print_config(key)
|
||||
return
|
||||
|
||||
try:
|
||||
current_value = self.config._lookup(key)
|
||||
current_type = type(current_value)
|
||||
except KeyError:
|
||||
self._error(_('This configuration option does not exist.'))
|
||||
return
|
||||
|
||||
if current_type == dict:
|
||||
self._error(_('Can only set leaf configuration nodes.'))
|
||||
return
|
||||
|
||||
try:
|
||||
if current_type == bool:
|
||||
new_value = (value in ('1', 'true', 'True'))
|
||||
else:
|
||||
new_value = current_type(value)
|
||||
except ValueError:
|
||||
self._error(_('Wrong value type for this configuration option.'))
|
||||
return
|
||||
|
||||
self.config.update_field(key, new_value)
|
||||
self.set(key)
|
||||
|
||||
@FirstArgumentIsPodcastURL
|
||||
def rename(self, url, title):
|
||||
podcast = self.client.get_podcast(url)
|
||||
|
|
|
@ -232,6 +232,9 @@ class Config(object):
|
|||
else:
|
||||
logger.warn('Observer not added: %s', repr(callback))
|
||||
|
||||
def all_keys(self):
|
||||
return self.__json_config._keys_iter()
|
||||
|
||||
def schedule_save(self):
|
||||
if self.__save_thread is None:
|
||||
self.__save_thread = threading.Thread(target=self.save_thread_proc)
|
||||
|
|
|
@ -179,6 +179,18 @@ class JsonConfig(object):
|
|||
def _lookup(self, name):
|
||||
return reduce(lambda d, k: d[k], name.split('.'), self._data)
|
||||
|
||||
def _keys_iter(self):
|
||||
work_queue = []
|
||||
work_queue.append(([], self._data))
|
||||
while work_queue:
|
||||
path, data = work_queue.pop(0)
|
||||
|
||||
if isinstance(data, dict):
|
||||
for key in sorted(data.keys()):
|
||||
work_queue.append((path + [key], data[key]))
|
||||
else:
|
||||
yield '.'.join(path)
|
||||
|
||||
def __getattr__(self, name):
|
||||
try:
|
||||
value = self._lookup(name)
|
||||
|
|
Loading…
Reference in New Issue