diff --git a/src/presenters/presenters/base_presenter.py b/src/presenters/presenters/base_presenter.py index f1d39bfec..b56d1bf06 100644 --- a/src/presenters/presenters/base_presenter.py +++ b/src/presenters/presenters/base_presenter.py @@ -145,7 +145,7 @@ def get_max_tlp(self, reports): color_values = {"WHITE": 0, "CLEAR": 1, "GREEN": 2, "AMBER": 3, "AMBER+STRICT": 4, "RED": 5} colors = [] for report in reports: - if report.type.startswith("Vulnerability Report"): + if hasattr(report.attrs, "tlp"): colors.append(report.attrs.tlp) if colors: max_tlp = max(colors, key=lambda color: color_values.get(color, 1)) @@ -184,13 +184,13 @@ def __init__(self, presenter_input): presenter_input -- input data """ # types of report items (e.g. vuln report, disinfo report) - report_types = dict() + report_types = {} for report_type in presenter_input.report_types: # index by ID report_types[report_type.id] = report_type # attributes that can be used in report items (for internal use) - attribute_map = dict() + attribute_map = {} for report_type in presenter_input.report_types: for attribute_group in report_type.attribute_groups: for attribute_group_item in attribute_group.attribute_group_items: @@ -198,7 +198,7 @@ def __init__(self, presenter_input): self.product = presenter_input.product self.product.date = datetime.datetime.now() - self.report_items = list() + self.report_items = [] for report in presenter_input.reports: self.report_items.append(BasePresenter.ReportItemObject(report, report_types, attribute_map)) @@ -209,12 +209,16 @@ def __init__(self, presenter_input): for report in self.report_items: if report.type.startswith("Vulnerability Report"): vul_report_count += 1 - if report.attrs.links: + if hasattr(report.attrs, "links"): for link in report.attrs.links: if link not in product_links: product_links.append(link) - report.attrs.description = self.link_renumbering(report.attrs.description, report.attrs.links, product_links) - report.attrs.recommendations = self.link_renumbering(report.attrs.recommendations, report.attrs.links, product_links) + if hasattr(report.attrs, "description"): + report.attrs.description = self.link_renumbering(report.attrs.description, report.attrs.links, product_links) + if hasattr(report.attrs, "recommendations"): + report.attrs.recommendations = self.link_renumbering( + report.attrs.recommendations, report.attrs.links, product_links + ) # If there are vulnerability reports, set the max TLP and product links if vul_report_count > 0: self.product.max_tlp = self.get_max_tlp(self.report_items) diff --git a/src/presenters/presenters/html_presenter.py b/src/presenters/presenters/html_presenter.py index 03571cb9d..7a9aabb53 100644 --- a/src/presenters/presenters/html_presenter.py +++ b/src/presenters/presenters/html_presenter.py @@ -1,4 +1,8 @@ +"""HTML Presenter. +Returns: + A dictionary containing the MIME type and the base64-encoded HTML data. +""" import os from base64 import b64encode import jinja2 @@ -8,37 +12,44 @@ class HTMLPresenter(BasePresenter): + """Presenter for generating HTML documents. + + Arguments: + BasePresenter -- The base presenter class. + + Returns: + A dictionary containing the MIME type and the base64-encoded HTML data. + """ + type = "HTML_PRESENTER" name = "HTML Presenter" description = "Presenter for generating html documents" - parameters = [ - Parameter(0, "HTML_TEMPLATE_PATH", "HTML template with its path", "Path of html template file", - ParameterType.STRING) - ] + parameters = [Parameter(0, "HTML_TEMPLATE_PATH", "HTML template with its path", "Path of html template file", ParameterType.STRING)] parameters.extend(BasePresenter.parameters) def generate(self, presenter_input): + """Generate the HTML presentation. + + Arguments: + presenter_input -- The input data for the presenter. + Returns: + A dictionary containing the MIME type and the base64-encoded HTML data. + """ try: - head, tail = os.path.split(presenter_input.parameter_values_map['HTML_TEMPLATE_PATH']) + head, tail = os.path.split(presenter_input.parameter_values_map["HTML_TEMPLATE_PATH"]) input_data = BasePresenter.generate_input_data(presenter_input) env = jinja2.Environment(loader=jinja2.FileSystemLoader(head)) env.filters["strfdate"] = BasePresenter._filter_datetime output_text = env.get_template(tail).render(data=input_data).encode() base64_bytes = b64encode(output_text) - data = base64_bytes.decode('UTF-8') + data = base64_bytes.decode("UTF-8") - presenter_output = { - 'mime_type': 'text/html', - 'data': data - } + presenter_output = {"mime_type": "text/html", "data": data} return presenter_output except Exception as error: BasePresenter.print_exception(self, error) - presenter_output = { - 'mime_type': 'text/plain', - 'data': b64encode(("TEMPLATING ERROR\n"+str(error)).encode()).decode('UTF-8') - } + presenter_output = {"mime_type": "text/plain", "data": b64encode((f"TEMPLATING ERROR\n{error}").encode()).decode("UTF-8")} return presenter_output diff --git a/src/presenters/presenters/message_presenter.py b/src/presenters/presenters/message_presenter.py index 23a61403e..eab08e104 100644 --- a/src/presenters/presenters/message_presenter.py +++ b/src/presenters/presenters/message_presenter.py @@ -3,6 +3,7 @@ Returns: _description_ """ + import os from base64 import b64encode import jinja2 @@ -66,5 +67,5 @@ def generate_part(template_path): except Exception as error: BasePresenter.print_exception(self, error) - presenter_output = {"mime_type": "text/plain", "data": b64encode(("TEMPLATING ERROR\n" + str(error)).encode()).decode("UTF-8")} + presenter_output = {"mime_type": "text/plain", "data": b64encode((f"TEMPLATING ERROR\n{error}").encode()).decode("UTF-8")} return presenter_output diff --git a/src/presenters/presenters/misp_presenter.py b/src/presenters/presenters/misp_presenter.py index 7493882ea..70fdfa7d9 100644 --- a/src/presenters/presenters/misp_presenter.py +++ b/src/presenters/presenters/misp_presenter.py @@ -1,3 +1,8 @@ +"""Module for MISP presenter. + +Returns: + dict: The presenter output containing the MIME type and data. +""" import os from base64 import b64encode import jinja2 @@ -7,21 +12,36 @@ class MISPPresenter(BasePresenter): + """Presenter class for generating MISP platform. + + This presenter is responsible for generating the MISP platform using a template file. + + Arguments: + BasePresenter (class): The base presenter class. + + Returns: + dict: The presenter output containing the MIME type and data. + """ + type = "MISP_PRESENTER" name = "MISP Presenter" description = "Presenter for generating MISP platform" - parameters = [ - Parameter(0, "MISP_TEMPLATE_PATH", "MISP template with its path", "Path of MISP template file", - ParameterType.STRING) - ] + parameters = [Parameter(0, "MISP_TEMPLATE_PATH", "MISP template with its path", "Path of MISP template file", ParameterType.STRING)] parameters.extend(BasePresenter.parameters) def generate(self, presenter_input): + """Generate the output data based on the presenter input. + + Arguments: + presenter_input (PresenterInput): The input data for the presenter. + Returns: + dict: The presenter output containing the mime type and data. + """ try: - head, tail = os.path.split(presenter_input.parameter_values_map['MISP_TEMPLATE_PATH']) + head, tail = os.path.split(presenter_input.parameter_values_map["MISP_TEMPLATE_PATH"]) input_data = BasePresenter.generate_input_data(presenter_input) @@ -31,18 +51,12 @@ def generate(self, presenter_input): base64_bytes = b64encode(output_text) - data = base64_bytes.decode('UTF-8') + data = base64_bytes.decode("UTF-8") - presenter_output = { - 'mime_type': 'application/json', - 'data': data - } + 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(("TEMPLATING ERROR\n"+str(error)).encode()).decode('UTF-8') - } + presenter_output = {"mime_type": "text/plain", "data": b64encode((f"TEMPLATING ERROR\n{error}").encode()).decode("UTF-8")} return presenter_output diff --git a/src/presenters/presenters/text_presenter.py b/src/presenters/presenters/text_presenter.py index edc454702..f0b3d5a10 100644 --- a/src/presenters/presenters/text_presenter.py +++ b/src/presenters/presenters/text_presenter.py @@ -1,3 +1,8 @@ +"""Module for the TEXT presenter. + +Returns: + dict: The presenter output containing the mime type and data of the generated text document. +""" import os from base64 import b64encode import jinja2 @@ -7,21 +12,36 @@ class TEXTPresenter(BasePresenter): + """Presenter for generating text documents. + + This presenter is responsible for generating text documents based on a text template. + + Arguments: + BasePresenter (class): The base presenter class. + + Returns: + dict: The presenter output containing the mime type and data of the generated text document. + """ + type = "TEXT_PRESENTER" name = "TEXT Presenter" description = "Presenter for generating text documents" - parameters = [ - Parameter(0, "TEXT_TEMPLATE_PATH", "TEXT template with its path", "Path of text template file", - ParameterType.STRING) - ] + parameters = [Parameter(0, "TEXT_TEMPLATE_PATH", "TEXT template with its path", "Path of text template file", ParameterType.STRING)] parameters.extend(BasePresenter.parameters) def generate(self, presenter_input): + """Generate the output text based on the presenter input. + + Arguments: + presenter_input (PresenterInput): The input data for the presenter. + Returns: + dict: The presenter output containing the mime type and the generated text data. + """ try: - head, tail = os.path.split(presenter_input.parameter_values_map['TEXT_TEMPLATE_PATH']) + head, tail = os.path.split(presenter_input.parameter_values_map["TEXT_TEMPLATE_PATH"]) input_data = BasePresenter.generate_input_data(presenter_input) @@ -39,18 +59,12 @@ def generate(self, presenter_input): base64_bytes = b64encode(output_text) - data = base64_bytes.decode('UTF-8') + data = base64_bytes.decode("UTF-8") - presenter_output = { - 'mime_type': 'text/plain', - 'data': data - } + presenter_output = {"mime_type": "text/plain", "data": data} return presenter_output except Exception as error: BasePresenter.print_exception(self, error) - presenter_output = { - 'mime_type': 'text/plain', - 'data': b64encode(("TEMPLATING ERROR\n"+str(error)).encode()).decode('UTF-8') - } + presenter_output = {"mime_type": "text/plain", "data": b64encode((f"TEMPLATING ERROR\n{error}").encode()).decode("UTF-8")} return presenter_output