Skip to content

Commit

Permalink
Merge pull request #161 from multiflexi/json_presenter
Browse files Browse the repository at this point in the history
JSON presenter
  • Loading branch information
milankowww authored Sep 27, 2023
2 parents 9ec773a + da1046d commit 93ade5f
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/presenters/managers/presenters_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from presenters.html_presenter import HTMLPresenter
from presenters.text_presenter import TEXTPresenter
from presenters.misp_presenter import MISPPresenter
from presenters.json_presenter import JSONPresenter
from presenters.message_presenter import MESSAGEPresenter
from shared.schema.presenter import PresenterInputSchema, PresenterOutputSchema

Expand All @@ -19,6 +20,7 @@ def initialize():
register_presenter(HTMLPresenter())
register_presenter(TEXTPresenter())
register_presenter(MISPPresenter())
register_presenter(JSONPresenter())
register_presenter(MESSAGEPresenter())


Expand Down
53 changes: 53 additions & 0 deletions src/presenters/presenters/json_presenter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
"""Presenter outputing JSON file.
Returns:
_type_: _description_
"""
from base64 import b64encode
import json

from .base_presenter import BasePresenter
from shared.schema.parameter import Parameter, ParameterType


class JSONPresenter(BasePresenter):
"""Class for JSON presenter.
Arguments:
BasePresenter -- Superclass
Returns:
_description_
"""

type = "JSON_PRESENTER"
name = "JSON Presenter"
description = "Presenter for generating JSON files"

parameters = [Parameter(0, "JSON_INDENT", "JSON indent", "Indentation of JSON output", ParameterType.NUMBER)]

parameters.extend(BasePresenter.parameters)

def generate(self, presenter_input):
"""Generate method.
Args:
presenter_input (_type_): _description_
Returns:
_type_: _description_
"""
indent = int(presenter_input.parameter_values_map["JSON_INDENT"])

try:
input_data = BasePresenter.generate_input_data(presenter_input)
json_object = json.dumps(input_data, sort_keys=True, indent=indent)
base64_bytes = b64encode(json_object.encode())
data = base64_bytes.decode("UTF-8")

presenter_output = {"mime_type": "application/json", "data": data}
return presenter_output
except Exception as error:
BasePresenter.print_exception(self, error)
presenter_output = {"mime_type": "text/plain", "data": b64encode(("ERROR\n" + str(error)).encode()).decode("UTF-8")}
return presenter_output

0 comments on commit 93ade5f

Please sign in to comment.