|
| 1 | +"""Summary report for HED schema compliance checking.""" |
| 2 | + |
| 3 | +from hed.schema.hed_schema_constants import HedSectionKey |
| 4 | + |
| 5 | +# Section display names for readable output |
| 6 | +_SECTION_DISPLAY_NAMES = { |
| 7 | + HedSectionKey.Tags: "Tags", |
| 8 | + HedSectionKey.UnitClasses: "Unit Classes", |
| 9 | + HedSectionKey.Units: "Units", |
| 10 | + HedSectionKey.UnitModifiers: "Unit Modifiers", |
| 11 | + HedSectionKey.ValueClasses: "Value Classes", |
| 12 | + HedSectionKey.Attributes: "Attributes", |
| 13 | + HedSectionKey.Properties: "Properties", |
| 14 | +} |
| 15 | + |
| 16 | + |
| 17 | +class ComplianceSummary: |
| 18 | + """Tracks what was checked during schema compliance validation and the results. |
| 19 | +
|
| 20 | + This provides a structured report of all checks performed, how many entries |
| 21 | + were examined, and how many issues were found per check category. |
| 22 | +
|
| 23 | + Use ``get_summary()`` for a human-readable text report, or access |
| 24 | + ``check_results`` directly for programmatic use. |
| 25 | + """ |
| 26 | + |
| 27 | + def __init__(self, schema_name="", schema_version=""): |
| 28 | + """Initialize a ComplianceSummary. |
| 29 | +
|
| 30 | + Parameters: |
| 31 | + schema_name (str): Display name for the schema being checked. |
| 32 | + schema_version (str): The schema version string. |
| 33 | + """ |
| 34 | + self.schema_name = schema_name |
| 35 | + self.schema_version = schema_version |
| 36 | + self.check_results = [] |
| 37 | + self._current_check = None |
| 38 | + |
| 39 | + def start_check(self, check_name, description=""): |
| 40 | + """Begin tracking a new compliance check. |
| 41 | +
|
| 42 | + Parameters: |
| 43 | + check_name (str): Short identifier for the check (e.g. "prerelease_version"). |
| 44 | + description (str): Human-readable description of what this check validates. |
| 45 | + """ |
| 46 | + self._current_check = { |
| 47 | + "name": check_name, |
| 48 | + "description": description, |
| 49 | + "sections_checked": {}, |
| 50 | + "entries_checked": 0, |
| 51 | + "entries_skipped": 0, |
| 52 | + "issue_count": 0, |
| 53 | + "sub_checks": [], |
| 54 | + } |
| 55 | + self.check_results.append(self._current_check) |
| 56 | + |
| 57 | + def record_section(self, section_key, entries_checked, entries_skipped=0): |
| 58 | + """Record that a section was examined during the current check. |
| 59 | +
|
| 60 | + Parameters: |
| 61 | + section_key (HedSectionKey or str): The section that was checked. |
| 62 | + entries_checked (int): Number of entries examined in this section. |
| 63 | + entries_skipped (int): Number of entries skipped (e.g. deprecated). |
| 64 | + """ |
| 65 | + if self._current_check is None: |
| 66 | + return |
| 67 | + key = str(section_key) |
| 68 | + self._current_check["sections_checked"][key] = { |
| 69 | + "entries_checked": entries_checked, |
| 70 | + "entries_skipped": entries_skipped, |
| 71 | + } |
| 72 | + self._current_check["entries_checked"] += entries_checked |
| 73 | + self._current_check["entries_skipped"] += entries_skipped |
| 74 | + |
| 75 | + def add_sub_check(self, sub_check_name): |
| 76 | + """Record a named sub-check within the current check. |
| 77 | +
|
| 78 | + Parameters: |
| 79 | + sub_check_name (str): Name of the sub-check (e.g. an attribute validator name). |
| 80 | + """ |
| 81 | + if self._current_check is None: |
| 82 | + return |
| 83 | + if sub_check_name not in self._current_check["sub_checks"]: |
| 84 | + self._current_check["sub_checks"].append(sub_check_name) |
| 85 | + |
| 86 | + def record_issues(self, issue_count): |
| 87 | + """Record issues found during the current check. |
| 88 | +
|
| 89 | + Parameters: |
| 90 | + issue_count (int): Number of issues found. |
| 91 | + """ |
| 92 | + if self._current_check is None: |
| 93 | + return |
| 94 | + self._current_check["issue_count"] += issue_count |
| 95 | + |
| 96 | + @property |
| 97 | + def total_issues(self): |
| 98 | + """Return total issues across all checks. |
| 99 | +
|
| 100 | + Returns: |
| 101 | + int: Total number of issues found. |
| 102 | + """ |
| 103 | + return sum(c["issue_count"] for c in self.check_results) |
| 104 | + |
| 105 | + @property |
| 106 | + def total_entries_checked(self): |
| 107 | + """Return total entries checked across all checks. |
| 108 | +
|
| 109 | + Returns: |
| 110 | + int: Total number of entries examined. |
| 111 | + """ |
| 112 | + return sum(c["entries_checked"] for c in self.check_results) |
| 113 | + |
| 114 | + def get_summary(self, verbose=True): |
| 115 | + """Return a human-readable summary of all compliance checks. |
| 116 | +
|
| 117 | + Parameters: |
| 118 | + verbose (bool): If True, include per-section breakdowns and sub-check lists. |
| 119 | +
|
| 120 | + Returns: |
| 121 | + str: Formatted multi-line summary report. |
| 122 | + """ |
| 123 | + lines = [] |
| 124 | + lines.append("=" * 70) |
| 125 | + lines.append("HED Schema Compliance Report") |
| 126 | + lines.append("=" * 70) |
| 127 | + if self.schema_name: |
| 128 | + lines.append(f"Schema: {self.schema_name}") |
| 129 | + if self.schema_version: |
| 130 | + lines.append(f"Version: {self.schema_version}") |
| 131 | + lines.append(f"Total issues found: {self.total_issues}") |
| 132 | + lines.append("") |
| 133 | + |
| 134 | + for i, check in enumerate(self.check_results, 1): |
| 135 | + status = "PASS" if check["issue_count"] == 0 else f"FAIL ({check['issue_count']} issues)" |
| 136 | + lines.append(f"{i}. [{status}] {check['name']}") |
| 137 | + if check["description"]: |
| 138 | + lines.append(f" {check['description']}") |
| 139 | + |
| 140 | + if verbose: |
| 141 | + if check["entries_checked"] > 0 or check["entries_skipped"] > 0: |
| 142 | + parts = [f"{check['entries_checked']} entries checked"] |
| 143 | + if check["entries_skipped"] > 0: |
| 144 | + parts.append(f"{check['entries_skipped']} skipped") |
| 145 | + lines.append(f" ({', '.join(parts)})") |
| 146 | + |
| 147 | + if check["sections_checked"] and verbose: |
| 148 | + for section_str, info in check["sections_checked"].items(): |
| 149 | + display_name = section_str |
| 150 | + # Try to get a nice display name |
| 151 | + for sk, dn in _SECTION_DISPLAY_NAMES.items(): |
| 152 | + if str(sk) == section_str: |
| 153 | + display_name = dn |
| 154 | + break |
| 155 | + skip_note = f", {info['entries_skipped']} skipped" if info["entries_skipped"] else "" |
| 156 | + lines.append(f" - {display_name}: {info['entries_checked']} checked{skip_note}") |
| 157 | + |
| 158 | + if check["sub_checks"]: |
| 159 | + lines.append(" Sub-checks performed:") |
| 160 | + for sc in check["sub_checks"]: |
| 161 | + lines.append(f" - {sc}") |
| 162 | + lines.append("") |
| 163 | + |
| 164 | + # Summary of what is NOT checked |
| 165 | + lines.append("-" * 70) |
| 166 | + lines.append("Known gaps (not currently checked):") |
| 167 | + lines.append(" - BoolRange attribute validation") |
| 168 | + lines.append(" - Missing descriptions on entries") |
| 169 | + lines.append(" - SuggestedTag/RelatedTag existence (8.3+ schemas)") |
| 170 | + lines.append(" - Unit class must have at least one unit") |
| 171 | + lines.append(" - DefaultUnits must be in the tag's own unit classes") |
| 172 | + lines.append(" - HedID uniqueness across entries") |
| 173 | + lines.append(" - HedID completeness (all entries should have IDs)") |
| 174 | + lines.append(" - Attributes must have exactly one range type") |
| 175 | + lines.append(" - Attributes must have at least one domain") |
| 176 | + lines.append(" - Reserved tag semantics") |
| 177 | + lines.append(" - Prologue/epilogue existence for released schemas") |
| 178 | + lines.append(" - StringRange value validation") |
| 179 | + lines.append("=" * 70) |
| 180 | + return "\n".join(lines) |
| 181 | + |
| 182 | + def __str__(self): |
| 183 | + return self.get_summary(verbose=False) |
0 commit comments