Skip to content

Commit

Permalink
keep skipped lines into attribute unparsed_lines
Browse files Browse the repository at this point in the history
Signed-off-by: Xiaoxue Wang <[email protected]>
  • Loading branch information
JoySnow committed Dec 23, 2024
1 parent 14982ff commit e3db9b4
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 3 deletions.
9 changes: 9 additions & 0 deletions insights/core/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -778,6 +778,14 @@ def parse_content(self, content):
class JSONParser(Parser, LegacyItemAccess):
"""
A parser class that reads JSON files. Base your own parser on this.
Attributes:
data (dict): The loaded json content
unparsed_lines (list): The skipped unparsed lines
Raises:
ParseException: When any error be thrown during the json loading of `content`.
SkipComponent: When `content` is empty or the loaded data is empty.
"""
def parse_content(self, content):
# If content is empty then raise a skip exception instead of a parse exception.
Expand All @@ -793,6 +801,7 @@ def parse_content(self, content):
if line and line.startswith('{') or line.startswith('['):
actual_start_index = idx
break
self.unparsed_lines = content[:actual_start_index]
self.data = json.loads('\n'.join(content[actual_start_index:]))
else:
self.data = json.loads(content)
Expand Down
4 changes: 4 additions & 0 deletions insights/tests/parsers/test_fwupdagent.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,10 @@ def test_security():
assert security["HostSecurityAttributes"][0]["HsiResult"] == "not-tainted"
assert security["HostSecurityAttributes"][1]["Name"] == "Encrypted RAM"
assert security["HostSecurityAttributes"][1]["HsiLevel"] == 4
assert security.unparsed_lines == [
"INFO: The fwupdagent command is deprecated, use `fwupdmgr --json` instead",
"WARNING: UEFI firmware can not be updated in legacy BIOS mode",
" See https://github.com/fwupd/fwupd/wiki/PluginFlag:legacy-bios for more information."]

with pytest.raises(ParseException):
FwupdagentSecurity(context_wrap(SECURITY_ERROR_1))
Expand Down
15 changes: 12 additions & 3 deletions insights/tests/test_json_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ class MyJsonParser(JSONParser):
def test_json_parser_success():
for jsonstr in json_test_strings:
ctx = context_wrap(jsonstr)
assert MyJsonParser(ctx).data == json_test_strings[jsonstr]
my_json_parser = MyJsonParser(ctx)
assert my_json_parser.data == json_test_strings[jsonstr]
assert my_json_parser.unparsed_lines == []


def test_json_parser_failure():
Expand All @@ -58,10 +60,17 @@ def test_json_parser_null_value():

def test_json_parser_with_extra_header_lines():
ctx = context_wrap(JSON_CONTENT_WITH_EXTRA_HEADER_LINES_1)
assert MyJsonParser(ctx).data == {'a': [{'b': 'x'}, {'b': 'y'}]}
my_json_parser = MyJsonParser(ctx)
assert my_json_parser.data == {'a': [{'b': 'x'}, {'b': 'y'}]}
assert my_json_parser.unparsed_lines == [
"INFO: The fwupdagent command is deprecated, use `fwupdmgr --json` instead",
"WARNING: UEFI firmware can not be updated in legacy BIOS mode",
" See https://github.com/fwupd/fwupd/wiki/PluginFlag:legacy-bios for more information."]

ctx = context_wrap(JSON_CONTENT_WITH_EXTRA_HEADER_LINES_2)
assert MyJsonParser(ctx).data == {}
my_json_parser = MyJsonParser(ctx)
assert my_json_parser.data == {}
assert len(my_json_parser.unparsed_lines) == 3

ctx = context_wrap(JSON_CONTENT_WITH_EXTRA_HEADER_LINES_3)
with pytest.raises(ParseException) as ex:
Expand Down

0 comments on commit e3db9b4

Please sign in to comment.