diff --git a/src/simple_config_parser/simple_config_parser.py b/src/simple_config_parser/simple_config_parser.py index 13256eb..2b6413d 100644 --- a/src/simple_config_parser/simple_config_parser.py +++ b/src/simple_config_parser/simple_config_parser.py @@ -192,11 +192,24 @@ class SimpleConfigParser: self.config[section] = {"_raw": f"[{section}]\n"} def _check_set_section_spacing(self): - prev_section: str = self.get_sections()[-1] - prev_section_content: Dict = self.config[prev_section] - last_item: str = list(prev_section_content.keys())[-1] - if last_item.startswith("#_") and prev_section_content[last_item][-1] != "\n": - prev_section_content[last_item].append("\n") + prev_section_name: str = self.get_sections()[-1] + prev_section_content: Dict = self.config[prev_section_name] + last_option_name: str = list(prev_section_content.keys())[-1] + + if last_option_name.startswith("#_"): + last_elem: str = prev_section_content[last_option_name][-1] + + # if the last section is a collector, we first check if the last element + # in the collector ends with a newline. if it does not, we append a newline. + # this can happen if the config file does not end with a newline. + if not last_elem.endswith("\n"): + last_elem = f"{last_elem}\n" + + # if the last item in a collector is not a newline, we append a newline, so + # that the new section is seperated from the options of the previous section + # by a newline + if last_elem != "\n": + prev_section_content[last_option_name].append("\n") else: prev_section_content[self._generate_rand_id()] = ["\n"]