Skip to content

Commit

Permalink
always set metadata version to current implemented specification
Browse files Browse the repository at this point in the history
  • Loading branch information
Teque5 committed Sep 10, 2024
1 parent f1a3f47 commit 050c497
Showing 1 changed file with 24 additions and 10 deletions.
34 changes: 24 additions & 10 deletions sigmf/sigmffile.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
SIGMF_METADATA_EXT,
SigMFArchive,
)
from .error import SigMFAccessError, SigMFFileError
from .error import SigMFAccessError, SigMFError, SigMFFileError
from .utils import dict_merge


Expand All @@ -45,7 +45,7 @@ def __repr__(self):
return f"SigMFFile({self})"

def __iter__(self):
'''special method to iterate through samples'''
"""special method to iterate through samples"""
self.iter_position = 0
return self

Expand Down Expand Up @@ -182,14 +182,7 @@ def __init__(self, metadata=None, data_file=None, global_info=None, skip_checksu
self._memmap = None
self.is_complex_data = False # numpy.iscomplexobj(self._memmap) is not adequate for fixed-point complex case

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] = __specification__
elif isinstance(metadata, dict):
self._metadata = metadata
else:
self._metadata = json.loads(metadata)
self.set_metadata(metadata)
if global_info is not None:
self.set_global_info(global_info)
if data_file is not None:
Expand Down Expand Up @@ -271,6 +264,27 @@ def get_schema(self):
assert isinstance(self.schema, dict)
return self.schema

def set_metadata(self, metadata):
"""
Read provided metadata as either None (empty), string, bytes, or dictionary.
"""
if metadata is None:
# Create empty
self._metadata = {self.GLOBAL_KEY: {}, self.CAPTURE_KEY: [], self.ANNOTATION_KEY: []}
elif isinstance(metadata, dict):
self._metadata = metadata
elif isinstance(metadata, (str, bytes)):
self._metadata = json.loads(metadata)
else:
raise SigMFError("Unable to interpret provided metadata.")

# if num_channels missing, default to 1
if self.get_global_field(self.NUM_CHANNELS_KEY) is None:
self.set_global_field(self.NUM_CHANNELS_KEY, 1)

# set specification version to current implemented version
self.set_global_field(self.VERSION_KEY, __specification__)

def set_global_info(self, new_global):
"""
Overwrite the global info with a new dictionary.
Expand Down

0 comments on commit 050c497

Please sign in to comment.