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

Add generate_report() #114

Merged
merged 4 commits into from
Mar 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
27 changes: 8 additions & 19 deletions modelscan/cli.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
import logging
import sys
import os
import importlib
from pathlib import Path
from typing import Optional, Dict, Any
from tomlkit import parse

import click

from modelscan.modelscan import ModelScan
from modelscan.reports import Report
from modelscan._version import __version__
from modelscan.settings import (
SettingsUtils,
Expand Down Expand Up @@ -133,25 +131,16 @@ def scan(
else:
raise click.UsageError("Command line must include a path")

report_settings: Dict[str, Any] = {}
if reporting_format == "custom":
reporting_module = settings["reporting"]["module"] # type: ignore[index]
else:
reporting_module = DEFAULT_REPORTING_MODULES[reporting_format]

report_settings = settings["reporting"]["settings"] # type: ignore[index]
report_settings["show_skipped"] = show_skipped
report_settings["output_file"] = output_file
# Report scan results
if reporting_format is not "custom":
modelscan._settings["reporting"]["module"] = DEFAULT_REPORTING_MODULES[
reporting_format
]

try:
(modulename, classname) = reporting_module.rsplit(".", 1)
imported_module = importlib.import_module(name=modulename, package=classname)
modelscan._settings["reporting"]["settings"]["show_skipped"] = show_skipped
modelscan._settings["reporting"]["settings"]["output_file"] = output_file

report_class: Report = getattr(imported_module, classname)
report_class.generate(scan=modelscan, settings=report_settings)

except Exception as e:
logger.error(f"Error generating report using {reporting_module}: {e}")
modelscan.generate_report()

# exit code 3 if no supported files were passed
if not modelscan.scanned:
Expand Down
26 changes: 26 additions & 0 deletions modelscan/modelscan.py
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,32 @@ def is_compatible(self, path: str) -> bool:

return False

def generate_report(self) -> Optional[str]:
reporting_module = self._settings["reporting"]["module"]
report_settings = self._settings["reporting"]["settings"]

scan_report = None
try:
(modulename, classname) = reporting_module.rsplit(".", 1)
imported_module = importlib.import_module(
name=modulename, package=classname
)

report_class = getattr(imported_module, classname)
scan_report = report_class.generate(scan=self, settings=report_settings)

except Exception as e:
logger.error(f"Error generating report using {reporting_module}: {e}")
self._errors.append(
ModelScanError(
"ModelScan",
ErrorCategories.MODEL_SCAN,
f"Error generating report using {reporting_module}: {e}",
)
)

return scan_report

@property
def issues(self) -> Issues:
return self._issues
Expand Down
4 changes: 2 additions & 2 deletions modelscan/reports.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,12 +88,12 @@ def generate(
settings: Dict[str, Any] = {},
) -> None:
report: Dict[str, Any] = scan._generate_results()
if not settings["show_skipped"]:
if not settings.get("show_skipped"):
del report["summary"]["skipped"]

print(json.dumps(report))

output = settings["output_file"]
output = settings.get("output_file")
if output:
with open(output, "w") as outfile:
json.dump(report, outfile)
Loading