Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

always set metadata version to current implemented specification #67

Merged
merged 1 commit into from
Sep 10, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading