Skip to content
Snippets Groups Projects
Commit 379006de authored by Paul McCarthy's avatar Paul McCarthy
Browse files

added ability to specify file suffixes for FilePath properties

parent 2f8314f6
No related branches found
No related tags found
No related merge requests found
......@@ -32,12 +32,13 @@ runChoices = OrderedDict((
('-A', 'Run bet2 and then betsurf to get additional skull and scalp surfaces'),
('-A2', 'As above, when also feeding in non-brain extracted T2')))
filetypes = ['.nii.gz', '.nii', '.hdr', '.img']
class BetOptions(tkp.HasProperties):
inputImage = tkp.FilePath(exists=True, required=True)
outputImage = tkp.FilePath( required=True)
t2Image = tkp.FilePath(exists=True, required=lambda i: i.runChoice == '-A2')
inputImage = tkp.FilePath(exists=True, suffixes=filetypes, required=True)
outputImage = tkp.FilePath( required=True)
t2Image = tkp.FilePath(exists=True, suffixes=filetypes, required=lambda i: i.runChoice == '-A2')
runChoice = tkp.Choice(runChoices)
......@@ -264,15 +265,12 @@ class BetFrame(tk.Frame):
if __name__ == '__main__':
import logging
logging.basicConfig(format='%(levelname)s - %(funcName)s: %(message)s', level=logging.DEBUG)
#import logging
#logging.basicConfig(format='%(levelname)s - %(funcName)s: %(message)s', level=logging.DEBUG)
app = tk.Tk()
betopts = BetOptions()
betopts.inputImage = '/Users/paulmc/MNI152_T1_2mm.nii.gz'
betopts.outputImage = '/Users/paulmc/brain'
frame = BetFrame(app, betopts)
# stupid hack for testing under OS X - forces the TK
......
#!/usr/bin/env python
#
# runshell.py -
# runshell.py - Run a process, display its output in a Tkinter window.
#
# Author: Paul McCarthy <pauldmccarthy@gmail.com>
#
......
......@@ -277,17 +277,21 @@ class FilePath(String):
may be either a file or a directory.
"""
def __init__(self, exists=False, isFile=True, **kwargs):
def __init__(self, exists=False, isFile=True, suffixes=[], **kwargs):
"""
FilePath constructor. Optional arguments:
- exists: If True, the path must exist.
- isFile: If True, the path must be a file. If False, the
path must be a directory. This check is only
performed if exists=True.
- exists: If True, the path must exist.
- isFile: If True, the path must be a file. If False, the
path must be a directory. This check is only
performed if exists=True.
- suffixes: List of acceptable file suffixes (only relevant
if isFile is True).
"""
self.exists = exists
self.isFile = isFile
self.exists = exists
self.isFile = isFile
self.suffixes = suffixes
String.__init__(self, **kwargs)
......@@ -295,16 +299,32 @@ class FilePath(String):
String.validate(self, instance, value)
if value is None: return
if value == '': return
if value is None: return
if value == '': return
if not self.exists: return
if self.isFile:
values = [value]
# if suffixes have been specified, check to
# see if any file exists with each of the
# suffixes (in addition to the specified path)
if len(self.suffixes) > 0:
values.extend(['{}{}'.format(value, s) for s in self.suffixes])
if self.exists:
files = map(op.isfile, values)
if self.isFile and (not op.isfile(value)):
raise ValueError('Must be a file ({})'.format(value))
if not any(map(op.isfile, values)):
if len(self.suffixes) == 0:
raise ValueError('Must be a file ({})'.format(value))
else:
raise ValueError(
'Must be a file ending in [{}] ({})'.format(
','.join(self.suffixes), value))
if (not self.isFile) and (not op.isdir(value)):
raise ValueError('Must be a directory ({})'.format(value))
elif not op.isdir(value):
raise ValueError('Must be a directory ({})'.format(value))
class ListWrapper(object):
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment