From 55bc8c380869ea663092212e8fe388ad7abae596 Mon Sep 17 00:00:00 2001 From: Ulf Magnusson Date: Sun, 2 Jun 2019 18:15:59 +0200 Subject: Have load_config() and write_(min_)config() return messages Hardcoding load_config() and write_(min_)config() to write any message to stdout is awkward, because it means that the message can't be easily reused when stdout is the wrong place to write it to (e.g. in menuconfig/guiconfig). This gets extra bad now that there's also the "No change to ..." message. Modify load_config() and write_(min_)config() to return the message as a string instead, and have them always return a message, instead of just when 'filename' is None and verbose=True. This makes things flexible and straightforward. Use the new behavior in menuconfig.py and guiconfig.py. They now show "No change to ..." when saving a file doesn't modify it. Tools that want to write messages to stdout should now do print(kconf.load_config()) / print(kconf.write_config()). There's no clean way to preserve perfect backwards compatibility here, but keep accepting the 'verbose' argument and print a deprecation warning if a value is ever passed for it. That way, scripts will keep running, though possibly with less output on stdout. This changes the meaning of the load_config() return value as well, though I suspect it was only ever used by the menuconfig/guiconfig interfaces. The new behavior applies for kconfiglib.VERSION >= (12, 0, 0). --- menuconfig.py | 32 +++++++++++++++++++------------- 1 file changed, 19 insertions(+), 13 deletions(-) (limited to 'menuconfig.py') diff --git a/menuconfig.py b/menuconfig.py index 3dbee95..a7f921b 100755 --- a/menuconfig.py +++ b/menuconfig.py @@ -650,12 +650,12 @@ def menuconfig(kconf): _kconf = kconf - # Load existing configuration and set _conf_changed True if it is outdated - _conf_changed = _load_config() - # Filename to save configuration to _conf_filename = standard_config_filename() + # Load existing configuration and set _conf_changed True if it is outdated + _conf_changed = _load_config() + # Filename to save minimal configuration to _minconf_filename = "defconfig" @@ -710,7 +710,8 @@ def _load_config(): # Returns True if .config is missing or outdated. We always prompt for # saving the configuration in that case. - if not _kconf.load_config(): + print(_kconf.load_config()) + if not os.path.exists(_conf_filename): # No .config return True @@ -922,8 +923,9 @@ def _quit_dialog(): return None if c == "y": - if _try_save(_kconf.write_config, _conf_filename, "configuration"): - return "Configuration saved to '{}'".format(_conf_filename) + msg = _try_save(_kconf.write_config, _conf_filename, "configuration") + if msg: + return msg elif c == "n": return "Configuration ({}) was not saved".format(_conf_filename) @@ -1858,29 +1860,33 @@ def _save_dialog(save_fn, default_filename, description): filename = os.path.expanduser(filename) - if _try_save(save_fn, filename, description): - _msg("Success", "{} saved to {}".format(description, filename)) + msg = _try_save(save_fn, filename, description) + if msg: + _msg("Success", msg) return filename def _try_save(save_fn, filename, description): - # Tries to save a configuration file. Pops up an error and returns False on - # failure. + # Tries to save a configuration file. Returns a message to print on + # success. # # save_fn: # Function to call with 'filename' to save the file # # description: # String describing the thing being saved + # + # Return value: + # A message to print on success, and None on failure try: - save_fn(filename) - return True + # save_fn() returns a message to print + return save_fn(filename) except OSError as e: _error("Error saving {} to '{}'\n\n{} (errno: {})" .format(description, e.filename, e.strerror, errno.errorcode[e.errno])) - return False + return None def _key_dialog(title, text, keys): -- cgit v1.2.3