-
Notifications
You must be signed in to change notification settings - Fork 77
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
Reference implementation of archive format from issue #15 #57
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
4c0c4b0
Remove prints, use relative imports, and other fixes
djanderson bf4b4ff
Add custom exception classes
djanderson b562b6b
Add a more robust testing framework
djanderson 4ad8122
Bring schema inline with current spec
djanderson 4886d47
Add reference implementation of SigMF archive format from issue #15
djanderson a5b3e33
Catch unwritable filename or fileobj input to SigMFArchive and raise …
djanderson 910d263
Add failing test case for adding multiple annotations
djanderson 879ddf9
Revert change causing test failure
djanderson File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,5 @@ | ||
[aliases] | ||
test=pytest | ||
|
||
[wheel] | ||
universal = 1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,166 @@ | ||
# Copyright 2017 GNU Radio Foundation | ||
# | ||
# Permission is hereby granted, free of charge, to any person obtaining a copy | ||
# of this software and associated documentation files (the "Software"), to deal | ||
# in the Software without restriction, including without limitation the rights | ||
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
# copies of the Software, and to permit persons to whom the Software is | ||
# furnished to do so, subject to the following conditions: | ||
# | ||
# The above copyright notice and this permission notice shall be included in | ||
# all copies or substantial portions of the Software. | ||
# | ||
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
# SOFTWARE. | ||
|
||
"""Create and extract SigMF archives.""" | ||
|
||
import os | ||
import shutil | ||
import tarfile | ||
import tempfile | ||
|
||
from . import error | ||
|
||
|
||
SIGMF_ARCHIVE_EXT = ".sigmf" | ||
SIGMF_METADATA_EXT = ".sigmf-meta" | ||
SIGMF_DATASET_EXT = ".sigmf-data" | ||
|
||
|
||
class SigMFArchive(object): | ||
"""Archive a SigMFFile. | ||
|
||
A `.sigmf` file must include both valid metadata and data. If metadata | ||
is not valid, raise `SigMFValidationError`. If `self.data_file` is not | ||
set or the requested output file is not writable, raise `SigMFFileError`. | ||
|
||
Parameters: | ||
|
||
sigmffile -- A SigMFFile object with valid metadata and data_file | ||
|
||
name -- path to archive file to create. If file exists, overwrite. | ||
If `name` doesn't end in .sigmf, it will be appended. | ||
For example: if `name` == "/tmp/archive1", then the | ||
following archive will be created: | ||
/tmp/archive1.sigmf | ||
- archive1/ | ||
- archive1.sigmf-meta | ||
- archive1.sigmf-data | ||
|
||
fileobj -- If `fileobj` is specified, it is used as an alternative to | ||
a file object opened in binary mode for `name`. It is | ||
supposed to be at position 0. `name` is not required, but | ||
if specified will be used to determine the directory and | ||
file names within the archive. `fileobj` won't be closed. | ||
For example: if `name` == "archive1" and fileobj is given, | ||
a tar archive will be written to fileobj with the | ||
following structure: | ||
- archive1/ | ||
- archive1.sigmf-meta | ||
- archive1.sigmf-data | ||
|
||
""" | ||
def __init__(self, sigmffile, name=None, fileobj=None): | ||
self.sigmffile = sigmffile | ||
self.name = name | ||
self.fileobj = fileobj | ||
|
||
self._check_input() | ||
|
||
archive_name = self._get_archive_name() | ||
sigmf_fileobj = self._get_output_fileobj() | ||
sigmf_archive = tarfile.TarFile(mode="w", fileobj=sigmf_fileobj) | ||
tmpdir = tempfile.mkdtemp() | ||
sigmf_md_filename = archive_name + SIGMF_METADATA_EXT | ||
sigmf_md_path = os.path.join(tmpdir, sigmf_md_filename) | ||
sigmf_data_filename = archive_name + SIGMF_DATASET_EXT | ||
sigmf_data_path = os.path.join(tmpdir, sigmf_data_filename) | ||
|
||
with open(sigmf_md_path, "w") as mdfile: | ||
self.sigmffile.dump(mdfile, pretty=True) | ||
|
||
shutil.copy(self.sigmffile.data_file, sigmf_data_path) | ||
|
||
def chmod(tarinfo): | ||
if tarinfo.isdir(): | ||
tarinfo.mode = 0o755 # dwrxw-rw-r | ||
else: | ||
tarinfo.mode = 0o644 # -wr-r--r-- | ||
return tarinfo | ||
|
||
sigmf_archive.add(tmpdir, arcname=archive_name, filter=chmod) | ||
sigmf_archive.close() | ||
if not fileobj: | ||
sigmf_fileobj.close() | ||
|
||
shutil.rmtree(tmpdir) | ||
|
||
self.path = sigmf_archive.name | ||
|
||
def _check_input(self): | ||
self._ensure_name_has_correct_extension() | ||
self._ensure_data_file_set() | ||
self._validate_sigmffile_metadata() | ||
|
||
def _ensure_name_has_correct_extension(self): | ||
name = self.name | ||
if name is None: | ||
return | ||
|
||
has_extension = "." in name | ||
has_correct_extension = name.endswith(SIGMF_ARCHIVE_EXT) | ||
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) | ||
|
||
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) | ||
|
||
def _validate_sigmffile_metadata(self): | ||
valid_md = self.sigmffile.validate() | ||
if not valid_md: | ||
err = "invalid metadata - {!s}" | ||
raise error.SigMFValidationError(err.format(valid_md)) | ||
|
||
def _get_archive_name(self): | ||
if self.fileobj and not self.name: | ||
pathname = self.fileobj.name | ||
else: | ||
pathname = self.name | ||
|
||
filename = os.path.split(pathname)[-1] | ||
archive_name, archive_ext = os.path.splitext(filename) | ||
return archive_name | ||
|
||
def _get_output_fileobj(self): | ||
try: | ||
fileobj = self._get_open_fileobj() | ||
except: | ||
if self.fileobj: | ||
e = "fileobj {!r} is not byte-writable".format(self.fileobj) | ||
else: | ||
e = "can't open {!r} for writing".format(self.name) | ||
|
||
raise error.SigMFFileError(e) | ||
|
||
return fileobj | ||
|
||
def _get_open_fileobj(self): | ||
if self.fileobj: | ||
fileobj = self.fileobj | ||
fileobj.write(bytes()) # force exception if not byte-writable | ||
else: | ||
fileobj = open(self.name, "wb") | ||
|
||
return fileobj |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
"""Defines SigMF exception classes.""" | ||
|
||
|
||
class SigMFError(Exception): | ||
""" SigMF base exception.""" | ||
pass | ||
|
||
|
||
class SigMFValidationError(SigMFError): | ||
"""Exceptions related to validating SigMF metadata.""" | ||
pass | ||
|
||
|
||
class SigMFFileError(SigMFError): | ||
"""Exceptions related to reading or writing SigMF archives.""" | ||
pass |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't we wrap this with try/catch in case we don't have write permissions?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In this case,
sigmf_md_path
is being created undertmpdir
, which is guaranteed to be "readable, writable, and searchable only by the creating user ID." (mkdtemp), so I think we're 100% safe here.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You're right, good call.