From 51928b0075bf03988544d36cadcceb7c5d406e39 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9gis=20Behmo?= Date: Wed, 6 Sep 2023 10:52:02 +0200 Subject: [PATCH] fix: parse strings prefixed with "#" in `config save --set ...` Pound keys were interpreted as comments. This is annoying when we want to parse html color codes, such as in: $ tutor config save --set "INDIGO_PRIMARY_COLOR=#225522" $ tutor config printvalue INDIGO_PRIMARY_COLOR null Close #866. --- .../20230906_105044_regis_parse_pound_keys_in_settings.md | 1 + tests/test_serialize.py | 4 ++++ tutor/serialize.py | 4 ++++ 3 files changed, 9 insertions(+) create mode 100644 changelog.d/20230906_105044_regis_parse_pound_keys_in_settings.md diff --git a/changelog.d/20230906_105044_regis_parse_pound_keys_in_settings.md b/changelog.d/20230906_105044_regis_parse_pound_keys_in_settings.md new file mode 100644 index 0000000000..631de93168 --- /dev/null +++ b/changelog.d/20230906_105044_regis_parse_pound_keys_in_settings.md @@ -0,0 +1 @@ +- [Bugfix] Correctly parse strings prefixed with pound "#" key in `tutor config save --set KEY=#value` commands. (by @regisb) diff --git a/tests/test_serialize.py b/tests/test_serialize.py index 874675854b..0923b15e8c 100644 --- a/tests/test_serialize.py +++ b/tests/test_serialize.py @@ -41,6 +41,10 @@ def test_parse_key_value(self) -> None: "x=key1:\n subkey: value\nkey2:\n subkey: value" ), ) + self.assertEqual( + ("INDIGO_PRIMARY_COLOR", "#225522"), + serialize.parse_key_value("INDIGO_PRIMARY_COLOR=#225522"), + ) def test_str_format(self) -> None: self.assertEqual("true", serialize.str_format(True)) diff --git a/tutor/serialize.py b/tutor/serialize.py index 0f4ae6cf1f..7d77b8349a 100644 --- a/tutor/serialize.py +++ b/tutor/serialize.py @@ -73,4 +73,8 @@ def parse_key_value(text: str) -> t.Optional[tuple[str, t.Any]]: if not value: # Empty strings are interpreted as null values, which is incorrect. value = "''" + elif "\n" not in value and value.startswith("#"): + # Single-line string that starts with a pound # key + # We need to escape the string, otherwise pound will be interpreted as a comment. + value = f'"{value}"' return key, parse(value)