Skip to content

Instantly share code, notes, and snippets.

@chrisgorgo
Created April 9, 2010 13:39
Show Gist options
  • Save chrisgorgo/361167 to your computer and use it in GitHub Desktop.
Save chrisgorgo/361167 to your computer and use it in GitHub Desktop.
'''
Created on Apr 7, 2010
@author: filo
'''
from nipype.interfaces.base import traits, isdefined, TraitedSpec
from enthought.traits.trait_base import _Undefined
class MultiPath(traits.List):
info_text = 'a list of paths'
def __init__(self, trait = None, value = None, **metadata):
if trait:
self.info_text = 'a list of %s' % trait.info()
super(MultiPath, self).__init__(trait, value,
**metadata)
def set(self, object, name, value):
self.set_value(object, name,self.validate(object,name,value))
def validate(self, object, name, value):
if not isdefined(value):
return value
newvalue = value
if isinstance(value, str):
newvalue = [value]
value = super(MultiPath, self).validate(object, name, newvalue)
if len(value) > 0:
return value
self.error( object, name, value )
class OutputMultiPath(MultiPath):
def get(self, object, name):
value = self.get_value(object, name)
if len(value) == 0:
return _Undefined()
elif len(value)==1:
return value[0]
else:
return value
class InputMultiPath(MultiPath):
def get(self, object, name):
value = self.get_value(object, name)
if len(value) == 0:
return _Undefined()
else:
return value
class Test(TraitedSpec):
foo = OutputMultiPath(traits.File())
boo = InputMultiPath(traits.File())
koo = traits.List()
t = Test()
print t.foo
t.foo = 'abc'
print t.foo
t.foo = ['abc']
print t.foo
t.foo = 'abc'
print t.foo
t.foo = ['abc', 'abc']
print t.foo
print t.boo
t.boo = 'abc'
print t.boo
t.boo = ['abc']
print t.boo
t.boo = 'abc'
print t.boo
t.boo = ['abc', 'abc']
print t.boo
t.boo = []
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment