Skip to content

Commit

Permalink
Validate that global core:version is present. (#55)
Browse files Browse the repository at this point in the history
Do not overwrite version info when reading files.
  • Loading branch information
anarkiwi committed Apr 12, 2024
1 parent 2410874 commit 1b03f63
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 4 deletions.
5 changes: 1 addition & 4 deletions sigmf/sigmffile.py
Expand Up @@ -174,6 +174,7 @@ def __init__(self, metadata=None, data_file=None, global_info=None, skip_checksu
if metadata is None:
self._metadata = {self.GLOBAL_KEY:{}, self.CAPTURE_KEY:[], self.ANNOTATION_KEY:[]}
self._metadata[self.GLOBAL_KEY][self.NUM_CHANNELS_KEY] = 1
self._metadata[self.GLOBAL_KEY][self.VERSION_KEY] = "1.0.0"
elif isinstance(metadata, dict):
self._metadata = metadata
else:
Expand All @@ -183,8 +184,6 @@ def __init__(self, metadata=None, data_file=None, global_info=None, skip_checksu
if data_file is not None:
self.set_data_file(data_file, skip_checksum=skip_checksum, map_readonly=map_readonly)

self._metadata[self.GLOBAL_KEY][self.VERSION_KEY] = '1.0.0'

def __len__(self):
return self._memmap.shape[0]

Expand Down Expand Up @@ -724,8 +723,6 @@ def __init__(self, metafiles=None, metadata=None, skip_checksums=False):
if not self.skip_checksums:
self.verify_stream_hashes()

self._metadata[self.COLLECTION_KEY][self.VERSION_KEY] = '1.0.0'

def __len__(self):
'''
the length of a collection is the number of streams
Expand Down
1 change: 1 addition & 0 deletions tests/conftest.py
Expand Up @@ -40,6 +40,7 @@ def test_sigmffile(test_data_file):
"""If pytest uses this signature, will return valid SigMF file."""
sigf = SigMFFile()
sigf.set_global_field("core:datatype", "rf32_le")
sigf.set_global_field("core:version", "1.0.0")
sigf.add_annotation(start_index=0, length=len(TEST_FLOAT32_DATA))
sigf.add_capture(start_index=0)
sigf.set_data_file(test_data_file.name)
Expand Down
1 change: 1 addition & 0 deletions tests/test_archivereader.py
Expand Up @@ -42,6 +42,7 @@ def test_access_data_without_untar(self):
global_info={
SigMFFile.DATATYPE_KEY: f"{complex_prefix}{key}_le",
SigMFFile.NUM_CHANNELS_KEY: num_channels,
SigMFFile.VERSION_KEY: "1.0.0",
},
)
temp_meta.tofile(temp_archive, toarchive=True)
Expand Down
6 changes: 6 additions & 0 deletions tests/test_validation.py
Expand Up @@ -39,6 +39,12 @@ class FailingCases(unittest.TestCase):
def setUp(self):
self.metadata = dict(TEST_METADATA)

def test_no_version(self):
'''core:version must be present'''
del self.metadata[SigMFFile.GLOBAL_KEY][SigMFFile.VERSION_KEY]
with self.assertRaises(ValidationError):
SigMFFile(self.metadata).validate()

def test_extra_top_level_key(self):
'''no extra keys allowed on the top level'''
self.metadata['extra'] = 0
Expand Down

0 comments on commit 1b03f63

Please sign in to comment.