Skip to content

Commit 5548d69

Browse files
author
Jonathan Goetzinger
committed
dictionary_functions: function to create dictionary from command output
In many testcases it is desired to convert the output of a command into a dictionary. The function "split_output_to_dict" executes a command with a given command protocol and generates a dictionary from the output. Signed-off-by: Jonathan Goetzinger <[email protected]>
1 parent e5544ef commit 5548d69

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

labgridhelper/dictionary_functions.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
def split_output_to_dict(command_protocoll, command, delimiter="=", strip_chars=""):
2+
'''
3+
returns a dictionary that contains the results of the command, where the key
4+
is the part before and the value is the part after the first delimiter optional
5+
chars can be specified to be stript from the value
6+
Args:
7+
command_protocoll: command_protocoll to execute command (shell, barebox, ...)
8+
command: command to execute
9+
delimiter: character that seperates key and value
10+
strip_chars: characters to strip from key and value
11+
Returns:
12+
output_dict: dictionary that contains the pairs of key and value
13+
'''
14+
command_output = command_protocoll.run_check(command)
15+
output_dict = {}
16+
for line in command_output:
17+
try:
18+
if line.count(delimiter) >= 1: # lines without a delimiter are ignored
19+
(key, value) = line.split(delimiter, 1)
20+
output_dict[key.strip(strip_chars)] = value.strip(strip_chars)
21+
except ValueError:
22+
pass
23+
return output_dict

0 commit comments

Comments
 (0)