Rename some stuff in config
This commit is contained in:
parent
36e52721da
commit
5f7759eaed
@ -114,8 +114,8 @@ class ConfigManager(QObject):
|
|||||||
self._configdir = configdir
|
self._configdir = configdir
|
||||||
self._interpolation = ExtendedInterpolation()
|
self._interpolation = ExtendedInterpolation()
|
||||||
self._proxies = {}
|
self._proxies = {}
|
||||||
for secname in self.sections.keys():
|
for sectname in self.sections.keys():
|
||||||
self._proxies[secname] = SectionProxy(self, secname)
|
self._proxies[sectname] = SectionProxy(self, sectname)
|
||||||
self._from_cp(self._configparser)
|
self._from_cp(self._configparser)
|
||||||
|
|
||||||
def __getitem__(self, key):
|
def __getitem__(self, key):
|
||||||
@ -125,20 +125,20 @@ class ConfigManager(QObject):
|
|||||||
def __str__(self):
|
def __str__(self):
|
||||||
"""Get the whole config as a string."""
|
"""Get the whole config as a string."""
|
||||||
lines = configdata.FIRST_COMMENT.strip('\n').splitlines()
|
lines = configdata.FIRST_COMMENT.strip('\n').splitlines()
|
||||||
for secname, section in self.sections.items():
|
for sectname, sect in self.sections.items():
|
||||||
lines.append('\n[{}]'.format(secname))
|
lines.append('\n[{}]'.format(sectname))
|
||||||
lines += self._str_section_desc(secname)
|
lines += self._str_section_desc(sectname)
|
||||||
lines += self._str_option_desc(secname, section)
|
lines += self._str_option_desc(sectname, sect)
|
||||||
lines += self._str_items(section)
|
lines += self._str_items(sect)
|
||||||
return '\n'.join(lines) + '\n'
|
return '\n'.join(lines) + '\n'
|
||||||
|
|
||||||
def _str_section_desc(self, secname):
|
def _str_section_desc(self, sectname):
|
||||||
"""Get the section description string for secname."""
|
"""Get the section description string for sectname."""
|
||||||
wrapper = textwrap.TextWrapper(initial_indent='# ',
|
wrapper = textwrap.TextWrapper(initial_indent='# ',
|
||||||
subsequent_indent='# ',
|
subsequent_indent='# ',
|
||||||
**self._wrapper_args)
|
**self._wrapper_args)
|
||||||
lines = []
|
lines = []
|
||||||
seclines = configdata.SECTION_DESC[secname].splitlines()
|
seclines = configdata.SECTION_DESC[sectname].splitlines()
|
||||||
for secline in seclines:
|
for secline in seclines:
|
||||||
if 'http://' in secline or 'https://' in secline:
|
if 'http://' in secline or 'https://' in secline:
|
||||||
lines.append('# ' + secline)
|
lines.append('# ' + secline)
|
||||||
@ -146,15 +146,15 @@ class ConfigManager(QObject):
|
|||||||
lines += wrapper.wrap(secline)
|
lines += wrapper.wrap(secline)
|
||||||
return lines
|
return lines
|
||||||
|
|
||||||
def _str_option_desc(self, secname, section):
|
def _str_option_desc(self, sectname, sect):
|
||||||
"""Get the option description strings for section/secname."""
|
"""Get the option description strings for sect/sectname."""
|
||||||
wrapper = textwrap.TextWrapper(initial_indent='#' + ' ' * 5,
|
wrapper = textwrap.TextWrapper(initial_indent='#' + ' ' * 5,
|
||||||
subsequent_indent='#' + ' ' * 5,
|
subsequent_indent='#' + ' ' * 5,
|
||||||
**self._wrapper_args)
|
**self._wrapper_args)
|
||||||
lines = []
|
lines = []
|
||||||
if not getattr(section, 'descriptions', None):
|
if not getattr(sect, 'descriptions', None):
|
||||||
return lines
|
return lines
|
||||||
for optname, option in section.items():
|
for optname, option in sect.items():
|
||||||
lines.append('#')
|
lines.append('#')
|
||||||
if option.typ.typestr is None:
|
if option.typ.typestr is None:
|
||||||
typestr = ''
|
typestr = ''
|
||||||
@ -162,7 +162,7 @@ class ConfigManager(QObject):
|
|||||||
typestr = ' ({})'.format(option.typ.typestr)
|
typestr = ' ({})'.format(option.typ.typestr)
|
||||||
lines.append("# {}{}:".format(optname, typestr))
|
lines.append("# {}{}:".format(optname, typestr))
|
||||||
try:
|
try:
|
||||||
desc = self.sections[secname].descriptions[optname]
|
desc = self.sections[sectname].descriptions[optname]
|
||||||
except KeyError:
|
except KeyError:
|
||||||
continue
|
continue
|
||||||
for descline in desc.splitlines():
|
for descline in desc.splitlines():
|
||||||
@ -180,10 +180,10 @@ class ConfigManager(QObject):
|
|||||||
option.values['default']))
|
option.values['default']))
|
||||||
return lines
|
return lines
|
||||||
|
|
||||||
def _str_items(self, section):
|
def _str_items(self, sect):
|
||||||
"""Get the option items as string for section."""
|
"""Get the option items as string for sect."""
|
||||||
lines = []
|
lines = []
|
||||||
for optname, option in section.items():
|
for optname, option in sect.items():
|
||||||
value = option.get_first_value(startlayer='conf')
|
value = option.get_first_value(startlayer='conf')
|
||||||
for c in self.KEY_ESCAPE:
|
for c in self.KEY_ESCAPE:
|
||||||
if optname.startswith(c):
|
if optname.startswith(c):
|
||||||
@ -198,47 +198,47 @@ class ConfigManager(QObject):
|
|||||||
Args:
|
Args:
|
||||||
cp: The configparser instance to read the values from.
|
cp: The configparser instance to read the values from.
|
||||||
"""
|
"""
|
||||||
for secname in self.sections.keys():
|
for sectname in self.sections.keys():
|
||||||
if secname not in cp:
|
if sectname not in cp:
|
||||||
continue
|
continue
|
||||||
for k, v in cp[secname].items():
|
for k, v in cp[sectname].items():
|
||||||
if k.startswith(self.ESCAPE_CHAR):
|
if k.startswith(self.ESCAPE_CHAR):
|
||||||
k = k[1:]
|
k = k[1:]
|
||||||
try:
|
try:
|
||||||
self.set('conf', secname, k, v)
|
self.set('conf', sectname, k, v)
|
||||||
except ValidationError as e:
|
except ValidationError as e:
|
||||||
e.section = secname
|
e.section = sectname
|
||||||
e.option = k
|
e.option = k
|
||||||
raise
|
raise
|
||||||
|
|
||||||
def has_option(self, section, option):
|
def has_option(self, sectname, option):
|
||||||
"""Check if option exists in section.
|
"""Check if option exists in section.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
section: The section name.
|
sectname: The section name.
|
||||||
option: The option name
|
option: The option name
|
||||||
|
|
||||||
Return:
|
Return:
|
||||||
True if the option and section exist, False otherwise.
|
True if the option and section exist, False otherwise.
|
||||||
"""
|
"""
|
||||||
if section not in self.sections:
|
if sectname not in self.sections:
|
||||||
return False
|
return False
|
||||||
return option in self.sections[section]
|
return option in self.sections[sectname]
|
||||||
|
|
||||||
def remove_option(self, section, option):
|
def remove_option(self, sectname, option):
|
||||||
"""Remove an option.
|
"""Remove an option.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
section: The section where to remove an option.
|
sectname: The section where to remove an option.
|
||||||
option: The option name to remove.
|
option: The option name to remove.
|
||||||
|
|
||||||
Return:
|
Return:
|
||||||
True if the option existed, False otherwise.
|
True if the option existed, False otherwise.
|
||||||
"""
|
"""
|
||||||
try:
|
try:
|
||||||
sectdict = self.sections[section]
|
sectdict = self.sections[sectname]
|
||||||
except KeyError:
|
except KeyError:
|
||||||
raise NoSectionError(section)
|
raise NoSectionError(sectname)
|
||||||
option = self.optionxform(option)
|
option = self.optionxform(option)
|
||||||
existed = option in sectdict
|
existed = option in sectdict
|
||||||
if existed:
|
if existed:
|
||||||
@ -247,23 +247,23 @@ class ConfigManager(QObject):
|
|||||||
|
|
||||||
@cmdutils.register(name='get', instance='config',
|
@cmdutils.register(name='get', instance='config',
|
||||||
completion=['section', 'option'])
|
completion=['section', 'option'])
|
||||||
def get_wrapper(self, section, option):
|
def get_wrapper(self, sectname, option):
|
||||||
"""Get the value from a section/option.
|
"""Get the value from a section/option.
|
||||||
|
|
||||||
Wrapper for the get-command to output the value in the status bar.
|
Wrapper for the get-command to output the value in the status bar.
|
||||||
"""
|
"""
|
||||||
try:
|
try:
|
||||||
val = self.get(section, option, transformed=False)
|
val = self.get(sectname, option, transformed=False)
|
||||||
except (NoOptionError, NoSectionError) as e:
|
except (NoOptionError, NoSectionError) as e:
|
||||||
message.error("get: {} - {}".format(e.__class__.__name__, e))
|
message.error("get: {} - {}".format(e.__class__.__name__, e))
|
||||||
else:
|
else:
|
||||||
message.info("{} {} = {}".format(section, option, val))
|
message.info("{} {} = {}".format(sectname, option, val))
|
||||||
|
|
||||||
def get(self, section, option, raw=False, transformed=True):
|
def get(self, sectname, option, raw=False, transformed=True):
|
||||||
"""Get the value from a section/option.
|
"""Get the value from a section/option.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
section: The section to get the option from.
|
sectname: The section to get the option from.
|
||||||
option: The option name
|
option: The option name
|
||||||
raw: Whether to get the uninterpolated, untransformed value.
|
raw: Whether to get the uninterpolated, untransformed value.
|
||||||
transformed: Whether the value should be transformed.
|
transformed: Whether the value should be transformed.
|
||||||
@ -271,19 +271,19 @@ class ConfigManager(QObject):
|
|||||||
Return:
|
Return:
|
||||||
The value of the option.
|
The value of the option.
|
||||||
"""
|
"""
|
||||||
logging.debug("getting {} -> {}".format(section, option))
|
logging.debug("getting {} -> {}".format(sectname, option))
|
||||||
try:
|
try:
|
||||||
sect = self.sections[section]
|
sect = self.sections[sectname]
|
||||||
except KeyError:
|
except KeyError:
|
||||||
raise NoSectionError(section)
|
raise NoSectionError(sectname)
|
||||||
try:
|
try:
|
||||||
val = sect[option]
|
val = sect[option]
|
||||||
except KeyError:
|
except KeyError:
|
||||||
raise NoOptionError(option, section)
|
raise NoOptionError(option, sectname)
|
||||||
if raw:
|
if raw:
|
||||||
return val.value
|
return val.value
|
||||||
mapping = {key: val.value for key, val in sect.values.items()}
|
mapping = {key: val.value for key, val in sect.values.items()}
|
||||||
newval = self._interpolation.before_get(self, section, option,
|
newval = self._interpolation.before_get(self, sectname, option,
|
||||||
val.value, mapping)
|
val.value, mapping)
|
||||||
logging.debug("interpolated val: {}".format(newval))
|
logging.debug("interpolated val: {}".format(newval))
|
||||||
if transformed:
|
if transformed:
|
||||||
@ -292,35 +292,35 @@ class ConfigManager(QObject):
|
|||||||
|
|
||||||
@cmdutils.register(name='set', instance='config',
|
@cmdutils.register(name='set', instance='config',
|
||||||
completion=['section', 'option', 'value'])
|
completion=['section', 'option', 'value'])
|
||||||
def set_wrapper(self, section, option, value):
|
def set_wrapper(self, sectname, option, value):
|
||||||
"""Set an option.
|
"""Set an option.
|
||||||
|
|
||||||
Wrapper for self.set() to output exceptions in the status bar.
|
Wrapper for self.set() to output exceptions in the status bar.
|
||||||
"""
|
"""
|
||||||
try:
|
try:
|
||||||
self.set('conf', section, option, value)
|
self.set('conf', sectname, option, value)
|
||||||
except (NoOptionError, NoSectionError, ValidationError,
|
except (NoOptionError, NoSectionError, ValidationError,
|
||||||
ValueError) as e:
|
ValueError) as e:
|
||||||
message.error("set: {} - {}".format(e.__class__.__name__, e))
|
message.error("set: {} - {}".format(e.__class__.__name__, e))
|
||||||
|
|
||||||
@cmdutils.register(name='set_temp', instance='config',
|
@cmdutils.register(name='set_temp', instance='config',
|
||||||
completion=['section', 'option', 'value'])
|
completion=['section', 'option', 'value'])
|
||||||
def set_temp_wrapper(self, section, option, value):
|
def set_temp_wrapper(self, sectname, option, value):
|
||||||
"""Set a temporary option.
|
"""Set a temporary option.
|
||||||
|
|
||||||
Wrapper for self.set() to output exceptions in the status bar.
|
Wrapper for self.set() to output exceptions in the status bar.
|
||||||
"""
|
"""
|
||||||
try:
|
try:
|
||||||
self.set('temp', section, option, value)
|
self.set('temp', sectname, option, value)
|
||||||
except (NoOptionError, NoSectionError, ValidationError) as e:
|
except (NoOptionError, NoSectionError, ValidationError) as e:
|
||||||
message.error("set: {} - {}".format(e.__class__.__name__, e))
|
message.error("set: {} - {}".format(e.__class__.__name__, e))
|
||||||
|
|
||||||
def set(self, layer, section, option, value):
|
def set(self, layer, sectname, option, value):
|
||||||
"""Set an option.
|
"""Set an option.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
layer: A layer name as string (conf/temp/default).
|
layer: A layer name as string (conf/temp/default).
|
||||||
section: The name of the section to change.
|
sectname: The name of the section to change.
|
||||||
option: The name of the option to change.
|
option: The name of the option to change.
|
||||||
value: The new value.
|
value: The new value.
|
||||||
|
|
||||||
@ -332,22 +332,22 @@ class ConfigManager(QObject):
|
|||||||
changed: If the config was changed.
|
changed: If the config was changed.
|
||||||
style_changed: When style caches need to be invalidated.
|
style_changed: When style caches need to be invalidated.
|
||||||
"""
|
"""
|
||||||
value = self._interpolation.before_set(self, section, option, value)
|
value = self._interpolation.before_set(self, sectname, option, value)
|
||||||
try:
|
try:
|
||||||
sect = self.sections[section]
|
sect = self.sections[sectname]
|
||||||
except KeyError:
|
except KeyError:
|
||||||
raise NoSectionError(section)
|
raise NoSectionError(sectname)
|
||||||
mapping = {key: val.value for key, val in sect.values.items()}
|
mapping = {key: val.value for key, val in sect.values.items()}
|
||||||
interpolated = self._interpolation.before_get(self, section, option,
|
interpolated = self._interpolation.before_get(self, sectname, option,
|
||||||
value, mapping)
|
value, mapping)
|
||||||
try:
|
try:
|
||||||
sect.setv(layer, option, value, interpolated)
|
sect.setv(layer, option, value, interpolated)
|
||||||
except KeyError:
|
except KeyError:
|
||||||
raise NoOptionError(option, section)
|
raise NoOptionError(option, sectname)
|
||||||
else:
|
else:
|
||||||
if section in ['colors', 'fonts']:
|
if sectname in ['colors', 'fonts']:
|
||||||
self.style_changed.emit(section, option)
|
self.style_changed.emit(sectname, option)
|
||||||
self.changed.emit(section, option)
|
self.changed.emit(sectname, option)
|
||||||
|
|
||||||
@cmdutils.register(instance='config')
|
@cmdutils.register(instance='config')
|
||||||
def save(self):
|
def save(self):
|
||||||
@ -365,10 +365,10 @@ class ConfigManager(QObject):
|
|||||||
The changed config part as string.
|
The changed config part as string.
|
||||||
"""
|
"""
|
||||||
lines = []
|
lines = []
|
||||||
for secname, section in self.sections.items():
|
for sectname, sect in self.sections.items():
|
||||||
changed = section.dump_userconfig()
|
changed = sect.dump_userconfig()
|
||||||
if changed:
|
if changed:
|
||||||
lines.append('[{}]'.format(secname))
|
lines.append('[{}]'.format(sectname))
|
||||||
lines += ['{} = {}'.format(k, v) for k, v in changed]
|
lines += ['{} = {}'.format(k, v) for k, v in changed]
|
||||||
return '\n'.join(lines)
|
return '\n'.join(lines)
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user