Skip to content

Commit f6b48c5

Browse files
committed
code deduplication
1 parent 89dc117 commit f6b48c5

File tree

5 files changed

+17
-68
lines changed

5 files changed

+17
-68
lines changed

sigmf/convert/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ def get_magic_bytes(file_path: Path, count: int = 4, offset: int = 0) -> bytes:
4141
if len(magic_bytes) < count:
4242
raise SigMFConversionError(f"File {file_path} too small to read {count} magic bytes at offset {offset}")
4343
return magic_bytes
44-
except OSError as err:
45-
raise SigMFConversionError(f"Failed to read magic bytes from {file_path}: {err}") from err
44+
except (IOError, OSError) as err:
45+
raise SigMFConversionError(f"Cannot read magic bytes from {file_path}: {err}") from err
4646

4747

4848
def detect_converter(file_path: Path):

sigmf/convert/signalhound.py

Lines changed: 13 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,16 @@ def convert_iq_data(xml_path: Path, sample_count: int) -> np.ndarray:
323323
return samples
324324

325325

326+
def _add_annotations(meta: SigMFFile, annotations: list) -> None:
327+
for annotation in annotations:
328+
start_idx = annotation.get(SigMFFile.START_INDEX_KEY, 0)
329+
length = annotation.get(SigMFFile.LENGTH_INDEX_KEY)
330+
annot_metadata = {
331+
k: v for k, v in annotation.items() if k not in [SigMFFile.START_INDEX_KEY, SigMFFile.LENGTH_INDEX_KEY]
332+
}
333+
meta.add_annotation(start_idx, length=length, metadata=annot_metadata)
334+
335+
326336
def signalhound_to_sigmf(
327337
signalhound_path: Path,
328338
out_path: Optional[Path] = None,
@@ -390,16 +400,7 @@ def signalhound_to_sigmf(
390400
meta.set_data_file(data_file=data_file_path, offset=0)
391401
meta.data_buffer = io.BytesIO()
392402
meta.add_capture(0, metadata=capture_info)
393-
394-
# add annotations from metadata
395-
for annotation in annotations:
396-
start_idx = annotation.get(SigMFFile.START_INDEX_KEY, 0)
397-
length = annotation.get(SigMFFile.LENGTH_INDEX_KEY)
398-
# pass remaining fields as metadata (excluding standard annotation keys)
399-
annot_metadata = {
400-
k: v for k, v in annotation.items() if k not in [SigMFFile.START_INDEX_KEY, SigMFFile.LENGTH_INDEX_KEY]
401-
}
402-
meta.add_annotation(start_idx, length=length, metadata=annot_metadata)
403+
_add_annotations(meta, annotations)
403404

404405
# write metadata file if output path specified
405406
if out_path is not None:
@@ -429,17 +430,7 @@ def signalhound_to_sigmf(
429430

430431
meta = SigMFFile(data_file=data_path, global_info=global_info)
431432
meta.add_capture(0, metadata=capture_info)
432-
433-
# add annotations from metadata
434-
for annotation in annotations:
435-
start_idx = annotation.get(SigMFFile.START_INDEX_KEY, 0)
436-
length = annotation.get(SigMFFile.LENGTH_INDEX_KEY)
437-
annot_metadata = {
438-
k: v
439-
for k, v in annotation.items()
440-
if k not in [SigMFFile.START_INDEX_KEY, SigMFFile.LENGTH_INDEX_KEY]
441-
}
442-
meta.add_annotation(start_idx, length=length, metadata=annot_metadata)
433+
_add_annotations(meta, annotations)
443434

444435
output_dir = filenames["archive_fn"].parent
445436
output_dir.mkdir(parents=True, exist_ok=True)
@@ -465,16 +456,7 @@ def signalhound_to_sigmf(
465456
# create sigmffile with converted iq data
466457
meta = SigMFFile(data_file=filenames["data_fn"], global_info=global_info)
467458
meta.add_capture(0, metadata=capture_info)
468-
469-
# add annotations from metadata
470-
for annotation in annotations:
471-
start_idx = annotation.get(SigMFFile.START_INDEX_KEY, 0)
472-
length = annotation.get(SigMFFile.LENGTH_INDEX_KEY)
473-
# pass remaining fields as metadata (excluding standard annotation keys)
474-
annot_metadata = {
475-
k: v for k, v in annotation.items() if k not in [SigMFFile.START_INDEX_KEY, SigMFFile.LENGTH_INDEX_KEY]
476-
}
477-
meta.add_annotation(start_idx, length=length, metadata=annot_metadata)
459+
_add_annotations(meta, annotations)
478460

479461
# write metadata file
480462
meta.tofile(filenames["meta_fn"], toarchive=False, overwrite=overwrite)

sigmf/utils.py

Lines changed: 0 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -113,37 +113,3 @@ def get_data_type_str(ray: np.ndarray) -> str:
113113
# only append endianness for types over 8 bits
114114
data_type_str += get_endian_str(ray)
115115
return data_type_str
116-
117-
118-
def get_magic_bytes(file_path: Path, count: int = 4, offset: int = 0) -> bytes:
119-
"""
120-
Get magic bytes from a file to help identify file type.
121-
122-
Parameters
123-
----------
124-
file_path : Path
125-
Path to the file to read magic bytes from.
126-
count : int, optional
127-
Number of bytes to read. Default is 4.
128-
offset : int, optional
129-
Byte offset to start reading from. Default is 0.
130-
131-
Returns
132-
-------
133-
bytes
134-
Magic bytes from the file.
135-
136-
Raises
137-
------
138-
SigMFConversionError
139-
If file cannot be read or is too small.
140-
"""
141-
try:
142-
with open(file_path, "rb") as handle:
143-
handle.seek(offset)
144-
magic_bytes = handle.read(count)
145-
if len(magic_bytes) < count:
146-
raise SigMFConversionError(f"File {file_path} too small to read {count} magic bytes at offset {offset}")
147-
return magic_bytes
148-
except (IOError, OSError) as err:
149-
raise SigMFConversionError(f"Cannot read magic bytes from {file_path}: {err}")

sigmf/validate.py

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

77
"""SigMF Validator"""
8+
89
import argparse
910
import glob
1011
import json

tests/test_hashing.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ def test_ncd_hash_covers_entire_file(self):
3939
# Create NCD file with header, data, and trailer
4040
handle.write(b"\x00" * 64) # header
4141
handle.write(TEST_FLOAT32_DATA.tobytes()) # sample data
42-
handle.write(b"\xFF" * 32) # trailer
42+
handle.write(b"\xff" * 32) # trailer
4343

4444
# Create SigMF metadata for NCD
4545
ncd_metadata = deepcopy(TEST_METADATA)

0 commit comments

Comments
 (0)