Skip to content

Commit f9794e9

Browse files
Merge branch 'dev' into fix_session_start_time_bug
2 parents 8d3e299 + 63b2d85 commit f9794e9

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
lines changed

nwbinspector/inspector_tools.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,29 @@
33
import sys
44
from typing import Dict, List
55
from pathlib import Path
6-
from natsort import natsorted
76
from enum import Enum
7+
from datetime import datetime
8+
from platform import platform
9+
10+
from natsort import natsorted
811

912
from .register_checks import InspectorMessage
1013
from .utils import FilePathType
1114

15+
try:
16+
from importlib.metadata import version
17+
18+
inspector_version = version("nwbinspector")
19+
except ModuleNotFoundError: # Remove the except clause when minimal supported version becomes 3.8
20+
from pkg_resources import get_distribution
21+
22+
inspector_version = get_distribution("nwbinspector").version
23+
24+
25+
def get_report_header():
26+
"""Grab basic information from system at time of report generation."""
27+
return dict(Timestamp=str(datetime.now().astimezone()), Platform=platform(), NWBInspector_version=inspector_version)
28+
1229

1330
def _sort_unique_values(unique_values: list, reverse: bool = False):
1431
"""Technically, the 'set' method applies basic sorting to the unique contents, but natsort is more general."""

nwbinspector/nwbinspector.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
from . import available_checks
2020
from .inspector_tools import (
21+
get_report_header,
2122
organize_messages,
2223
format_organized_results_output,
2324
print_to_console,
@@ -201,8 +202,12 @@ def inspect_all_cli(
201202
)
202203
)
203204
if json_file_path is not None:
205+
if Path(json_file_path).exists() and not overwrite:
206+
raise FileExistsError(f"The file {json_file_path} already exists! Specify the '-o' flag to overwrite.")
204207
with open(file=json_file_path, mode="w") as fp:
205-
json.dump(obj=messages, fp=fp, cls=InspectorOutputJSONEncoder)
208+
json_report = dict(header=get_report_header(), messages=messages)
209+
json.dump(obj=json_report, fp=fp, cls=InspectorOutputJSONEncoder)
210+
print(f"{os.linesep*2}Report saved to {str(Path(json_file_path).absolute())}!{os.linesep}")
206211
if len(messages):
207212
organized_results = organize_messages(messages=messages, levels=["file_path", "importance"])
208213
formatted_results = format_organized_results_output(organized_results=organized_results)
@@ -342,6 +347,7 @@ def inspect_nwb(
342347
)
343348
nwbfile_path = str(nwbfile_path)
344349
filterwarnings(action="ignore", message="No cached namespaces found in .*")
350+
filterwarnings(action="ignore", message="Ignoring cached namespace .*")
345351
with pynwb.NWBHDF5IO(path=nwbfile_path, mode="r", load_namespaces=True, driver=driver) as io:
346352
if skip_validate:
347353
validation_errors = pynwb.validate(io=io)

0 commit comments

Comments
 (0)