Skip to content

Commit d4031c0

Browse files
daniestevezTeque5
andauthored
Take filenames with dots into account in get_sigmf_filenames() (#107)
* take filenames with dots into account in get_sigmf_filenames() The function get_sigmf_filenames() does not work correctly if a file contains dots in the filename. These are interpretted as suffixes by pathlib, and a part of the filename can be removed when forming the different SigMF filenames. This fixes the problem by only considering as file extensions the canonical SigMF file extensions. Any other suffixes are treated as part of the filename and not removed. This fixes #106. * fix SigMFCollection.get_SigMFFile() The get_SigMFFile() method was broken because it used ".sigmf_meta" instead of ".sigmf-meta". This rewrites the method to use get_sigmf_filenames(). * test that SigMF files with . in the filename are loaded correctly This adds a unit test that shows that SigMF files like a.sigmf-meta, b.c.sigmf-meta, and d.e.f.sigmf-meta are all loaded correctly. * increment patch version --------- Co-authored-by: Teque5 <teque5@gmail.com>
1 parent b9b6bed commit d4031c0

File tree

3 files changed

+41
-12
lines changed

3 files changed

+41
-12
lines changed

sigmf/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
# SPDX-License-Identifier: LGPL-3.0-or-later
66

77
# version of this python module
8-
__version__ = "1.2.8"
8+
__version__ = "1.2.9"
99
# matching version of the SigMF specification
1010
__specification__ = "1.2.3"
1111

sigmf/sigmffile.py

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -915,13 +915,13 @@ def get_SigMFFile(self, stream_name=None, stream_index=None):
915915
"""
916916
Returns the SigMFFile instance of the specified stream if it exists
917917
"""
918-
metafile = None
919-
if stream_name is not None:
920-
if stream_name in self.get_stream_names():
921-
metafile = stream_name + ".sigmf_meta"
918+
if stream_name is not None and stream_name not in self.get_stream_names():
919+
# invalid stream name
920+
return
922921
if stream_index is not None and stream_index < len(self):
923-
metafile = self.get_stream_names()[stream_index] + ".sigmf_meta"
924-
if metafile is not None:
922+
stream_name = self.get_stream_names()[stream_index]
923+
if stream_name is not None:
924+
metafile = get_sigmf_filenames(stream_name)["meta_fn"]
925925
metafile_path = self.base_path / metafile
926926
return fromfile(metafile_path, skip_checksum=self.skip_checksums)
927927

@@ -1111,11 +1111,26 @@ def get_sigmf_filenames(filename):
11111111
-------
11121112
dict with 'data_fn', 'meta_fn', and 'archive_fn' as keys.
11131113
"""
1114-
stem_path = Path(filename).with_suffix("")
1114+
stem_path = Path(filename)
1115+
# If the path has a sigmf suffix, remove it. Otherwise do not remove the
1116+
# suffix, because the filename might contain '.' characters which are part
1117+
# of the filename rather than an extension.
1118+
sigmf_suffixes = [
1119+
SIGMF_DATASET_EXT, SIGMF_METADATA_EXT,
1120+
SIGMF_ARCHIVE_EXT, SIGMF_COLLECTION_EXT,
1121+
]
1122+
if stem_path.suffix in sigmf_suffixes:
1123+
with_suffix_path = stem_path
1124+
stem_path = stem_path.with_suffix("")
1125+
else:
1126+
# Add a dummy suffix to prevent the .with_suffix() calls below from
1127+
# overriding part of the filename which is interpreted as a suffix
1128+
with_suffix_path = stem_path.with_name(f"{stem_path.name}{SIGMF_DATASET_EXT}")
1129+
11151130
return {
11161131
"base_fn": stem_path,
1117-
"data_fn": stem_path.with_suffix(SIGMF_DATASET_EXT),
1118-
"meta_fn": stem_path.with_suffix(SIGMF_METADATA_EXT),
1119-
"archive_fn": stem_path.with_suffix(SIGMF_ARCHIVE_EXT),
1120-
"collection_fn": stem_path.with_suffix(SIGMF_COLLECTION_EXT),
1132+
"data_fn": with_suffix_path.with_suffix(SIGMF_DATASET_EXT),
1133+
"meta_fn": with_suffix_path.with_suffix(SIGMF_METADATA_EXT),
1134+
"archive_fn": with_suffix_path.with_suffix(SIGMF_ARCHIVE_EXT),
1135+
"collection_fn": with_suffix_path.with_suffix(SIGMF_COLLECTION_EXT),
11211136
}

tests/test_sigmffile.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,20 @@ def test_pathlib_handle(self):
4343
obj_pth = sigmffile.fromfile(self.temp_path_data)
4444
obj_pth.validate()
4545

46+
def test_filenames_with_dots(self):
47+
"""test that filenames with non-extension . characters are handled correctly"""
48+
filenames = ["a", "b.c", "d.e.f"]
49+
for filename in filenames:
50+
temp_path_data = self.temp_dir / f"{filename}.sigmf-data"
51+
temp_path_meta = self.temp_dir / f"{filename}.sigmf-meta"
52+
TEST_FLOAT32_DATA.tofile(temp_path_data)
53+
self.sigmf_object = SigMFFile(TEST_METADATA, data_file=temp_path_data)
54+
self.sigmf_object.tofile(temp_path_meta)
55+
files = [str(temp_path_data), temp_path_data, str(temp_path_meta), temp_path_meta]
56+
for filename in files:
57+
obj = sigmffile.fromfile(filename)
58+
obj.validate()
59+
4660
def test_iterator_basic(self):
4761
"""make sure default batch_size works"""
4862
count = 0

0 commit comments

Comments
 (0)