Clean up ConfigManager._from_cp.

This commit is contained in:
Florian Bruhin 2014-12-15 22:25:06 +01:00
parent 3c68506665
commit 0d9c7049b6

View File

@ -341,6 +341,33 @@ class ConfigManager(QObject):
lines.append(keyval) lines.append(keyval)
return lines return lines
def _get_real_sectname(self, cp, sectname):
"""Get an old or new section name based on a configparser.
This checks if sectname is in cp, and if not, migrates it if needed and
tries again.
Args:
cp: The configparser to check.
sectname: The new section name.
Returns:
The section name in the configparser as a string, or None if the
configparser doesn't contain the section.
"""
reverse_renamed_sections = {v: k for k, v in
self.RENAMED_SECTIONS.items()}
if sectname in reverse_renamed_sections:
old_sectname = reverse_renamed_sections[sectname]
else:
old_sectname = sectname
if old_sectname in cp:
return old_sectname
elif sectname in cp:
return sectname
else:
return None
def _from_cp(self, cp): def _from_cp(self, cp):
"""Read the config from a configparser instance. """Read the config from a configparser instance.
@ -354,17 +381,8 @@ class ConfigManager(QObject):
raise UnknownSectionError("Unknown section '{}'!".format( raise UnknownSectionError("Unknown section '{}'!".format(
sectname)) sectname))
for sectname in self.sections: for sectname in self.sections:
reverse_renamed_sections = {v: k for k, v in real_sectname = self._get_real_sectname(cp, sectname)
self.RENAMED_SECTIONS.items()} if real_sectname is None:
if sectname in reverse_renamed_sections:
old_sectname = reverse_renamed_sections[sectname]
else:
old_sectname = sectname
if old_sectname in cp:
real_sectname = old_sectname
elif sectname in cp:
real_sectname = sectname
else:
continue continue
for k, v in cp[real_sectname].items(): for k, v in cp[real_sectname].items():
if k.startswith(self.ESCAPE_CHAR): if k.startswith(self.ESCAPE_CHAR):