Commit eac9b7b8 authored by Michael Hanselmann's avatar Michael Hanselmann

Add utility to format dictionary as key=value strings

This will be used in QA to format network interface parameters.
Signed-off-by: default avatarMichael Hanselmann <hansmi@google.com>
Reviewed-by: default avatarGuido Trotter <ultrotter@google.com>
parent aa6c1bb1
......@@ -654,3 +654,15 @@ def FilterEmptyLinesAndComments(text):
return [line for line in map(lambda s: s.strip(), text.splitlines())
# Ignore empty lines and comments
if line and not line.startswith("#")]
def FormatKeyValue(data):
"""Formats a dictionary as "key=value" parameters.
The keys are sorted to have a stable order.
@type data: dict
@rtype: list of string
"""
return ["%s=%s" % (key, value) for (key, value) in sorted(data.items())]
......@@ -639,5 +639,16 @@ class TestFilterEmptyLinesAndComments(unittest.TestCase):
])
class TestFormatKeyValue(unittest.TestCase):
def test(self):
self.assertEqual(utils.FormatKeyValue({}), [])
self.assertEqual(utils.FormatKeyValue({1: 2}), ["1=2"])
self.assertEqual(utils.FormatKeyValue({
"zzz": "0",
"aaa": "1",
}),
["aaa=1", "zzz=0"])
if __name__ == "__main__":
testutils.GanetiTestProgram()
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