Skip to content

Commit 1dfd317

Browse files
committed
Factor out opening of XDF files and use _read_varlen_int
1 parent 4b2e248 commit 1dfd317

1 file changed

Lines changed: 19 additions & 24 deletions

File tree

pyxdf/pyxdf.py

Lines changed: 19 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -206,18 +206,7 @@ def load_xdf(filename,
206206
# number of bytes in the file for fault tolerance
207207
filesize = os.path.getsize(filename)
208208

209-
# read file contents ([SomeText] below refers to items in the XDF Spec)
210-
filename = Path(filename) # convert to pathlib object
211-
if filename.suffix == '.xdfz' or filename.suffixes == ['.xdf', '.gz']:
212-
f_open = gzip.open
213-
else:
214-
f_open = open
215-
216-
with f_open(filename, 'rb') as f:
217-
# read [MagicCode]
218-
if f.read(4) != b'XDF:':
219-
raise Exception('not a valid XDF file: %s' % filename)
220-
209+
with open_xdf(filename) as f:
221210
# for each chunk
222211
while True:
223212
# noinspection PyBroadException
@@ -352,6 +341,18 @@ def load_xdf(filename,
352341
return streams, fileheader
353342

354343

344+
def open_xdf(filename):
345+
"""Open XDF file for reading."""
346+
filename = Path(filename) # convert to pathlib object
347+
if filename.suffix == '.xdfz' or filename.suffixes == ['.xdf', '.gz']:
348+
f = gzip.open(filename, 'rb')
349+
else:
350+
f = open(filename, 'rb')
351+
if f.read(4) != b'XDF:': # magic bytes
352+
raise IOError('Invalid XDF file {}'.format(filename))
353+
return f
354+
355+
355356
def _read_chunk3(f, s):
356357
# read [NumSampleBytes], [NumSamples]
357358
nsamples = _read_varlen_int(f)
@@ -403,6 +404,8 @@ def _read_varlen_int(f):
403404
return struct.unpack('<I', f.read(4))[0]
404405
elif nbytes == b'\x08':
405406
return struct.unpack('<Q', f.read(8))[0]
407+
elif not nbytes: # EOF
408+
raise EOFError
406409
else:
407410
raise RuntimeError('invalid variable-length integer encountered.')
408411

@@ -661,9 +664,7 @@ def parse_xdf(fname):
661664
List of all chunks contained in the XDF file.
662665
"""
663666
chunks = []
664-
with open(fname, "rb") as f:
665-
if f.read(4) != b"XDF:": # magic code
666-
raise ValueError(f"Invalid XDF file {fname}.")
667+
with open_xdf(fname) as f:
667668
for chunk in _read_chunks(f):
668669
chunks.append(chunk)
669670
return chunks
@@ -705,15 +706,9 @@ def _read_chunks(f):
705706
while True:
706707
chunk = dict()
707708
try:
708-
nbytes = struct.unpack("B", f.read(1))[0]
709-
except struct.error:
710-
return # reached EOF
711-
if nbytes == 1:
712-
chunk["nbytes"] = struct.unpack("B", f.read(1))[0]
713-
elif nbytes == 4:
714-
chunk["nbytes"] = struct.unpack("<I", f.read(4))[0]
715-
elif nbytes == 8:
716-
chunk["nbytes"] = struct.unpack("<Q", f.read(8))[0]
709+
chunk["nbytes"] = _read_varlen_int(f)
710+
except EOFError:
711+
return
717712
chunk["tag"] = struct.unpack('<H', f.read(2))[0]
718713
if chunk["tag"] in [2, 3, 4, 6]:
719714
chunk["stream_id"] = struct.unpack("<I", f.read(4))[0]

0 commit comments

Comments
 (0)