Skip to content

Commit 0a7e230

Browse files
committed
Merge pull request #129 from matthew-brett/optional-dicomfs-followlinks
RF: followlinks optional for dicom file system followlinks keyword argument to os.walk was causing an error in python 2.5. Maybe it should be optional anyway.
2 parents 73ce5d6 + 617e8f1 commit 0a7e230

File tree

3 files changed

+37
-17
lines changed

3 files changed

+37
-17
lines changed

bin/nib-dicomfs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,14 @@ class FileHandle:
4444
class DICOMFS(fuse.Fuse):
4545

4646
def __init__(self, *args, **kwargs):
47+
self.followlinks = kwargs.pop('followlinks', False)
4748
fuse.Fuse.__init__(self, *args, **kwargs)
4849
self.fhs = {}
4950
return
5051

5152
def get_paths(self):
5253
paths = {}
53-
for study in dft.get_studies(self.dicom_path):
54+
for study in dft.get_studies(self.dicom_path, self.followlinks):
5455
pd = paths.setdefault(study.patient_name_or_uid(), {})
5556
patient_info = 'patient information\n'
5657
patient_info = 'name: %s\n' % study.patient_name
@@ -187,6 +188,11 @@ def get_opt_parser():
187188
help="make noise. Could be specified multiple times"),
188189
])
189190

191+
p.add_options([
192+
Option("-L", "--follow-links", action="store_true",
193+
dest="followlinks", default=False,
194+
help="Follow symbolic links in DICOM directory"),
195+
])
190196
return p
191197

192198
if __name__ == '__main__':
@@ -201,7 +207,7 @@ if __name__ == '__main__':
201207
sys.stderr.write("Please provide two arguments:\n%s\n" % parser.usage)
202208
sys.exit(1)
203209

204-
fs = DICOMFS(dash_s_do='setsingle')
210+
fs = DICOMFS(dash_s_do='setsingle', followlinks=opts.followlinks)
205211
fs.parse(['-f', '-s', files[1]])
206212
fs.dicom_path = files[0].decode(encoding)
207213
try:

nibabel/dft.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -259,9 +259,11 @@ def __exit__(self, type, value, traceback):
259259
DB.rollback()
260260
return
261261

262-
def _get_subdirs(base_dir, files_dict=None):
262+
def _get_subdirs(base_dir, files_dict=None, followlinks=False):
263263
dirs = []
264-
for (dirpath, dirnames, filenames) in os.walk(base_dir, followlinks=True):
264+
# followlinks keyword not available for python 2.5.
265+
kwargs = {} if not followlinks else {'followlinks': True}
266+
for (dirpath, dirnames, filenames) in os.walk(base_dir, **kwargs):
265267
abs_dir = os.path.realpath(dirpath)
266268
if abs_dir in dirs:
267269
raise CachingError, 'link cycle detected under %s' % base_dir
@@ -270,10 +272,10 @@ def _get_subdirs(base_dir, files_dict=None):
270272
files_dict[abs_dir] = filenames
271273
return dirs
272274

273-
def update_cache(base_dir):
275+
def update_cache(base_dir, followlinks=False):
274276
mtimes = {}
275277
files_by_dir = {}
276-
dirs = _get_subdirs(base_dir, files_by_dir)
278+
dirs = _get_subdirs(base_dir, files_by_dir, followlinks)
277279
for d in dirs:
278280
os.stat(d)
279281
mtimes[d] = os.stat(d).st_mtime
@@ -300,9 +302,9 @@ def update_cache(base_dir):
300302
c.execute(query, (dir, mtimes[dir]))
301303
return
302304

303-
def get_studies(base_dir=None):
305+
def get_studies(base_dir=None, followlinks=False):
304306
if base_dir is not None:
305-
update_cache(base_dir)
307+
update_cache(base_dir, followlinks)
306308
if base_dir is None:
307309
with _db_nochange() as c:
308310
c.execute("SELECT * FROM study")
@@ -321,7 +323,7 @@ def get_studies(base_dir=None):
321323
WHERE directory = ?))"""
322324
with _db_nochange() as c:
323325
study_uids = {}
324-
for dir in _get_subdirs(base_dir):
326+
for dir in _get_subdirs(base_dir, followlinks=followlinks):
325327
c.execute(query, (dir, ))
326328
for row in c:
327329
study_uids[row[0]] = None

tools/dicomfs.wsgi

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,29 @@
1010

1111
import sys
1212
import traceback
13+
from functools import partial
1314
import urllib
1415
import cgi
16+
1517
import jinja2
18+
1619
from nibabel import dft
1720

21+
# this is the directory containing the DICOM data, or None for all cached data
22+
BASE_DIR = '/path/to/DICOM'
23+
BASE_DIR = None
24+
25+
# default setting for whether to follow symlinks in BASE_DIR. Python 2.5 only
26+
# accepts False for this setting, Python >= 2.6 accepts True or False
27+
FOLLOWLINKS=False
28+
29+
# Define routine to get studies
30+
studies_getter = partial(dft.get_studies, followlinks=FOLLOWLINKS)
31+
32+
1833
def html_unicode(u):
1934
return cgi.escape(u.encode('utf-8'))
2035

21-
# this is the directory containing the DICOM data, or None for all cached data
22-
base_dir = '/path/to/DICOM'
23-
base_dir = None
2436

2537
template_env = jinja2.Environment(autoescape=True)
2638
template_env.filters['urlquote'] = urllib.quote
@@ -161,13 +173,13 @@ def study_cmp(a, b):
161173

162174
def index(environ):
163175
patients = {}
164-
for s in dft.get_studies(base_dir):
176+
for s in studies_getter(BASE_DIR):
165177
patients.setdefault(s.patient_name_or_uid(), []).append(s)
166178
template = template_env.from_string(index_template)
167179
return template.render(patients=patients).encode('utf-8')
168180

169181
def patient(patient):
170-
studies = [ s for s in dft.get_studies() if s.patient_name_or_uid() == patient ]
182+
studies = [ s for s in studies_getter() if s.patient_name_or_uid() == patient ]
171183
if len(studies) == 0:
172184
raise HandlerError('404 Not Found', 'patient %s not found\n' % patient)
173185
studies.sort(study_cmp)
@@ -176,7 +188,7 @@ def patient(patient):
176188

177189
def patient_date_time(patient, date_time):
178190
study = None
179-
for s in dft.get_studies():
191+
for s in studies_getter():
180192
if s.patient_name_or_uid() != patient:
181193
continue
182194
if date_time != '%s_%s' % (s.date, s.time):
@@ -190,7 +202,7 @@ def patient_date_time(patient, date_time):
190202

191203
def nifti(patient, date_time, scan):
192204
study = None
193-
for s in dft.get_studies():
205+
for s in studies_getter():
194206
if s.patient_name_or_uid() != patient:
195207
continue
196208
if date_time != '%s_%s' % (s.date, s.time):
@@ -211,7 +223,7 @@ def nifti(patient, date_time, scan):
211223

212224
def png(patient, date_time, scan):
213225
study = None
214-
for s in dft.get_studies():
226+
for s in studies_getter():
215227
if s.patient_name_or_uid() != patient:
216228
continue
217229
if date_time != '%s_%s' % (s.date, s.time):

0 commit comments

Comments
 (0)