Skip to content

Commit fec7eaa

Browse files
author
Dan Lavu
committed
adding delimiter_parser function that has a configurable delimiter
1 parent 9c026fb commit fec7eaa

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

sssd_test_framework/misc/__init__.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,26 @@ def attrs_parse(lines: list[str], attrs: list[str] | None = None) -> dict[str, l
4444
return out
4545

4646

47+
def delimiter_parse(lines: list[Any], delimiter: str = ":") -> dict[str, str]:
48+
"""
49+
Parse delimited lines from output.
50+
51+
:param lines: Output.
52+
:type lines: list[str]
53+
:param delimiter: Delimiter, optional
54+
:type delimiter: str, defaults to :
55+
:return: Dictionary with first element as the name as a key.
56+
:rtype: dict[str, str]
57+
"""
58+
out: dict[str, str] = {}
59+
60+
for item in lines:
61+
key, value = item.split(delimiter, 1)
62+
out[key.strip()] = value.strip()
63+
64+
return out
65+
66+
4767
def attrs_include_value(attr: Any | list[Any] | None, value: Any) -> list[Any]:
4868
"""
4969
Include ``value`` to attribute list if it is not yet present.

tests/test_misc.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
attrs_include_value,
1212
attrs_parse,
1313
attrs_to_hash,
14+
delimiter_parse,
1415
get_attr,
1516
ip_to_ptr,
1617
ip_version,
@@ -86,6 +87,25 @@ def test_attrs_parse__long_line(input, expected):
8687
assert attrs_parse(input) == expected
8788

8889

90+
@pytest.mark.parametrize(
91+
"input,expected, delimiter",
92+
[
93+
(
94+
["Unique ID: 5fe04e66-da53-4ac0-94f3-fd0cd5cefd6d", "Owner: user1"],
95+
{"Unique ID": "5fe04e66-da53-4ac0-94f3-fd0cd5cefd6d", "Owner": "user1"},
96+
":",
97+
),
98+
(
99+
["Unique ID, 5fe04e66-da53-4ac0-94f3-fd0cd5cefd6d", "Owner, user1"],
100+
{"Unique ID": "5fe04e66-da53-4ac0-94f3-fd0cd5cefd6d", "Owner": "user1"},
101+
",",
102+
),
103+
],
104+
)
105+
def test_delimiter_parse(input, expected, delimiter):
106+
assert delimiter_parse(input, delimiter=delimiter) == expected
107+
108+
89109
@pytest.mark.parametrize(
90110
"input,expected",
91111
[

0 commit comments

Comments
 (0)