Commit 180509ca authored by Noé Gaumont's avatar Noé Gaumont 🐙
Browse files

feat: Add header_message in ConfigurationMixIn.generate_config

Allow helper message to be added in the beginning of the file.
This makes sure all the lines are properly commented
parent 67aafda96261
Pipeline #103374 canceled with stages
in 1 minute and 14 seconds
......@@ -33,6 +33,11 @@ py38:
extends: py3
image: python:3.8
py3:
image: python:3.7
before_script:
- pip install tox
trigger-cubicweb-pipeline:
stage: after-tests
except:
......
......@@ -658,6 +658,7 @@ class OptionsManagerMixIn(object):
stream: Union[StringIO, TextIOWrapper] = None,
skipsections: Tuple[()] = (),
encoding: Optional[Any] = None,
header_message: Optional[str] = None,
) -> None:
"""write a configuration file according to the current configuration
into the given stream or stdout
......@@ -681,6 +682,14 @@ class OptionsManagerMixIn(object):
stream = stream or sys.stdout
encoding = _get_encoding(encoding, stream)
printed = False
if header_message is not None:
# Make sure all the lines in header_message begin with '# '
# This is done by matching all the lines that do not start
# with '#' (possibly with whitespaces) and prefix them by '# '
exp = re.compile(r"^\s*(?=[^#])", re.MULTILINE)
commented_header = exp.sub("# ", header_message)
print(commented_header, file=stream)
for section in sections:
if printed:
print("\n", file=stream)
......
......@@ -267,6 +267,22 @@ diffgroup=pouet
#opt-b-2=""",
)
def test_generate_config_header(self):
header_message = """This a multiline header
All lines should be commented.
Everything should be at the top of the file."""
commented_header_message = """# This a multiline header
# All lines should be commented.
# Everything should be at the top of the file."""
exepected_stream = StringIO()
print(commented_header_message, file=exepected_stream)
self.cfg.generate_config(exepected_stream)
stream = StringIO()
self.cfg.generate_config(stream, header_message=header_message)
self.assertMultiLineEqual(stream.getvalue().strip(), exepected_stream.getvalue().strip())
def test_generate_config_with_space_string(self):
self.cfg["value"] = " "
stream = StringIO()
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment