From 985794fbcb7ce4fbcd07d0ea63bb1ab2f4c58f70 Mon Sep 17 00:00:00 2001 From: A_D Date: Fri, 7 May 2021 12:58:10 +0200 Subject: [PATCH] Ensure that Section exists on linuxconf When starting EDMC for the first time, linuxconf creates the required file automatically but does _not_ create our config section. This causes a keyerror on any access to config. Simple solution, test that the section exists, if it doesnt, create it (and backup any existing file beforehand) --- config.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/config.py b/config.py index 8eafbe5d..1a388fce 100644 --- a/config.py +++ b/config.py @@ -843,6 +843,16 @@ class LinuxConfig(AbstractConfig): logger.debug(f'Error occurred while reading in file. Assuming that we are creating a new one: {e}') self.config.add_section(self.SECTION) + # Ensure that our section exists + try: + self.config[self.SECTION].get("this_does_not_exist", fallback=None) + except KeyError: + logger.info("Config section not found. Backing up existing file (if any) and readding a section header") + if self.filename.exists(): + (self.filename.parent / f'{appname}.ini.backup').write_bytes(self.filename.read_bytes()) + + self.config.add_section(self.SECTION) + if (outdir := self.get_str('outdir')) is None or not pathlib.Path(outdir).is_dir(): self.set('outdir', self.home)