From e058d315d43dcc60c108474a144e0381a49f9788 Mon Sep 17 00:00:00 2001 From: Benjamin Renard Date: Tue, 23 Nov 2021 12:31:33 +0100 Subject: [PATCH] config: BaseOption.set now also set parser option value --- mylib/config.py | 30 ++++++++++++++++++------------ 1 file changed, 18 insertions(+), 12 deletions(-) diff --git a/mylib/config.py b/mylib/config.py index 00eabda..b8872fb 100644 --- a/mylib/config.py +++ b/mylib/config.py @@ -90,22 +90,28 @@ class BaseOption: def set(self, value): """ Set option value to config file """ - assert self.config.config_parser, "Can set option value only if configuration file is configured" + assert self.config.config_parser or (self.config.options and not self.no_arg), ( + "Can't set option value: configuration file not configured, not options (or no argument)") if value == '': value = None - if value == self.default or value is None: - # Remove option from config - self.config.config_parser.remove_option( - self.section.name, self.name) - else: - # Store option to config - if not self.config.config_parser.has_section(self.section.name): - self.config.config_parser.add_section(self.section.name) + if self.config.config_parser: + if value == self.default or value is None: + # Remove option from config (is section exists) + if self.config.config_parser.has_section(self.section.name): + self.config.config_parser.remove_option( + self.section.name, self.name) + else: + # Store option to config + if not self.config.config_parser.has_section(self.section.name): + self.config.config_parser.add_section(self.section.name) - self.config.config_parser.set( - self.section.name, self.name, self.to_config(value) - ) + self.config.config_parser.set( + self.section.name, self.name, self.to_config(value) + ) + + if self.config.options and not self.no_arg: + setattr(self.config.options, self.parser_dest, value) @property def parser_action(self):