|
| 1 | +function validateEmbeddedSpecifications(h5_file_id, expectedNamespaceNames) |
| 2 | +% validateEmbeddedSpecifications - Validate the embedded specifications |
| 3 | +% |
| 4 | +% This function does two things: |
| 5 | +% 1) Displays a warning if specifications of expected namespaces |
| 6 | +% are not embedded in the file. |
| 7 | +% E.g if cached namespaces were cleared prior to export. |
| 8 | +% |
| 9 | +% 2) Deletes specifications for unused namespaces that are embedded. |
| 10 | +% - E.g. If neurodata type from an embedded namespace was removed and the |
| 11 | +% file was re-exported |
| 12 | + |
| 13 | +% NB: Input h5_file_id must point to a file opened with write access |
| 14 | + |
| 15 | + specLocation = io.spec.internal.readEmbeddedSpecLocation(h5_file_id); |
| 16 | + embeddedNamespaceNames = io.internal.h5.listGroupNames(h5_file_id, specLocation); |
| 17 | + |
| 18 | + checkMissingNamespaces(expectedNamespaceNames, embeddedNamespaceNames) |
| 19 | + |
| 20 | + unusedNamespaces = checkUnusedNamespaces(... |
| 21 | + expectedNamespaceNames, embeddedNamespaceNames); |
| 22 | + |
| 23 | + if ~isempty(unusedNamespaces) |
| 24 | + deleteUnusedNamespaces(h5_file_id, unusedNamespaces, specLocation) |
| 25 | + end |
| 26 | +end |
| 27 | + |
| 28 | +function checkMissingNamespaces(expectedNamespaceNames, embeddedNamespaceNames) |
| 29 | +% checkMissingNamespaces - Check if any namespace specs are missing from the file |
| 30 | + missingNamespaces = setdiff(expectedNamespaceNames, embeddedNamespaceNames); |
| 31 | + if ~isempty(missingNamespaces) |
| 32 | + missingNamespacesStr = strjoin(" " + string(missingNamespaces), newline); |
| 33 | + warning('NWB:validators:MissingEmbeddedNamespace', 'Namespace is missing:\n%s', missingNamespacesStr) |
| 34 | + end |
| 35 | +end |
| 36 | + |
| 37 | +function unusedNamespaces = checkUnusedNamespaces(expectedNamespaceNames, embeddedNamespaceNames) |
| 38 | +% checkUnusedNamespaces - Check if any namespace specs in the file are unused |
| 39 | + unusedNamespaces = setdiff(embeddedNamespaceNames, expectedNamespaceNames); |
| 40 | +end |
| 41 | + |
| 42 | +function deleteUnusedNamespaces(fileId, unusedNamespaces, specRootLocation) |
| 43 | + for i = 1:numel(unusedNamespaces) |
| 44 | + thisName = unusedNamespaces{i}; |
| 45 | + namespaceSpecLocation = strjoin( {specRootLocation, thisName}, '/'); |
| 46 | + io.internal.h5.deleteGroup(fileId, namespaceSpecLocation) |
| 47 | + end |
| 48 | +end |
0 commit comments