Skip to content

Commit

Permalink
use SigMFArchiveReader for fromfile()
Browse files Browse the repository at this point in the history
  • Loading branch information
Jacob Gilbert authored and jacobagilbert committed Jan 16, 2023
1 parent 83d8e11 commit 3b69e03
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 37 deletions.
8 changes: 4 additions & 4 deletions sigmf/archive.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import tarfile
import tempfile

from . import error
from .error import SigMFFileError


SIGMF_ARCHIVE_EXT = ".sigmf"
Expand Down Expand Up @@ -106,14 +106,14 @@ def _ensure_name_has_correct_extension(self):
if has_extension and not has_correct_extension:
apparent_ext = os.path.splitext(name)[-1]
err = "extension {} != {}".format(apparent_ext, SIGMF_ARCHIVE_EXT)
raise error.SigMFFileError(err)
raise SigMFFileError(err)

self.name = name if has_correct_extension else name + SIGMF_ARCHIVE_EXT

def _ensure_data_file_set(self):
if not self.sigmffile.data_file:
err = "no data file - use `set_data_file`"
raise error.SigMFFileError(err)
raise SigMFFileError(err)

def _validate_sigmffile_metadata(self):
self.sigmffile.validate()
Expand All @@ -137,7 +137,7 @@ def _get_output_fileobj(self):
else:
err = "can't open {!r} for writing".format(self.name)

raise error.SigMFFileError(err)
raise SigMFFileError(err)

return fileobj

Expand Down
8 changes: 3 additions & 5 deletions sigmf/archivereader.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
from .sigmffile import SigMFFile
from .archive import SigMFArchive, SIGMF_DATASET_EXT, SIGMF_METADATA_EXT, SIGMF_ARCHIVE_EXT
from .utils import dict_merge
from .error import SigMFFileError, SigMFAccessError
from .error import SigMFFileError


class SigMFArchiveReader():
Expand All @@ -31,7 +31,7 @@ def __init__(self, name=None, skip_checksum=False, map_readonly=True, archive_bu
if self.name is not None:
if not name.endswith(SIGMF_ARCHIVE_EXT):
err = "archive extension != {}".format(SIGMF_ARCHIVE_EXT)
raise error.SigMFFileError(err)
raise SigMFFileError(err)

tar_obj = tarfile.open(self.name)

Expand Down Expand Up @@ -69,12 +69,10 @@ def __init__(self, name=None, skip_checksum=False, map_readonly=True, archive_bu
print('A member of type', memb.type, 'and name', memb.name, 'was found but not handled, just FYI.')

if data_offset_size is None:
raise error.SigMFFileError('No .sigmf-data file found in archive!')
raise SigMFFileError('No .sigmf-data file found in archive!')

self.sigmffile = SigMFFile(metadata=json_contents)
valid_md = self.sigmffile.validate()
if not valid_md:
print('Metadata in archive did not .validate()!')

self.sigmffile.set_data_file(self.name, data_buffer=archive_buffer, skip_checksum=skip_checksum, offset=data_offset_size[0],
size_bytes=data_offset_size[1], map_readonly=map_readonly)
Expand Down
32 changes: 4 additions & 28 deletions sigmf/sigmffile.py
Original file line number Diff line number Diff line change
Expand Up @@ -891,35 +891,11 @@ def get_dataset_filename_from_metadata(meta_fn, metadata=None):
def fromarchive(archive_path, dir=None):
"""Extract an archive and return a SigMFFile.
If `dir` is given, extract the archive to that directory. Otherwise,
the archive will be extracted to a temporary directory. For example,
`dir` == "." will extract the archive into the current working
directory.
The `dir` parameter is no longer used as this function has been changed to
access SigMF archives without extracting them.
"""
if not dir:
dir = tempfile.mkdtemp()

archive = tarfile.open(archive_path, mode="r", format=tarfile.PAX_FORMAT)
members = archive.getmembers()

try:
archive.extractall(path=dir)

data_file = None
metadata = None

for member in members:
if member.name.endswith(SIGMF_METADATA_EXT):
bytestream_reader = codecs.getreader("utf-8") # bytes -> str
mdfile_reader = bytestream_reader(archive.extractfile(member))
metadata = json.load(mdfile_reader)
data_file = get_dataset_filename_from_metadata(member.name, metadata)
else:
archive.extractfile(member)
finally:
archive.close()

return SigMFFile(metadata=metadata, data_file=data_file)
from .archivereader import SigMFArchiveReader
return SigMFArchiveReader(archive_path).sigmffile


def fromfile(filename, skip_checksum=False):
Expand Down

0 comments on commit 3b69e03

Please sign in to comment.