Skip to content

Commit fe092d5

Browse files
committed
NF: function for upper/lower case file extensions
Restore ignoring case in file extensions by using an explicit function.
1 parent ca9430d commit fe092d5

File tree

3 files changed

+63
-1
lines changed

3 files changed

+63
-1
lines changed

bin/parrec2nii

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import nibabel.parrec as pr
1313
from nibabel.mriutils import calculate_dwell_time, MRIError
1414
import nibabel.nifti1 as nifti1
1515
from nibabel.filename_parser import splitext_addext
16-
from nibabel.volumeutils import array_to_file
16+
from nibabel.volumeutils import array_to_file, fname_ext_ul_case
1717

1818

1919
def _oneline(big_str):
@@ -140,6 +140,7 @@ def proc_file(infile, opts):
140140

141141
# load the PAR header and data
142142
scaling = None if opts.scaling == 'off' else opts.scaling
143+
infile = fname_ext_ul_case(infile)
143144
pr_img = pr.load(infile, opts.permit_truncated, scaling)
144145
pr_hdr = pr_img.header
145146
raw_data = pr_img.dataobj.get_unscaled()

nibabel/tests/test_utils.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
''' Test for volumeutils module '''
1010
from __future__ import division
1111

12+
from os.path import exists
13+
1214
from ..externals.six import BytesIO
1315
import tempfile
1416
import warnings
@@ -23,6 +25,7 @@
2325
array_to_file,
2426
allopen, # for backwards compatibility
2527
BinOpener,
28+
fname_ext_ul_case,
2629
calculate_scale,
2730
can_cast,
2831
write_zeros,
@@ -825,6 +828,31 @@ def test_BinOpener():
825828
assert_true(hasattr(fobj.fobj, 'compress'))
826829

827830

831+
def test_fname_ext_ul_case():
832+
# Get filename ignoring the case of the filename extension
833+
with InTemporaryDirectory():
834+
with open('afile.TXT', 'wt') as fobj:
835+
fobj.write('Interesting information')
836+
# OSX usually has case-insensitive file systems; Windows also
837+
os_cares_case = not exists('afile.txt')
838+
with open('bfile.txt', 'wt') as fobj:
839+
fobj.write('More interesting information')
840+
# If there is no file, the case doesn't change
841+
assert_equal(fname_ext_ul_case('nofile.txt'), 'nofile.txt')
842+
assert_equal(fname_ext_ul_case('nofile.TXT'), 'nofile.TXT')
843+
# If there is a file, accept upper or lower case for ext
844+
if os_cares_case:
845+
assert_equal(fname_ext_ul_case('afile.txt'), 'afile.TXT')
846+
assert_equal(fname_ext_ul_case('bfile.TXT'), 'bfile.txt')
847+
else:
848+
assert_equal(fname_ext_ul_case('afile.txt'), 'afile.txt')
849+
assert_equal(fname_ext_ul_case('bfile.TXT'), 'bfile.TXT')
850+
assert_equal(fname_ext_ul_case('afile.TXT'), 'afile.TXT')
851+
assert_equal(fname_ext_ul_case('bfile.txt'), 'bfile.txt')
852+
# Not mixed case though
853+
assert_equal(fname_ext_ul_case('afile.TxT'), 'afile.TxT')
854+
855+
828856
def test_allopen():
829857
# This import into volumeutils is for compatibility. The code is the
830858
# ``openers`` module.

nibabel/volumeutils.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
import numpy as np
1717

18+
from os.path import exists, splitext
1819
from .casting import (shared_range, type_info, OK_FLOATS)
1920
from .openers import Opener
2021

@@ -1514,6 +1515,38 @@ class BinOpener(Opener):
15141515
compress_ext_map['.mgz'] = Opener.gz_def
15151516

15161517

1518+
def fname_ext_ul_case(fname):
1519+
""" `fname` with ext changed to upper / lower case if file exists
1520+
1521+
Check for existence of `fname`. If it does exist, return unmodified. If
1522+
it doesn't, check for existence of `fname` with case changed from lower to
1523+
upper, or upper to lower. Return this modified `fname` if it exists.
1524+
Otherwise return `fname` unmodified
1525+
1526+
Parameters
1527+
----------
1528+
fname : str
1529+
filename.
1530+
1531+
Returns
1532+
-------
1533+
mod_fname : str
1534+
filename, maybe with extension of opposite case
1535+
"""
1536+
if exists(fname):
1537+
return fname
1538+
froot, ext = splitext(fname)
1539+
if ext == ext.lower():
1540+
mod_fname = froot + ext.upper()
1541+
if exists(mod_fname):
1542+
return mod_fname
1543+
elif ext == ext.upper():
1544+
mod_fname = froot + ext.lower()
1545+
if exists(mod_fname):
1546+
return mod_fname
1547+
return fname
1548+
1549+
15171550
def allopen(fileish, *args, **kwargs):
15181551
""" Compatibility wrapper for old ``allopen`` function
15191552

0 commit comments

Comments
 (0)