Skip to content
Snippets Groups Projects
Commit 0b057f46 authored by Jorrit Schaap's avatar Jorrit Schaap
Browse files

TMSS-648: sometimes the incoming parset_string does not contain line endings....

TMSS-648: sometimes the incoming parset_string does not contain line endings. Parse it using knowledge about key-value-pairs and whitespaces
parent 1563adfa
No related branches found
No related tags found
1 merge request!363Resolve TMSS-648
...@@ -161,7 +161,23 @@ class parameterset(PyParameterSet): ...@@ -161,7 +161,23 @@ class parameterset(PyParameterSet):
Splits the string in lines, and parses each '=' seperated key/value pair. Splits the string in lines, and parses each '=' seperated key/value pair.
''' '''
lines = [l.strip() for l in parset_string.split('\n')] lines = [l.strip() for l in parset_string.split('\n')]
kv_pairs = [tuple(l.split('=')) for l in lines if '=' in l] if len(lines) == 1 and parset_string.count('=') > 1:
# the given parset_string lacks proper line endings.
# try to split the single-line-parset_string into proper lines, and reparse.
# a parset line is made of three parts: <key> = <value>
# the <key> contains no whitespace, the '=' can be surrounded by whitespace, and the value can contain whitespace as well.
# so, split the string at each '=', strip the ends of the parts, and extract the key-value pairs
parts = [part.strip() for part in parset_string.split('=')]
kv_pairs = []
key = parts[0]
for part in parts[1:-1]:
part_parts = part.split()
value = ' '.join(part_parts[:-1])
kv_pairs.append((key,value))
key = part_parts[-1]
kv_pairs.append((key,parts[-1]))
else:
kv_pairs = [tuple(l.split('=')) for l in lines if '=' in l]
parset_dict = dict(kv_pairs) parset_dict = dict(kv_pairs)
return parameterset(parset_dict) return parameterset(parset_dict)
...@@ -262,4 +278,5 @@ class parameterset(PyParameterSet): ...@@ -262,4 +278,5 @@ class parameterset(PyParameterSet):
def __str__(self): def __str__(self):
""":returns the parset in a human readable string (lines of key=value, sorted by key)""" """:returns the parset in a human readable string (lines of key=value, sorted by key)"""
return '\n'.join("%s=%s" % (key, self[key]) for key in sorted(self.keys())) return '\n'.join("%s=%s" % (key, self[key]) for key in sorted(self.keys()))
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment