Skip to content

Commit aef64fc

Browse files
committed
STY: Uniformization: tab2spaces, common file headers
1 parent 55596bf commit aef64fc

File tree

3 files changed

+735
-715
lines changed

3 files changed

+735
-715
lines changed

bin/dicomfs

Lines changed: 151 additions & 145 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
#!/usr/bin/python
2-
2+
# emacs: -*- mode: python-mode; py-indent-offset: 4; indent-tabs-mode: nil -*-
3+
# vi: set ft=python sts=4 ts=4 sw=4 et:
4+
### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ##
5+
#
6+
# See COPYING file distributed along with the NiBabel package for the
7+
# copyright and license terms.
8+
#
9+
### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ##
310
# Copyright (C) 2011 Christian Haselgrove
4-
# see LICENSE file for copyright and full license
511

612
import sys
713
import os
@@ -20,164 +26,164 @@ fuse.fuse_python_api = (0, 2)
2026

2127
class FileHandle:
2228

23-
def __init__(self, fno):
24-
self.fno = fno
25-
self.keep_cache = False
26-
self.direct_io = False
27-
return
29+
def __init__(self, fno):
30+
self.fno = fno
31+
self.keep_cache = False
32+
self.direct_io = False
33+
return
2834

29-
def __str__(self):
30-
return 'FileHandle(%d)' % self.fno
35+
def __str__(self):
36+
return 'FileHandle(%d)' % self.fno
3137

3238
class DICOMFS(fuse.Fuse):
3339

