def write()

in src/handlers/resource_report_handler.py [0:0]


    def write(self, wsh: Worksheet, wb: Workbook):
        bold = wb.add_format({'bold': True})
        red = wb.add_format({'bg_color': '#da9694'})
        yellow = wb.add_format({'bg_color': '#ffff00'})
        green = wb.add_format({'bg_color': '#92d051'})
        gray = wb.add_format({'bg_color': '#bfbfbf'})

        def sf(sev: str):
            """Format for severity"""
            if sev == Severity.HIGH:
                return red
            if sev == Severity.MEDIUM:
                return yellow
            if sev == Severity.LOW:
                return green
            return gray

        table = Table()
        table.new_row()
        for h in self.head:
            table.add_cells(CellContent(h, bold))

        # a bit devilish code :(
        # imagine you have a list of lists or ints. The thing below sorts the
        # main lists when the key equal to the maximum value of inner lists.
        # But,
        # - instead of ints -> severities and custom cmp function
        # - instead of list of lists -> dict there values are tuples with
        # the first element - that list
        # - values of inner lists not the actual values to sort by. They
        # are not severities. Actual severities must be retrieved from a map
        key = cmp_to_key(severity_cmp)
        aggregated = dict(
            sorted(
                self._aggregated().items(),
                key=lambda p: key(
                    self._meta.rule(
                        max(
                            p[1][0],
                            key=lambda x: key(
                                self._meta.rule(x).severity.value
                            ),
                        )
                    ).severity.value
                ),
                reverse=True,
            )
        )
        i = 0
        for unique, data in aggregated.items():
            _, region, resource = unique
            rules, dto, ts = data
            rules = sorted(
                rules,
                key=lambda x: key(self._meta.rule(x).severity.value),
                reverse=True,
            )
            table.new_row()
            table.add_cells(CellContent(i))
            services = set(
                filter(None, (self._meta.rule(rule).service for rule in rules))
            )
            if services:
                table.add_cells(CellContent(', '.join(services)))
            else:
                table.add_cells()
            table.add_cells(CellContent(dto))
            if self._keep_region:
                table.add_cells(CellContent(region))
            else:
                table.add_cells(CellContent())
            table.add_cells(CellContent(utc_iso(datetime.fromtimestamp(ts))))
            table.add_cells(*[CellContent(rule) for rule in rules])
            table.add_cells(
                *[
                    CellContent(self._it.collection.meta[rule]['description'])
                    for rule in rules
                ]
            )
            table.add_cells(
                *[
                    CellContent(
                        self._meta.rule(rule).severity.value,
                        sf(self._meta.rule(rule).severity.value),
                    )
                    for rule in rules
                ]
            )
            table.add_cells(
                *[CellContent(self._meta.rule(rule).article) for rule in rules]
            )
            table.add_cells(
                *[
                    CellContent(self._meta.rule(rule).remediation)
                    for rule in rules
                ]
            )
            i += 1
        writer = XlsxRowsWriter()
        writer.write(wsh, table)