Skip to content

Commit 0abb454

Browse files
László Vaskóe3krisztian
authored andcommitted
write metadata result
1 parent 5d2a082 commit 0abb454

File tree

3 files changed

+29
-2
lines changed

3 files changed

+29
-2
lines changed

unblob/cli.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -192,8 +192,11 @@ def cli(
192192
)
193193

194194
logger.info("Start processing file", file=file)
195-
all_reports = process_file(config, file)
196-
return all_reports
195+
results = process_file(config, file)
196+
197+
with file.with_suffix(".result.json").open("w") as fd:
198+
fd.write(results.to_json())
199+
return results
197200

198201

199202
cli.context_class = UnblobContext

unblob/models.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import abc
22
import itertools
3+
import json
4+
from enum import Enum
35
from pathlib import Path
46
from typing import List, Optional, Tuple, Type
57

@@ -128,6 +130,25 @@ def errors(self) -> List[ErrorReport]:
128130
def register(self, result: TaskResult):
129131
self.results.append(result)
130132

133+
def to_json(self, indent=" "):
134+
return json.dumps(self.results, cls=_JSONEncoder, indent=indent)
135+
136+
137+
class _JSONEncoder(json.JSONEncoder):
138+
def default(self, obj):
139+
if isinstance(obj, Enum):
140+
return obj.name
141+
142+
if attr.has(type(obj)):
143+
extend_attr_output = True
144+
attr_output = attr.asdict(obj, recurse=not extend_attr_output)
145+
attr_output["__typename__"] = obj.__class__.__name__
146+
return attr_output
147+
148+
if isinstance(obj, Path):
149+
return str(obj)
150+
return json.JSONEncoder.default(self, obj)
151+
131152

132153
class ExtractError(Exception):
133154
"""There was an error during extraction"""

vulture_whitelist.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import unblob.plugins
44
from unblob import cli
55
from unblob.file_utils import File, iterbits, round_down
6+
from unblob.models import _JSONEncoder
67
from unblob.parser import _HexStringToRegex
78

89
_HexStringToRegex.literal
@@ -11,6 +12,8 @@
1112
_HexStringToRegex.range_jump
1213
_HexStringToRegex.alternative
1314

15+
_JSONEncoder.default
16+
1417
sys.breakpointhook
1518
cli.cli.context_class
1619

0 commit comments

Comments
 (0)