34-
def __init__(self, *args, **kwargs):
35-
fuse.Fuse.__init__(self, *args, **kwargs)
36-
self.fhs = {}
37-
return
38-
39-
def get_paths(self):
40-
paths = {}
41-
for study in dft.get_studies(self.dicom_path):
42-
pd = paths.setdefault(study.patient_name_or_uid(), {})
43-
patient_info = 'patient information\n'
44-
patient_info = 'name: %s\n' % study.patient_name
45-
patient_info += 'ID: %s\n' % study.patient_id
46-
patient_info += 'birth date: %s\n' % study.patient_birth_date
47-
patient_info += 'sex: %s\n' % study.patient_sex
48-
pd['INFO'] = patient_info.encode('ascii', 'replace')
49-
study_datetime = '%s_%s' % (study.date, study.time)
50-
study_info = 'study info\n'
51-
study_info += 'UID: %s\n' % study.uid
52-
study_info += 'date: %s\n' % study.date
53-
study_info += 'time: %s\n' % study.time
54-
study_info += 'comments: %s\n' % study.comments
55-
d = {'INFO': study_info.encode('ascii', 'replace')}
56-
for series in study.series:
57-
series_info = 'series info\n'
58-
series_info += 'UID: %s\n' % series.uid
59-
series_info += 'number: %s\n' % series.number
60-
series_info += 'description: %s\n' % series.description
61-
series_info += 'rows: %d\n' % series.rows
62-
series_info += 'columns: %d\n' % series.columns
63-
series_info += 'bits allocated: %d\n' % series.bits_allocated
64-
series_info += 'bits stored: %d\n' % series.bits_stored
65-
series_info += 'storage instances: %d\n' % len(series.storage_instances)
66-
d[series.number] = {'INFO': series_info.encode('ascii', 'replace'),
67-
'%s.nii' % series.number: (series.nifti_size, series.as_nifti),
68-
'%s.png' % series.number: (series.png_size, series.as_png)}
69-
pd[study_datetime] = d
70-
return paths
71-
72-
def match_path(self, path):
73-
wd = self.get_paths()
74-
if path == '/':
75-
print 'return root'
76-
return wd
77-
for part in path.lstrip('/').split('/'):
78-
print path, part
79-
if part not in wd:
80-
return None
81-
wd = wd[part]
82-
print 'return'
83-
return wd
84-
85-
def readdir(self, path, fh):
86-
print 'readdir'
87-
print path
88-
matched_path = self.match_path(path)
89-
if matched_path is None:
90-
return -errno.ENOENT
91-
print 'match', matched_path
92-
fnames = [ k.encode('ascii', 'replace') for k in matched_path.keys() ]
93-
fnames.append('.')
94-
fnames.append('..')
95-
return [ fuse.Direntry(f) for f in fnames ]
40+
def __init__(self, *args, **kwargs):
41+
fuse.Fuse.__init__(self, *args, **kwargs)
42+
self.fhs = {}
43+
return
44+
45+
def get_paths(self):
46+
paths = {}
47+
for study in dft.get_studies(self.dicom_path):
48+
pd = paths.setdefault(study.patient_name_or_uid(), {})
49+
patient_info = 'patient information\n'
50+
patient_info = 'name: %s\n' % study.patient_name
51+
patient_info += 'ID: %s\n' % study.patient_id
52+
patient_info += 'birth date: %s\n' % study.patient_birth_date
53+
patient_info += 'sex: %s\n' % study.patient_sex
54+
pd['INFO'] = patient_info.encode('ascii', 'replace')
55+
study_datetime = '%s_%s' % (study.date, study.time)
56+
study_info = 'study info\n'
57+
study_info += 'UID: %s\n' % study.uid
58+
study_info += 'date: %s\n' % study.date
59+
study_info += 'time: %s\n' % study.time
60+
study_info += 'comments: %s\n' % study.comments
61+
d = {'INFO': study_info.encode('ascii', 'replace')}
62+
for series in study.series:
63+
series_info = 'series info\n'
64+
series_info += 'UID: %s\n' % series.uid
65+
series_info += 'number: %s\n' % series.number
66+
series_info += 'description: %s\n' % series.description
67+
series_info += 'rows: %d\n' % series.rows
68+
series_info += 'columns: %d\n' % series.columns
69+
series_info += 'bits allocated: %d\n' % series.bits_allocated
70+
series_info += 'bits stored: %d\n' % series.bits_stored
71+
series_info += 'storage instances: %d\n' % len(series.storage_instances)
72+
d[series.number] = {'INFO': series_info.encode('ascii', 'replace'),
73+
'%s.nii' % series.number: (series.nifti_size, series.as_nifti),
74+
'%s.png' % series.number: (series.png_size, series.as_png)}
75+
pd[study_datetime] = d
76+
return paths
77+
78+
def match_path(self, path):
79+
wd = self.get_paths()
80+
if path == '/':
81+
print 'return root'
82+
return wd
83+
for part in path.lstrip('/').split('/'):
84+
print path, part
85+
if part not in wd:
86+
return None
87+
wd = wd[part]
88+
print 'return'
89+
return wd
90+
91+
def readdir(self, path, fh):
92+
print 'readdir'
93+
print path
94+
matched_path = self.match_path(path)
95+
if matched_path is None:
96+
return -errno.ENOENT
97+
print 'match', matched_path
98+
fnames = [ k.encode('ascii', 'replace') for k in matched_path.keys() ]
99+
fnames.append('.')
100+
fnames.append('..')
101+
return [ fuse.Direntry(f) for f in fnames ]
96102

