Skip to content

Commit 6a0c746

Browse files
authored
Merge pull request #1 from jgoetzinger/topic/dictionary_functions
Helper function to split a list into a dictionary, from @jgoetzinger
2 parents e5544ef + 4eb74c4 commit 6a0c746

File tree

2 files changed

+24
-9
lines changed

2 files changed

+24
-9
lines changed

labgridhelper/barebox.py

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from labgrid.driver import BareboxDriver
2-
2+
from labgridhelper.dict import split_to_dict
33

44
def get_commands(command):
55
"""Returns the available commands of a running Barebox bootloader
@@ -26,11 +26,4 @@ def get_globals(command):
2626
"""
2727
assert isinstance(command, BareboxDriver)
2828
out = command.run_check("global")
29-
out = map(lambda x: x[2:], out)
30-
global_variables = {}
31-
for line in out:
32-
sep = line.index(":")
33-
key = line[:sep]
34-
value = line[sep+2:]
35-
global_variables[key] = value
36-
return global_variables
29+
return split_to_dict(out, delimiter=":", strip_chars="* ")

labgridhelper/dict.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
def split_to_dict(command_output, delimiter="=", strip_chars=""):
2+
'''
3+
returns a dictionary that splits the content of the command_output.
4+
The key is the part before and the value is the part after the first
5+
delimiter. Optional chars can be specified to be stript from key and
6+
value.
7+
Args:
8+
command_output: output to split into dict
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+
output_dict = {}
15+
for line in command_output:
16+
try:
17+
if delimiter in line: # lines without a delimiter are ignored
18+
(key, value) = line.split(delimiter, 1)
19+
output_dict[key.strip(strip_chars)] = value.strip(strip_chars)
20+
except ValueError:
21+
pass
22+
return output_dict

0 commit comments

Comments
 (0)