From 5fff8afc9f47f8cc6db8ed3613b90b5cdc683b43 Mon Sep 17 00:00:00 2001 From: Kelly Brazil Date: Fri, 10 Jul 2020 08:21:15 -0700 Subject: [PATCH] add fixes for freebsd where values can be on separate lines under the key --- jc/parsers/sysctl.py | 42 ++++++++++++++++++++++++++++++++---------- 1 file changed, 32 insertions(+), 10 deletions(-) diff --git a/jc/parsers/sysctl.py b/jc/parsers/sysctl.py index 76aa7b42..eb236c0e 100644 --- a/jc/parsers/sysctl.py +++ b/jc/parsers/sysctl.py @@ -111,20 +111,42 @@ def parse(data, raw=False, quiet=False): if ' = ' in data[0]: delim = ' = ' else: - delim = ':' + delim = ': ' for line in filter(None, data): linedata = line.split(delim, maxsplit=1) - key = linedata[0] - value = linedata[1].lstrip() - # syctl -a repeats some keys on linux. Append values from repeating keys - # to the previous key value - if key in raw_output: - existing_value = raw_output[key] - raw_output[key] = existing_value + '\n' + value - else: - raw_output[key] = value + # bsd adds values to newlines, which need to be fixed up with this try/except block + try: + key = linedata[0] + value = linedata[1] + + # syctl -a repeats some keys on linux. Append values from repeating keys + # to the previous key value + if key in raw_output: + existing_value = raw_output[key] + raw_output[key] = existing_value + '\n' + value + continue + + # fix for weird multiline output in bsd + # if the key looks strange (has spaces or no dots) then it's probably a value field + # on a separate line. in this case, just append it to the previous key in the dictionary. + if '.' not in key or ' ' in key: + previous_key = [*raw_output.keys()][-1] + raw_output[previous_key] = raw_output[previous_key] + '\n' + line + continue + + # if the key looks normal then just add to the dictionary as normal + else: + raw_output[key] = value + continue + + # if there is an exception, then there was no delimiter in the line. In this case + # just append the data line as a value to the previous key. + except Exception: + prior_key = [*raw_output.keys()][-1] + raw_output[prior_key] = raw_output[prior_key] + '\n' + line + continue if raw: return raw_output