Skip to content

Commit

Permalink
fix: parse strings prefixed with "#" in config save --set ...
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
regisb committed Sep 6, 2023
1 parent 8eccaa6 commit 51928b0
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- [Bugfix] Correctly parse strings prefixed with pound "#" key in `tutor config save --set KEY=#value` commands. (by @regisb)
4 changes: 4 additions & 0 deletions tests/test_serialize.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
4 changes: 4 additions & 0 deletions tutor/serialize.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

0 comments on commit 51928b0

Please sign in to comment.