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). --- examples/menuconfig_example.py | 24 +++++++++++------------- 1 file changed, 11 insertions(+), 13 deletions(-) mode change 100644 => 100755 examples/menuconfig_example.py (limited to 'examples/menuconfig_example.py') diff --git a/examples/menuconfig_example.py b/examples/menuconfig_example.py old mode 100644 new mode 100755 index 4e2560d..b265e69 --- a/examples/menuconfig_example.py +++ b/examples/menuconfig_example.py @@ -1,3 +1,5 @@ +#!/usr/bin/env python + # Implements a simple configuration interface on top of Kconfiglib to # demonstrate concepts for building a menuconfig-like. Emulates how the # standard menuconfig prints menu entries. @@ -117,6 +119,7 @@ # # Enter a symbol/choice name, "load_config", or "write_config" (or press CTRL+D to exit): ^D +from __future__ import print_function import readline import sys @@ -300,27 +303,22 @@ if __name__ == "__main__": if cmd == "load_config": config_filename = input(".config file to load: ") - try: - kconf.load_config(config_filename) + # Returns a message telling which file got loaded + print(kconf.load_config(config_filename)) except IOError as e: - # Print the (spammy) error from Kconfiglib itself - print(e.message + "\n") - else: - print("Configuration loaded from " + config_filename) + print(e, file=sys.stderr) print_menuconfig(kconf) continue if cmd == "write_config": config_filename = input("To this file: ") - try: - kconf.write_config(config_filename) + # Returns a message telling which file got saved + print(kconf.write_config(config_filename)) except IOError as e: - print(e.message) - else: - print("Configuration written to " + config_filename) + print(e, file=sys.stderr) continue @@ -340,5 +338,5 @@ if __name__ == "__main__": continue - print("No symbol/choice named '{}' in the configuration" - .format(cmd)) + print("No symbol/choice named '{}' in the configuration".format(cmd), + file=sys.stderr) -- cgit v1.2.3