Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JSON output [5] #105

Merged
merged 28 commits into from
Oct 3, 2022
Merged

JSON output [5] #105

merged 28 commits into from
Oct 3, 2022

Conversation

Honny1
Copy link
Member

@Honny1 Honny1 commented Aug 16, 2022

This PR creates the ability to use templates for JSON output. And creates JSON-EVERYTHING format which is JSON contains all information that openscap-report can offer.

Depends on PR: #103

@Honny1 Honny1 added the dependency on another PR Waiting for merge another PR label Aug 17, 2022
@Honny1 Honny1 marked this pull request as ready for review August 19, 2022 09:31
@Honny1 Honny1 marked this pull request as draft August 21, 2022 21:58
@Honny1 Honny1 marked this pull request as ready for review August 22, 2022 12:59
@Honny1 Honny1 force-pushed the json-output-6 branch 3 times, most recently from d80acdd to 07dc415 Compare August 24, 2022 09:36
@Honny1 Honny1 force-pushed the json-output-6 branch 3 times, most recently from 9b0c5d5 to 8b4b4bc Compare September 2, 2022 14:21
@@ -2,7 +2,9 @@
# SPDX-License-Identifier: LGPL-2.1-or-later

from .full_text_parser import FullTextParser
from .groupe_parser import GroupParser
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
from .groupe_parser import GroupParser
from .group_parser import GroupParser

@@ -2,7 +2,9 @@
# SPDX-License-Identifier: LGPL-2.1-or-later

from .full_text_parser import FullTextParser
from .groupe_parser import GroupParser
from .info_of_test_parser import InfoOfTest
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd recommend

Suggested change
from .info_of_test_parser import InfoOfTest
from .test_info_parser import InfoOfTest

def generate_report(self, debug_setting):
logging.warning("JSON Format is experimental output!")
template = self.env.get_template("base.json")
json_data = json.loads(template.render(report=self.report, debug_setting=debug_setting))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not quite sure that Jinja is needed here. You can just filter out keys from the report dict and then export it to JSON. Even if in the future there would be some more sophisticated logic (like keeping report.foo.bar and report.foo.baz but not report.foo.others) it'd be easier to just write a more complicated filter function rather than to siphon the whole report through Jinja engine.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right.

self._transform_tuples_to_dict(out)
if out:
return out
return None
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

First of all you can just return out or None.

But I don't quite understand why you're trying to use None instead of an empty list if there are no elements. What benefit does it give? I can see a drawback, though. In situations when one would iterate over a list there always should be a check if the list is not None.

Copy link
Member Author

@Honny1 Honny1 Sep 11, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

By using None instead of an empty array, I mean that there is no data in the JSON for that item. A dictionary filter could solve this.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, but why do you think that

foo {
   bars_list: None
}

is better than

foo {
   bars_list: []
}

?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In terms of python code, it is better to use an empty array. As for the JSON content, I think it's better to use null because it's more obvious that there's no data or this data is optional. I think it is about a discussion about JSON content. Which is the best value to use for cases when there is no data?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right, let's discuss it in the JSON structure issue. But here is some homework for you: https://www.infoq.com/presentations/Null-References-The-Billion-Dollar-Mistake-Tony-Hoare/ :)

references.append(Reference(referenc.get("href"), referenc.text))
if references:
return references
return None
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

None instead of [], why?

identifiers.append(Identifier(identifier.get("system"), identifier.text))
if identifiers:
return identifiers
return None
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

None instead of [], why?

return warnings
if warnings:
return warnings
return None
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

None instead of [], why?

return output
if output:
return output
return None
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

None instead of [], why?

def get_rules(self):
rules = {}
for rule in self.root.findall(".//xccdf:Rule", NAMESPACES):
rule = self.process_rule(rule)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This kind of shadowing could lead to a lot of troubles in a dynamically typed language. If you can have a Rule class entity and a rule xml sub-document it's better to introduce a naming scheme which won't allow you to mix them up. For example for all xml entities it could be _els (rule_el).

@Honny1 Honny1 mentioned this pull request Sep 12, 2022
@Honny1 Honny1 force-pushed the json-output-6 branch 3 times, most recently from 223f566 to e166e04 Compare September 14, 2022 14:49
@Honny1 Honny1 mentioned this pull request Sep 15, 2022
@Honny1 Honny1 changed the title JSON output [6] JSON output [5] Sep 15, 2022
@Honny1 Honny1 force-pushed the json-output-6 branch 2 times, most recently from f409022 to 79e8a41 Compare September 15, 2022 14:55
@Honny1
Copy link
Member Author

Honny1 commented Sep 15, 2022

@evgenyz I changed the code according to your review. And I dropped the json-output-2 branch because there was code there that replace empty arrays with none.

@Honny1 Honny1 requested a review from evgenyz September 15, 2022 15:02
@evgenyz evgenyz merged commit 712e7a7 into OpenSCAP:master Oct 3, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
dependency on another PR Waiting for merge another PR
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants