Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit db163ef

Browse files
committed
Convert envar to key-value pairs
1 parent 1cfc098 commit db163ef

File tree

4 files changed

+44
-3
lines changed

4 files changed

+44
-3
lines changed

‎localstack/config.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -419,7 +419,7 @@ def in_docker():
419419
DEBUG = is_env_true("DEBUG") or LS_LOG in TRACE_LOG_LEVELS
420420

421421
# allow setting custom log levels for individual loggers
422-
LOGGING_OVERRIDE = os.environ.get("LOGGING_OVERRIDE", "{}")
422+
LOGGING_OVERRIDE = os.environ.get("LOGGING_OVERRIDE", "")
423423

424424
# whether to enable debugpy
425425
DEVELOP = is_env_true("DEVELOP")

‎localstack/logging/setup.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
import json
21
import logging
32
import sys
43
import warnings
54

65
from localstack import config, constants
76

7+
from ..utils.collections import parse_key_value_pairs
88
from .format import AddFormattedAttributes, DefaultFormatter
99

1010
# The log levels for modules are evaluated incrementally for logging granularity,
@@ -82,7 +82,7 @@ def setup_logging_from_config():
8282
raw_logging_override = config.LOGGING_OVERRIDE
8383
if raw_logging_override:
8484
try:
85-
logging_overrides = json.loads(raw_logging_override)
85+
logging_overrides = parse_key_value_pairs(raw_logging_override)
8686
for logger, level_name in logging_overrides.items():
8787
level = getattr(logging, level_name, None)
8888
if not level:

‎localstack/utils/collections.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -533,3 +533,18 @@ def is_comma_delimited_list(string: str, item_regex: Optional[str] = None) -> bo
533533
if pattern.match(string) is None:
534534
return False
535535
return True
536+
537+
538+
def parse_key_value_pairs(raw_text: str) -> dict:
539+
result = {}
540+
for pair in raw_text.split(","):
541+
items = pair.split("=", maxsplit=2)
542+
if len(items) != 2:
543+
raise ValueError(f"invalid key/value pair: '{pair}'")
544+
raw_key, raw_value = items[0].strip(), items[1].strip()
545+
if not raw_key:
546+
raise ValueError(f"missing key: '{pair}'")
547+
if not raw_value:
548+
raise ValueError(f"missing value: '{pair}'")
549+
result[raw_key] = raw_value
550+
return result

‎tests/unit/utils/test_collections.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
ImmutableList,
1010
convert_to_typed_dict,
1111
is_comma_delimited_list,
12+
parse_key_value_pairs,
1213
select_from_typed_dict,
1314
)
1415

@@ -193,3 +194,28 @@ def test_is_comma_limited_list():
193194
assert not is_comma_delimited_list("foo, bar baz")
194195
assert not is_comma_delimited_list("foo,")
195196
assert not is_comma_delimited_list("")
197+
198+
199+
@pytest.mark.parametrize(
200+
"input_text,expected",
201+
[
202+
("a=b", {"a": "b"}),
203+
("a=b,c=d", {"a": "b", "c": "d"}),
204+
],
205+
)
206+
def test_parse_key_value_pairs(input_text, expected):
207+
assert parse_key_value_pairs(input_text) == expected
208+
209+
210+
@pytest.mark.parametrize(
211+
"input_text,message",
212+
[
213+
("a=b,", "invalid key/value pair: ''"),
214+
("a=b,c=", "missing value: 'c='"),
215+
],
216+
)
217+
def test_parse_key_value_pairs_error_messages(input_text, message):
218+
with pytest.raises(ValueError) as exc_info:
219+
parse_key_value_pairs(input_text)
220+
221+
assert str(exc_info.value) == message

0 commit comments

Comments
 (0)