Skip to content

Commit

Permalink
Merge branch 'main' into introduce-model
Browse files Browse the repository at this point in the history
  • Loading branch information
asofter authored Mar 14, 2024
2 parents 705d17f + 9eb0604 commit 5eea800
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 21 deletions.
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 @@ -291,6 +291,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)

0 comments on commit 5eea800

Please sign in to comment.