diff --git a/sigmf/archive.py b/sigmf/archive.py index b60090b..86c2ecc 100644 --- a/sigmf/archive.py +++ b/sigmf/archive.py @@ -11,7 +11,7 @@ import tarfile import tempfile -from . import error +from .error import SigMFFileError SIGMF_ARCHIVE_EXT = ".sigmf" @@ -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() @@ -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 diff --git a/sigmf/archivereader.py b/sigmf/archivereader.py index 8eac4f5..5759b74 100644 --- a/sigmf/archivereader.py +++ b/sigmf/archivereader.py @@ -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(): @@ -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) @@ -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) diff --git a/sigmf/sigmffile.py b/sigmf/sigmffile.py index 117cf68..ee35d04 100644 --- a/sigmf/sigmffile.py +++ b/sigmf/sigmffile.py @@ -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):