Skip to content

Commit 9072480

Browse files
committed
ENH: Support multiline header fields in TCK
Closes gh-957
1 parent e662e55 commit 9072480

File tree

1 file changed

+19
-12
lines changed

1 file changed

+19
-12
lines changed

nibabel/streamlines/tck.py

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -331,23 +331,30 @@ def _read_header(cls, fileobj):
331331
f.seek(1, os.SEEK_CUR) # Skip \n
332332

333333
found_end = False
334+
key = None
335+
lines = []
334336

335337
# Read all key-value pairs contained in the header, stop at EOF
336338
for n_line, line in enumerate(f, 1):
337-
line = line.decode('utf-8').strip()
338-
339-
if not line: # Skip empty lines
340-
continue
341-
342-
if line == 'END': # End of the header
339+
if line.strip() == b'END':
343340
found_end = True
344-
break
345341

346-
if ':' not in line: # Invalid header line
347-
raise HeaderError(f'Invalid header (line {n_line}): {line}')
348-
349-
key, value = line.split(':', 1)
350-
hdr[key.strip()] = value.strip()
342+
# Multiline values mean we don't know we're done until we find the next value
343+
if found_end or b':' in line:
344+
if key is not None:
345+
hdr[key] = b''.join(lines).decode().strip()
346+
if found_end:
347+
break
348+
349+
parts = line.split(b':', 1)
350+
key = parts[0].decode().strip()
351+
lines = [parts[1]]
352+
elif key is None:
353+
# Apparent continuation line before any keys are found
354+
if line.strip():
355+
raise HeaderError(f'Invalid header (line in {n_line}): {line}')
356+
else:
357+
lines.append(line)
351358

352359
if not found_end:
353360
raise HeaderError('Missing END in the header.')

0 commit comments

Comments
 (0)