97-
def getattr(self, path):
98-
print 'getattr'
99-
print 'path:', path
100-
matched_path = self.match_path(path)
101-
print matched_path
102-
now = time.time()
103-
st = fuse.Stat()
104-
if isinstance(matched_path, dict):
105-
st.st_mode = stat.S_IFDIR | 0755
106-
st.st_ctime = now
107-
st.st_mtime = now
108-
st.st_atime = now
109-
st.st_uid = uid
110-
st.st_gid = gid
111-
st.st_nlink = len(matched_path)
112-
return st
113-
if isinstance(matched_path, str):
114-
st.st_mode = stat.S_IFREG | 0644
115-
st.st_ctime = now
116-
st.st_mtime = now
117-
st.st_atime = now
118-
st.st_uid = uid
119-
st.st_gid = gid
120-
st.st_size = len(matched_path)
121-
st.st_nlink = 1
122-
return st
123-
if isinstance(matched_path, tuple):
124-
st.st_mode = stat.S_IFREG | 0644
125-
st.st_ctime = now
126-
st.st_mtime = now
127-
st.st_atime = now
128-
st.st_uid = uid
129-
st.st_gid = gid
130-
st.st_size = matched_path[0]()
131-
st.st_nlink = 1
132-
return st
133-
return -errno.ENOENT
103+
def getattr(self, path):
104+
print 'getattr'
105+
print 'path:', path
106+
matched_path = self.match_path(path)
107+
print matched_path
108+
now = time.time()
109+
st = fuse.Stat()
110+
if isinstance(matched_path, dict):
111+
st.st_mode = stat.S_IFDIR | 0755
112+
st.st_ctime = now
113+
st.st_mtime = now
114+
st.st_atime = now
115+
st.st_uid = uid
116+
st.st_gid = gid
117+
st.st_nlink = len(matched_path)
118+
return st
119+
if isinstance(matched_path, str):
120+
st.st_mode = stat.S_IFREG | 0644
121+
st.st_ctime = now
122+
st.st_mtime = now
123+
st.st_atime = now
124+
st.st_uid = uid
125+
st.st_gid = gid
126+
st.st_size = len(matched_path)
127+
st.st_nlink = 1
128+
return st
129+
if isinstance(matched_path, tuple):
130+
st.st_mode = stat.S_IFREG | 0644
131+
st.st_ctime = now
132+
st.st_mtime = now
133+
st.st_atime = now
134+
st.st_uid = uid
135+
st.st_gid = gid
136+
st.st_size = matched_path[0]()
137+
st.st_nlink = 1
138+
return st
139+
return -errno.ENOENT
134140

135-
def open(self, path, flags):
136-
print 'open'
137-
print path
138-
matched_path = self.match_path(path)
139-
if matched_path is None:
140-
return -errno.ENOENT
141-
for i in xrange(1, 10):
142-
if i not in self.fhs:
143-
if isinstance(matched_path, str):
144-
self.fhs[i] = matched_path
145-
elif isinstance(matched_path, tuple):
146-
self.fhs[i] = matched_path[1]()
147-
else:
148-
raise -errno.EFTYPE
149-
return FileHandle(i)
150-
raise -errno.ENFILE
151-
152-
# not done
153-
def read(self, path, size, offset, fh):
154-
print 'read'
155-
print path
156-
print size
157-
print offset
158-
print fh
159-
return self.fhs[fh.fno][offset:offset+size]
160-
161-
def release(self, path, flags, fh):
162-
print 'release'
163-
print path
164-
print fh
165-
del self.fhs[fh.fno]
166-
return
141+
def open(self, path, flags):
142+
print 'open'
143+
print path
144+
matched_path = self.match_path(path)
145+
if matched_path is None:
146+
return -errno.ENOENT
147+
for i in xrange(1, 10):
148+
if i not in self.fhs:
149+
if isinstance(matched_path, str):
150+
self.fhs[i] = matched_path
151+
elif isinstance(matched_path, tuple):
152+
self.fhs[i] = matched_path[1]()
153+
else:
154+
raise -errno.EFTYPE
155+
return FileHandle(i)
156+
raise -errno.ENFILE
157+
158+
# not done
159+
def read(self, path, size, offset, fh):
160+
print 'read'
161+
print path
162+
print size
163+
print offset
164+
print fh
165+
return self.fhs[fh.fno][offset:offset+size]
166+
167+
def release(self, path, flags, fh):
168+
print 'release'
169+
print path
170+
print fh
171+
del self.fhs[fh.fno]
172+
return
167173

168174
progname = os.path.basename(sys.argv[0])
169175
if len(sys.argv) != 3:
170-
sys.stderr.write('usage: %s <directory containing DICOMs> <mount point>\n' % progname)
171-
sys.exit(1)
176+
sys.stderr.write('usage: %s <directory containing DICOMs> <mount point>\n' % progname)
177+
sys.exit(1)
172178

173179
fs = DICOMFS(dash_s_do='setsingle')
174180
fs.parse(['-f', '-s', sys.argv[2]])
175181
fs.dicom_path = sys.argv[1].decode(encoding)
176182
try:
177-
fs.main()
183+
fs.main()
178184
except fuse.FuseError:
179-
# fuse prints the error message
180-
sys.exit(1)
185+
# fuse prints the error message
186+
sys.exit(1)
181187

182188
sys.exit(0)
183189

0 commit comments

Comments
 (0)