-
Notifications
You must be signed in to change notification settings - Fork 9
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
JSON output [5] #105
Conversation
d80acdd
to
07dc415
Compare
9b0c5d5
to
8b4b4bc
Compare
8b4b4bc
to
486fe0a
Compare
@@ -2,7 +2,9 @@ | |||
# SPDX-License-Identifier: LGPL-2.1-or-later | |||
|
|||
from .full_text_parser import FullTextParser | |||
from .groupe_parser import GroupParser |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd recommend
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)) |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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: []
}
?
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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 _el
s (rule_el
).
223f566
to
e166e04
Compare
e166e04
to
fa1c005
Compare
f409022
to
79e8a41
Compare
@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. |
* Move methots of parser to separate classes to impruve organisation of code. * Reduce using empty structures in __init__ methodes and impruve overall code quality. * Impruve code strucutre for better tesing.
JSON-EVERYTHING contains everyhing what openscap-report can offer.
79e8a41
to
4f7456c
Compare
This PR creates the ability to use templates for
JSON
output. And createsJSON-EVERYTHING
format which isJSON
contains all information thatopenscap-report
can offer.Depends on PR: #103