Skip to content

Commit 3053c0f

Browse files
authored
Add support to serialize/deserialize SARIF report
Merge pull request #1 from FunJim/serialize-support
2 parents 8bcc64f + 32f1f24 commit 3053c0f

File tree

4 files changed

+115
-8
lines changed

4 files changed

+115
-8
lines changed

README.rst

Lines changed: 54 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,18 +27,65 @@ How to use
2727

2828
.. code-block:: python
2929
30-
import cattrs
31-
import json
32-
import sys
30+
from sarif_om import SarifLog, Run, Tool, ToolComponent, Result, Message
31+
32+
33+
sarif_log = SarifLog(
34+
version="2.1.0",
35+
runs=[
36+
Run(
37+
tool=Tool(
38+
driver=ToolComponent(
39+
name="My Static Analyzer",
40+
version="1.0.0",
41+
)
42+
),
43+
results=[
44+
Result(
45+
rule_id="R001",
46+
message=Message(text="Use of uninitialized variable 'x'"),
47+
)
48+
],
49+
)
50+
],
51+
)
52+
53+
print(sarif_log)
54+
55+
To serialize it to plain dict or JSON:
3356

34-
from sarif_om import SarifLog
57+
.. code-block:: python
58+
59+
from sarif_om import to_dict, to_json
3560
61+
print(to_json(sarif_log))
62+
print(to_dict(sarif_log))
3663
37-
with open(sys.argv[1]) as fp:
38-
data = json.load(fp)
64+
To deserialize dict/JSON data to ``sarif_om.SarifLog``:
3965

40-
sarif_log = cattrs.structure(data, SarifLog)
66+
.. code-block:: python
4167
68+
import json
69+
from sarif_om import from_dict, from_json
70+
71+
72+
sarif_log_dict = {
73+
"runs": [
74+
{
75+
"tool": {"driver": {"name": "My Static Analyzer", "version": "1.0.0"}},
76+
"results": [
77+
{
78+
"message": {"text": "Use of uninitialized variable 'x'"},
79+
"ruleId": "R001",
80+
}
81+
],
82+
}
83+
],
84+
"version": "2.1.0",
85+
}
86+
87+
print(from_dict(sarif_log_dict))
88+
print(from_json(json.dumps(sarif_log_dict)))
4289
4390
Generation
4491
==========

sarif_om/__init__.py

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# This file was generated by jschema_to_python version 1.2.3.
22

3+
import json
4+
from typing import Dict
5+
from attrs import has, fields
6+
from cattrs import Converter
7+
from cattrs.gen import make_dict_unstructure_fn, make_dict_structure_fn, override
8+
39
from ._sarif_log import SarifLog
410
from ._address import Address
511
from ._artifact import Artifact
@@ -53,3 +59,56 @@
5359
from ._version_control_details import VersionControlDetails
5460
from ._web_request import WebRequest
5561
from ._web_response import WebResponse
62+
63+
64+
def _generate_sarif_report_cattrs_converter() -> Converter:
65+
converter = Converter()
66+
67+
def _unstructure(cls):
68+
return make_dict_unstructure_fn(
69+
cls,
70+
converter,
71+
**{
72+
a.name: override(
73+
rename=a.metadata["schema_property_name"],
74+
omit_if_default=True,
75+
)
76+
for a in fields(cls)
77+
},
78+
)
79+
80+
def _structure(cls):
81+
return make_dict_structure_fn(
82+
cls,
83+
converter,
84+
**{
85+
a.name: override(
86+
rename=a.metadata["schema_property_name"],
87+
)
88+
for a in fields(cls)
89+
},
90+
)
91+
92+
converter.register_unstructure_hook_factory(has, _unstructure)
93+
converter.register_structure_hook_factory(has, _structure)
94+
return converter
95+
96+
97+
_converter = _generate_sarif_report_cattrs_converter()
98+
99+
100+
def to_dict(sarif_log: SarifLog) -> Dict:
101+
return _converter.unstructure(sarif_log, SarifLog)
102+
103+
104+
def to_json(sarif_log: SarifLog, indent=2) -> str:
105+
obj = to_dict(sarif_log)
106+
return json.dumps(obj, indent=indent)
107+
108+
109+
def from_dict(data: Dict) -> SarifLog:
110+
return _converter.structure(data, SarifLog)
111+
112+
113+
def from_json(data: str) -> SarifLog:
114+
return from_dict(json.loads(data))

setup.cfg

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ classifier =
2020
python_requires = >= 2.7
2121
install_requires =
2222
attrs
23+
cattrs
2324
pbr
2425

2526
[files]

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22

33
from setuptools import setup
44

5-
setup(setup_requires=["pbr"], pbr=True)
5+
setup(setup_requires=["pbr"], install_requires=["attrs", "cattrs"], pbr=True)

0 commit comments

Comments
 (0)