From c54db8e7c9cde50a2b13cebe256cfd907b45d762 Mon Sep 17 00:00:00 2001 From: Ulf Magnusson Date: Fri, 29 Sep 2017 16:26:05 +0200 Subject: .config header parsing nits Rearrange a bit and document that a trailing newline does not need to be added. Clean up the tests a bit too. --- kconfiglib.py | 25 ++++++++++++++----------- testsuite.py | 11 ++++------- 2 files changed, 18 insertions(+), 18 deletions(-) diff --git a/kconfiglib.py b/kconfiglib.py index ffb3ce6..25fedd7 100644 --- a/kconfiglib.py +++ b/kconfiglib.py @@ -267,12 +267,10 @@ class Config(object): """Returns the (uncommented) textual header of the .config file most recently loaded with load_config(). Returns None if no .config file has been loaded or if the most recently loaded .config file has no header. - The header consists of all lines up to but not including the first line - that either - 1. Does not start with "#" - 2. Has the form "# CONFIG_FOO is not set." - """ + The header consists of all lines up to but not including the first line + that either (1) does not begin with "#", or (2) matches + "# CONFIG_FOO is not set".""" return self._config_header def get_mainmenu_text(self): @@ -422,14 +420,18 @@ class Config(object): return line is not None and line.startswith("#") and \ not unset_re_match(line) - self._config_header = None - - line = line_feeder.peek_next() - if is_header_line(line): + if not is_header_line(line_feeder.peek_next()): + self._config_header = None + else: + # Kinda inefficient, but this is an unlikely hotspot self._config_header = "" while is_header_line(line_feeder.peek_next()): self._config_header += line_feeder.get_next()[1:] - # Remove trailing newline + # Makes c.write_config(".config", c.get_config_header()) preserve + # the header exactly. We also handle weird cases like a .config + # file with just "# foo" and no trailing newline in it (though we + # would never generate that ourselves), hence the slight + # awkwardness. if self._config_header.endswith("\n"): self._config_header = self._config_header[:-1] @@ -513,7 +515,8 @@ class Config(object): header (default: None): A textual header that will appear at the beginning of the file, with each line commented out automatically. - None means no header.""" + Does not need to include a trailing newline. None means no + header.""" # Symbol._already_written is set to True when _add_config_strings() is # called on a symbol, so that symbols defined in multiple locations diff --git a/testsuite.py b/testsuite.py index 53f0159..3a6886f 100644 --- a/testsuite.py +++ b/testsuite.py @@ -1651,15 +1651,12 @@ def run_selftests(): config_test_file = "Kconfiglib/tests/config_test" - def verify_header(config_name, header): - c.load_config(config_name) - verify(c.get_config_header() == header, - "Expected the header '{}' from '{}', got the header '{}'.". - format(header, config_name, c.get_config_header())) - def write_and_verify_header(header): c.write_config(config_test_file, header) - verify_header(config_test_file, header) + c.load_config(config_test_file) + verify(c.get_config_header() == header, + "The header {} morphed into {} on loading" + .format(repr(header), repr(c.get_config_header()))) def verify_file_contents(fname, contents): with open(fname, "r") as f: -- cgit v1.2.3