From 08640e5b177595f5e84d4bf9194311282b1a91e2 Mon Sep 17 00:00:00 2001 From: dw-0 Date: Wed, 19 Jun 2024 20:06:45 +0200 Subject: [PATCH] Squashed 'kiauh/core/submodules/simple_config_parser/' changes from 4d60d30..2698f60 2698f60 refactor: reset state on read method call git-subtree-dir: kiauh/core/submodules/simple_config_parser git-subtree-split: 2698f600e4bef3197d696a798f2c3436dabe836a --- .../simple_config_parser.py | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/src/simple_config_parser/simple_config_parser.py b/src/simple_config_parser/simple_config_parser.py index 38cf2d9..409161c 100644 --- a/src/simple_config_parser/simple_config_parser.py +++ b/src/simple_config_parser/simple_config_parser.py @@ -111,7 +111,13 @@ class SimpleConfigParser: self.in_option_block: bool = False # whether we are in a multiline option block def read(self, file: Path) -> None: - """Read the given file and store the result in the internal state""" + """ + Read the given file and store the result in the internal state. + Call this method before using any other methods. Calling this method + multiple times will reset the internal state on each call. + """ + + self._reset_state() try: with open(file, "r") as f: @@ -120,6 +126,16 @@ class SimpleConfigParser: except OSError: raise + def _reset_state(self): + """Reset the internal state.""" + + self._config.clear() + self._header.clear() + self._all_sections.clear() + self._all_options.clear() + self.section_name = "" + self.in_option_block = False + def write(self, filename): """Write the internal state to the given file"""