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 presenter #161

Merged
merged 2 commits into from
Sep 27, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/presenters/managers/presenters_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,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 shared.schema.presenter import PresenterInputSchema, PresenterOutputSchema

presenters = {}
Expand All @@ -12,6 +13,7 @@ def initialize():
register_presenter(HTMLPresenter())
register_presenter(TEXTPresenter())
register_presenter(MISPPresenter())
register_presenter(JSONPresenter())


def register_presenter(presenter):
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