Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

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.
  • Loading branch information...
commit 0a7e230942fcd73deb85b9f8c47438628bf08b5b 2 parents 73ce5d6 + 617e8f1
@matthew-brett matthew-brett authored
Showing with 37 additions and 17 deletions.
  1. +8 −2 bin/nib-dicomfs
  2. +9 −7 nibabel/dft.py
  3. +20 −8 tools/dicomfs.wsgi
View
10 bin/nib-dicomfs
@@ -44,13 +44,14 @@ class FileHandle:
class DICOMFS(fuse.Fuse):
def __init__(self, *args, **kwargs):
+ self.followlinks = kwargs.pop('followlinks', False)
fuse.Fuse.__init__(self, *args, **kwargs)
self.fhs = {}
return
def get_paths(self):
paths = {}
- for study in dft.get_studies(self.dicom_path):
+ for study in dft.get_studies(self.dicom_path, self.followlinks):
pd = paths.setdefault(study.patient_name_or_uid(), {})
patient_info = 'patient information\n'
patient_info = 'name: %s\n' % study.patient_name
@@ -187,6 +188,11 @@ def get_opt_parser():
help="make noise. Could be specified multiple times"),
])
+ p.add_options([
+ Option("-L", "--follow-links", action="store_true",
+ dest="followlinks", default=False,
+ help="Follow symbolic links in DICOM directory"),
+ ])
return p
if __name__ == '__main__':
@@ -201,7 +207,7 @@ if __name__ == '__main__':
sys.stderr.write("Please provide two arguments:\n%s\n" % parser.usage)
sys.exit(1)
- fs = DICOMFS(dash_s_do='setsingle')
+ fs = DICOMFS(dash_s_do='setsingle', followlinks=opts.followlinks)
fs.parse(['-f', '-s', files[1]])
fs.dicom_path = files[0].decode(encoding)
try:
View
16 nibabel/dft.py
@@ -259,9 +259,11 @@ def __exit__(self, type, value, traceback):
DB.rollback()
return
-def _get_subdirs(base_dir, files_dict=None):
+def _get_subdirs(base_dir, files_dict=None, followlinks=False):
dirs = []
- for (dirpath, dirnames, filenames) in os.walk(base_dir, followlinks=True):
+ # followlinks keyword not available for python 2.5.
+ kwargs = {} if not followlinks else {'followlinks': True}
+ for (dirpath, dirnames, filenames) in os.walk(base_dir, **kwargs):
abs_dir = os.path.realpath(dirpath)
if abs_dir in dirs:
raise CachingError, 'link cycle detected under %s' % base_dir
@@ -270,10 +272,10 @@ def _get_subdirs(base_dir, files_dict=None):
files_dict[abs_dir] = filenames
return dirs
-def update_cache(base_dir):
+def update_cache(base_dir, followlinks=False):
mtimes = {}
files_by_dir = {}
- dirs = _get_subdirs(base_dir, files_by_dir)
+ dirs = _get_subdirs(base_dir, files_by_dir, followlinks)
for d in dirs:
os.stat(d)
mtimes[d] = os.stat(d).st_mtime
@@ -300,9 +302,9 @@ def update_cache(base_dir):
c.execute(query, (dir, mtimes[dir]))
return
-def get_studies(base_dir=None):
+def get_studies(base_dir=None, followlinks=False):
if base_dir is not None:
- update_cache(base_dir)
+ update_cache(base_dir, followlinks)
if base_dir is None:
with _db_nochange() as c:
c.execute("SELECT * FROM study")
@@ -321,7 +323,7 @@ def get_studies(base_dir=None):
WHERE directory = ?))"""
with _db_nochange() as c:
study_uids = {}
- for dir in _get_subdirs(base_dir):
+ for dir in _get_subdirs(base_dir, followlinks=followlinks):
c.execute(query, (dir, ))
for row in c:
study_uids[row[0]] = None
View
28 tools/dicomfs.wsgi
@@ -10,17 +10,29 @@
import sys
import traceback
+from functools import partial
import urllib
import cgi
+
import jinja2
+
from nibabel import dft
+# this is the directory containing the DICOM data, or None for all cached data
+BASE_DIR = '/path/to/DICOM'
+BASE_DIR = None
+
+# default setting for whether to follow symlinks in BASE_DIR. Python 2.5 only
+# accepts False for this setting, Python >= 2.6 accepts True or False
+FOLLOWLINKS=False
+
+# Define routine to get studies
+studies_getter = partial(dft.get_studies, followlinks=FOLLOWLINKS)
+
+
def html_unicode(u):
return cgi.escape(u.encode('utf-8'))
-# this is the directory containing the DICOM data, or None for all cached data
-base_dir = '/path/to/DICOM'
-base_dir = None
template_env = jinja2.Environment(autoescape=True)
template_env.filters['urlquote'] = urllib.quote
@@ -161,13 +173,13 @@ def study_cmp(a, b):
def index(environ):
patients = {}
- for s in dft.get_studies(base_dir):
+ for s in studies_getter(BASE_DIR):
patients.setdefault(s.patient_name_or_uid(), []).append(s)
template = template_env.from_string(index_template)
return template.render(patients=patients).encode('utf-8')
def patient(patient):
- studies = [ s for s in dft.get_studies() if s.patient_name_or_uid() == patient ]
+ studies = [ s for s in studies_getter() if s.patient_name_or_uid() == patient ]
if len(studies) == 0:
raise HandlerError('404 Not Found', 'patient %s not found\n' % patient)
studies.sort(study_cmp)
@@ -176,7 +188,7 @@ def patient(patient):
def patient_date_time(patient, date_time):
study = None
- for s in dft.get_studies():
+ for s in studies_getter():
if s.patient_name_or_uid() != patient:
continue
if date_time != '%s_%s' % (s.date, s.time):
@@ -190,7 +202,7 @@ def patient_date_time(patient, date_time):
def nifti(patient, date_time, scan):
study = None
- for s in dft.get_studies():
+ for s in studies_getter():
if s.patient_name_or_uid() != patient:
continue
if date_time != '%s_%s' % (s.date, s.time):
@@ -211,7 +223,7 @@ def nifti(patient, date_time, scan):
def png(patient, date_time, scan):
study = None
- for s in dft.get_studies():
+ for s in studies_getter():
if s.patient_name_or_uid() != patient:
continue
if date_time != '%s_%s' % (s.date, s.time):
Please sign in to comment.
Something went wrong with that request. Please try again.