Skip to content

Commit b21d5e7

Browse files
committed
ENH: use logging in dicomfs + optparse to provide control over logging etc
1 parent 96e0b52 commit b21d5e7

File tree

1 file changed

+62
-37
lines changed

1 file changed

+62
-37
lines changed

bin/dicomfs

Lines changed: 62 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,21 @@ import stat
1515
import errno
1616
import time
1717
import locale
18+
import logging
1819
import fuse
20+
import nibabel as nib
1921
import nibabel.dft as dft
2022

23+
from optparse import OptionParser, Option
24+
2125
uid = os.getuid()
2226
gid = os.getgid()
2327
encoding = locale.getdefaultlocale()[1]
2428

2529
fuse.fuse_python_api = (0, 2)
2630

31+
logger = logging.getLogger('nibabel.dft')
32+
2733
class FileHandle:
2834

2935
def __init__(self, fno):
@@ -78,33 +84,31 @@ class DICOMFS(fuse.Fuse):
7884
def match_path(self, path):
7985
wd = self.get_paths()
8086
if path == '/':
81-
print 'return root'
87+
logger.debug('return root')
8288
return wd
8389
for part in path.lstrip('/').split('/'):
84-
print path, part
90+
logger.debug("path:%s part:%s" % (path, part))
8591
if part not in wd:
8692
return None
8793
wd = wd[part]
88-
print 'return'
94+
logger.debug('return')
8995
return wd
9096

9197
def readdir(self, path, fh):
92-
print 'readdir'
93-
print path
98+
logger.info('readdir %s' % (path,))
9499
matched_path = self.match_path(path)
95100
if matched_path is None:
96101
return -errno.ENOENT
97-
print 'match', matched_path
102+
logger.debug('matched %s' % (matched_path,))
98103
fnames = [ k.encode('ascii', 'replace') for k in matched_path.keys() ]
99104
fnames.append('.')
100105
fnames.append('..')
101106
return [ fuse.Direntry(f) for f in fnames ]
102-
107+
103108
def getattr(self, path):
104-
print 'getattr'
105-
print 'path:', path
109+
logger.debug('getattr %s' % path)
106110
matched_path = self.match_path(path)
107-
print matched_path
111+
logger.debug('matched: %s' % (matched_path,))
108112
now = time.time()
109113
st = fuse.Stat()
110114
if isinstance(matched_path, dict):
@@ -137,10 +141,9 @@ class DICOMFS(fuse.Fuse):
137141
st.st_nlink = 1
138142
return st
139143
return -errno.ENOENT
140-
144+
141145
def open(self, path, flags):
142-
print 'open'
143-
print path
146+
logger.debug('open %s' % (path,))
144147
matched_path = self.match_path(path)
145148
if matched_path is None:
146149
return -errno.ENOENT
@@ -157,34 +160,56 @@ class DICOMFS(fuse.Fuse):
157160

158161
# not done
159162
def read(self, path, size, offset, fh):
160-
print 'read'
161-
print path
162-
print size
163-
print offset
164-
print fh
163+
logger.debug('read')
164+
logger.debug(path)
165+
logger.debug(size)
166+
logger.debug(offset)
167+
logger.debug(fh)
165168
return self.fhs[fh.fno][offset:offset+size]
166169

167170
def release(self, path, flags, fh):
168-
print 'release'
169-
print path
170-
print fh
171+
logger.debug('release')
172+
logger.debug(path)
173+
logger.debug(fh)
171174
del self.fhs[fh.fno]
172175
return
173-
174-
progname = os.path.basename(sys.argv[0])
175-
if len(sys.argv) != 3:
176-
sys.stderr.write('usage: %s <directory containing DICOMs> <mount point>\n' % progname)
177-
sys.exit(1)
178-
179-
fs = DICOMFS(dash_s_do='setsingle')
180-
fs.parse(['-f', '-s', sys.argv[2]])
181-
fs.dicom_path = sys.argv[1].decode(encoding)
182-
try:
183-
fs.main()
184-
except fuse.FuseError:
185-
# fuse prints the error message
186-
sys.exit(1)
187-
188-
sys.exit(0)
176+
177+
def get_opt_parser():
178+
# use module docstring for help output
179+
p = OptionParser(
180+
usage="%s [OPTIONS] <DIRECTORY CONTAINING DICOMSs> <mount point>"
181+
% os.path.basename(sys.argv[0]),
182+
version="%prog " + nib.__version__)
183+
184+
p.add_options([
185+
Option("-v", "--verbose", action="count",
186+
dest="verbose", default=0,
187+
help="make noise. Could be specified multiple times"),
188+
])
189+
190+
return p
191+
192+
if __name__ == '__main__':
193+
parser = get_opt_parser()
194+
(opts, files) = parser.parse_args()
195+
196+
if opts.verbose:
197+
logger.addHandler(logging.StreamHandler(sys.stdout))
198+
logger.setLevel(opts.verbose > 1 and logging.DEBUG or logging.INFO)
199+
200+
if len(files) != 2:
201+
sys.stderr.write("Please provide two arguments:\n%s\n" % parser.usage)
202+
sys.exit(1)
203+
204+
fs = DICOMFS(dash_s_do='setsingle')
205+
fs.parse(['-f', '-s', files[1]])
206+
fs.dicom_path = files[0].decode(encoding)
207+
try:
208+
fs.main()
209+
except fuse.FuseError:
210+
# fuse prints the error message
211+
sys.exit(1)
212+
213+
sys.exit(0)
189214

190215
# eof

0 commit comments

Comments
 (0)