Add command descriptions to config file generation

This commit is contained in:
Florian Bruhin 2014-02-27 19:22:46 +01:00
parent e4cd92a293
commit 277f4d841d
3 changed files with 34 additions and 8 deletions

View File

@ -57,6 +57,7 @@ class NewConfig:
def __init__(self):
self.config = configdata.configdata()
print(str(self))
def __getitem__(self, key):
"""Get a section from the config."""
@ -65,21 +66,39 @@ class NewConfig:
def __str__(self):
"""Get the whole config as a string."""
# FIXME empty lines get discared
# FIXME we should set subsequent_indent for config options later
wrapper = textwrap.TextWrapper(
normal_wrapper = textwrap.TextWrapper(
width=72, replace_whitespace=False, break_long_words=False,
break_on_hyphens=False, initial_indent='# ',
subsequent_indent='# ')
option_wrapper = textwrap.TextWrapper(
width=72, replace_whitespace=False, break_long_words=False,
break_on_hyphens=False, initial_indent='# ',
subsequent_indent='# ')
lines = []
for par in map(wrapper.wrap, configdata.FIRST_COMMENT.splitlines()):
for par in map(normal_wrapper.wrap,
configdata.FIRST_COMMENT.splitlines()):
lines += par
for secname, section in self.config.items():
lines.append('\n[{}]'.format(secname))
for par in map(wrapper.wrap,
configdata.SECTION_DESC[secname].splitlines()):
lines += par
lines.append('\n\n[{}]\n'.format(secname))
seclines = configdata.SECTION_DESC[secname].splitlines()
for secline in seclines:
if 'http://' in secline:
lines.append('# ' + secline)
else:
lines += normal_wrapper.wrap(secline)
lines.append('')
for optname, option in section.items():
# FIXME display option comment
try:
desc = self.config[secname].descriptions[optname]
except KeyError:
continue
wrapped_desc = []
for descline in desc.splitlines():
if 'http://' in descline:
wrapped_desc.append('# ' + descline)
else:
wrapped_desc += option_wrapper.wrap(descline)
lines.append('\n'.join(wrapped_desc))
lines.append('{} = {}'.format(optname, option))
return '\n'.join(lines)

View File

@ -34,6 +34,7 @@ class KeyValue:
values: An OrderedDict with key as index and value as value.
key: string
value: SettingValue
descriptions: A dict with the description strings for the keys.
"""
@ -47,9 +48,11 @@ class KeyValue:
"""
if args:
self.descriptions = {}
self.values = OrderedDict()
for (k, settingval, desc) in args:
self.values[k] = settingval
self.descriptions[k] = desc
def __getitem__(self, key):
"""Get the value for key.

View File

@ -84,12 +84,16 @@ class ValueListSection:
default: An OrderedDict with the default configuration as strings.
After __init__, the strings become key/value types.
types: A tuple for (keytype, valuetype)
descriptions: A dict with the description strings for the keys.
Currently a global empty dict to be compatible with
KeyValue section.
"""
values = None
default = None
types = None
descriptions = {}
def __init__(self):
"""Wrap types over default values. Take care when overriding this."""