Skip to content

Commit

Permalink
style(logging): ⚡ use lazy concetenation for logging
Browse files Browse the repository at this point in the history
  • Loading branch information
CandiedCode committed Apr 6, 2024
1 parent 0535ae8 commit 28129d9
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 20 deletions.
6 changes: 5 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -128,4 +128,8 @@ cython_debug/
.DS_Store

.vscode/
.idea/
.idea/

# Notebook Model Downloads
notebooks/PyTorchModels/
pytorch-model-scan-results.json
11 changes: 6 additions & 5 deletions modelscan/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ def scan(
settings = DEFAULT_SETTINGS

if settings_file_path and settings_file_path.is_file():
with open(settings_file_path) as sf:
with open(settings_file_path, encoding="utf-8") as sf:
settings = parse(sf.read()).unwrap()
click.echo(f"Detected settings file. Using {settings_file_path}. \n")
else:
Expand Down Expand Up @@ -174,16 +174,17 @@ def create_settings(force: bool, location: Optional[str]) -> None:
settings_path = location

try:
open(settings_path)
open(settings_path, encoding="utf-8")
if force:
with open(settings_path, "w") as settings_file:
with open(settings_path, mode="w", encoding="utf-8") as settings_file:
settings_file.write(SettingsUtils.get_default_settings_as_toml())
else:
logger.warning(
f"{settings_path} file already exists. Please use `--force` flag if you intend to overwrite it."
"%s file already exists. Please use `--force` flag if you intend to overwrite it.",
settings_path,
)
except FileNotFoundError:
with open(settings_path, "w") as settings_file:
with open(settings_path, mode="w", encoding="utf-8") as settings_file:
settings_file.write(SettingsUtils.get_default_settings_as_toml())


Expand Down
2 changes: 1 addition & 1 deletion modelscan/issues.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ def print(self) -> None:
if self.code == IssueCode.UNSAFE_OPERATOR:
issue_description = "Unsafe operator"
else:
logger.error(f"No issue description for issue code ${self.code}")
logger.error("No issue description for issue code %s", self.code)

print(f"\n{issue_description} found:")
print(f" - Severity: {self.severity.name}")
Expand Down
28 changes: 17 additions & 11 deletions modelscan/modelscan.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ def _load_scanners(self) -> None:
self._scanners_to_run.append(scanner_class)

except Exception as e:
logger.error(f"Error importing scanner {scanner_path}")
logger.error("Error importing scanner %s", scanner_path)
self._init_errors.append(
ModelScanError(
scanner_path,
Expand All @@ -81,7 +81,7 @@ def _load_scanners(self) -> None:

def _iterate_models(self, model_path: Path) -> Generator[Model, None, None]:
if not model_path.exists():
logger.error(f"Path {model_path} does not exist")
logger.error("Path %s does not exist", model_path)
self._errors.append(
ModelScanError(
"ModelScan",
Expand All @@ -93,7 +93,7 @@ def _iterate_models(self, model_path: Path) -> Generator[Model, None, None]:

files = [model_path]
if model_path.is_dir():
logger.debug(f"Path {str(model_path)} is a directory")
logger.debug("Path %s is a directory", str(model_path))
files = [f for f in model_path.rglob("*") if Path.is_file(f)]

for file in files:
Expand Down Expand Up @@ -127,15 +127,15 @@ def _iterate_models(self, model_path: Path) -> Generator[Model, None, None]:
yield Model(file_name, file_io)
except zipfile.BadZipFile as e:
logger.debug(
f"Skipping zip file {str(model.get_source())}, due to error",
e,
"Skipping zip file %s, due to error",
str(model.get_source()),
exc_info=True,
)
self._skipped.append(
ModelScanSkipped(
"ModelScan",
SkipCategories.BAD_ZIP,
f"Skipping zip file due to error: {e}",
"Skipping zip file due to error: %s" % e,
str(model.get_source()),
)
)
Expand Down Expand Up @@ -189,13 +189,17 @@ def _scan_source(
scan_results = scanner.scan(model)
except Exception as e:
logger.error(
f"Error encountered from scanner {scanner.full_name()} with path {str(model.get_source())}: {e}"
"Error encountered from scanner %s with path %s: %s",
scanner.full_name(),
str(model.get_source()),
e,
)
self._errors.append(
ModelScanError(
scanner.full_name(),
ErrorCategories.MODEL_SCAN,
f"Error encountered from scanner {scanner.full_name()}: {e}",
"Error encountered from scanner %s: %s"
% (scanner.full_name(), e),
str(model.get_source()),
)
)
Expand All @@ -204,7 +208,9 @@ def _scan_source(
if scan_results is not None:
scanned = True
logger.info(
f"Scanning {model.get_source()} using {scanner.full_name()} model scan"
"Scanning %s using %s model scan",
model.get_source(),
scanner.full_name(),
)
if scan_results.errors:
self._errors.extend(scan_results.errors)
Expand Down Expand Up @@ -343,12 +349,12 @@ def generate_report(self) -> Optional[str]:
scan_report = report_class.generate(scan=self, settings=report_settings)

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

Expand Down
3 changes: 1 addition & 2 deletions notebooks/utils/pickle_codeinjection.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@
import struct
import os
from typing import overload

from git import Union
from typing import Union


class PickleInject:
Expand Down

0 comments on commit 28129d9

Please sign in to comment.