Skip to content

Commit

Permalink
feat: add support for spreadsheet
Browse files Browse the repository at this point in the history
  • Loading branch information
anthonyharrison committed Aug 12, 2024
1 parent e972b3f commit 9717e9c
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 4 deletions.
5 changes: 2 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ up for testing using different versions of Python.
## Usage

```
usage: sbom2doc [-h] [-i INPUT_FILE] [--debug] [--include-license] [-f {console,json,markdown,pdf}] [-o OUTPUT_FILE] [-V]
usage: sbom2doc [-h] [-i INPUT_FILE] [--debug] [--include-license] [-f {console,excel,json,markdown,pdf}] [-o OUTPUT_FILE] [-V]
SBOM2doc generates documentation for a SBOM.
Expand All @@ -36,11 +36,10 @@ Input:
Output:
--debug add debug information
--include-license add license text
-f {console,json,markdown,pdf}, --format {console,json,markdown,pdf}
-f {console,excel,json,markdown,pdf}, --format {console,excel,json,markdown,pdf}
Output format (default: output to console)
-o OUTPUT_FILE, --output-file OUTPUT_FILE
output filename (default: output to stdout)
```
## Operation
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ rich
reportlab
packageurl-python
requests
openpyxl
2 changes: 1 addition & 1 deletion sbom2doc/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def main(argv=None):
"--format",
action="store",
help="Output format (default: output to console)",
choices=["console", "json", "markdown", "pdf"],
choices=["console", "excel", "json", "markdown", "pdf"],
default="console",
)

Expand Down
78 changes: 78 additions & 0 deletions sbom2doc/docbuilder/spreadsheetbuilder.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
# Copyright (C) 2024 Anthony Harrison
# SPDX-License-Identifier: Apache-2.0

from openpyxl import Workbook
from openpyxl.styles import Font

from sbom2doc.docbuilder.docbuilder import DocBuilder


class SpreadsheetBuilder(DocBuilder):
def __init__(self):
self.workbook_document = Workbook()
self.worksheet = self.workbook_document.active
self.worksheet_data = []
self.table_width = 0
self.headingcount = 0
self.heading_title = None

def heading(self, level, title, number=True):
self._flush_table()
self.heading_title = title

def _flush_table(self):
if len(self.worksheet_data) > 0:
# flush table
self.showtable()

def _heading(self, title):
# Optionally create new worksheet
sheet_title = f"{self.headingcount} - {title}"
if self.headingcount > 0:
self.worksheet = self.workbook_document.create_sheet(sheet_title)
self.headingcount += 1
else:
# Name current sheet
self.worksheet.title = title
self.headingcount = 1
self.workbook_document.active = self.worksheet
self.worksheet_data = []

def paragraph(self, text):
if len(text) > 0:
if self.table_width == 0:
# Create pseudo table
self.createtable(["Text"])
self.worksheet_data.append([text])

def createtable(self, header, validate=None):
self._heading(self.heading_title)
self.worksheet_data.append(header)
self.table_width = len(header)

def addrow(self, data):
# Add row to table
my_data = []
for d in data:
if d is not None:
my_data.append(d)
else:
my_data.append("")
self.worksheet_data.append(my_data)

def showtable(self, widths=None):
# Add data to current worksheet
for data in self.worksheet_data:
self.worksheet.append(data)
# Now make first row Bold
font = Font(bold=True)
end_column = chr(ord("A") + self.table_width - 1)
for row in self.worksheet[f"A1:{end_column}1"]:
for cell in row:
cell.font = font
self.worksheet_data = []
self.table_width = 0

def publish(self, filename):
self._flush_table()
self.workbook_document.save(filename)
3 changes: 3 additions & 0 deletions sbom2doc/generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from sbom2doc.docbuilder.jsonbuilder import JSONBuilder
from sbom2doc.docbuilder.markdownbuilder import MarkdownBuilder
from sbom2doc.docbuilder.pdfbuilder import PDFBuilder
from sbom2doc.docbuilder.spreadsheetbuilder import SpreadsheetBuilder


def generate_document(format, sbom_parser, filename, outfile, include_license):
Expand All @@ -30,6 +31,8 @@ def generate_document(format, sbom_parser, filename, outfile, include_license):
sbom_document = JSONBuilder()
elif format == "pdf":
sbom_document = PDFBuilder()
elif format == "excel":
sbom_document = SpreadsheetBuilder()
else:
sbom_document = ConsoleBuilder()

Expand Down

0 comments on commit 9717e9c

Please sign in to comment.