@@ -305,11 +305,6 @@ def validate(self, objekt, name, value, return_pathlike=False):
305
305
return value
306
306
307
307
308
- # Patch in traits these two new
309
- traits .File = File
310
- traits .Directory = Directory
311
-
312
-
313
308
class ImageFile (File ):
314
309
"""Defines a trait whose value must be a known neuroimaging file."""
315
310
@@ -486,7 +481,7 @@ def inner_traits(self):
486
481
return self .types
487
482
488
483
489
- class PatchedEither (TraitType ):
484
+ class Either (TraitType ):
490
485
"""Defines a trait whose value can be any of of a specified list of traits."""
491
486
492
487
def __init__ (self , * traits , ** metadata ):
@@ -501,7 +496,7 @@ def as_ctrait(self):
501
496
502
497
503
498
traits .Tuple = Tuple
504
- traits .Either = PatchedEither
499
+ traits .Either = Either
505
500
506
501
507
502
def _rebase_path (value , cwd ):
@@ -520,34 +515,6 @@ def _rebase_path(value, cwd):
520
515
return value
521
516
522
517
523
- def rebase_path_traits (thistrait , value , cwd ):
524
- """Rebase a BasePath-derived trait given an interface spec."""
525
- if thistrait .is_trait_type (BasePath ):
526
- value = _rebase_path (value , cwd )
527
- elif thistrait .is_trait_type (traits .List ):
528
- innertrait , = thistrait .inner_traits
529
- if not isinstance (value , (list , tuple )):
530
- value = rebase_path_traits (innertrait , value , cwd )
531
- else :
532
- value = [rebase_path_traits (innertrait , v , cwd )
533
- for v in value ]
534
- elif thistrait .is_trait_type (traits .Dict ):
535
- _ , innertrait = thistrait .inner_traits
536
- value = {k : rebase_path_traits (innertrait , v , cwd )
537
- for k , v in value .items ()}
538
- elif thistrait .is_trait_type (Tuple ):
539
- value = tuple ([rebase_path_traits (subtrait , v , cwd )
540
- for subtrait , v in zip (thistrait .inner_traits , value )])
541
- elif thistrait .alternatives :
542
- is_str = [f .is_trait_type ((traits .String , traits .BaseStr , traits .BaseBytes , Str ))
543
- for f in thistrait .alternatives ]
544
- if any (is_str ) and isinstance (value , (bytes , str )) and not value .startswith ('/' ):
545
- return value
546
- for subtrait in thistrait .alternatives :
547
- value = rebase_path_traits (subtrait , value , cwd )
548
- return value
549
-
550
-
551
518
def _resolve_path (value , cwd ):
552
519
if isinstance (value , list ):
553
520
return [_resolve_path (v , cwd ) for v in value ]
@@ -562,29 +529,41 @@ def _resolve_path(value, cwd):
562
529
return value
563
530
564
531
565
- def resolve_path_traits ( thistrait , value , cwd ):
566
- """Resolve a BasePath-derived trait given an interface spec ."""
532
+ def _recurse_on_path_traits ( func , thistrait , value , cwd ):
533
+ """Run func recursively on BasePath-derived traits ."""
567
534
if thistrait .is_trait_type (BasePath ):
568
- value = _resolve_path (value , cwd )
535
+ value = func (value , cwd )
569
536
elif thistrait .is_trait_type (traits .List ):
570
537
innertrait , = thistrait .inner_traits
571
538
if not isinstance (value , (list , tuple )):
572
- value = resolve_path_traits ( innertrait , value , cwd )
539
+ value = _recurse_on_path_traits ( func , innertrait , value , cwd )
573
540
else :
574
- value = [resolve_path_traits ( innertrait , v , cwd )
541
+ value = [_recurse_on_path_traits ( func , innertrait , v , cwd )
575
542
for v in value ]
576
543
elif thistrait .is_trait_type (traits .Dict ):
577
544
_ , innertrait = thistrait .inner_traits
578
- value = {k : resolve_path_traits ( innertrait , v , cwd )
545
+ value = {k : _recurse_on_path_traits ( func , innertrait , v , cwd )
579
546
for k , v in value .items ()}
580
547
elif thistrait .is_trait_type (Tuple ):
581
- value = tuple ([resolve_path_traits ( subtrait , v , cwd )
548
+ value = tuple ([_recurse_on_path_traits ( func , subtrait , v , cwd )
582
549
for subtrait , v in zip (thistrait .inner_traits , value )])
583
550
elif thistrait .alternatives :
584
551
is_str = [f .is_trait_type ((traits .String , traits .BaseStr , traits .BaseBytes , Str ))
585
552
for f in thistrait .alternatives ]
586
553
if any (is_str ) and isinstance (value , (bytes , str )) and not value .startswith ('/' ):
587
554
return value
588
- for subtrait in thistrait .alternatives :
589
- value = resolve_path_traits (subtrait , value , cwd )
555
+ is_basepath = [f .is_trait_type (BasePath ) for f in thistrait .alternatives ]
556
+ if any (is_basepath ):
557
+ subtrait = thistrait .alternatives [is_basepath .index (True )]
558
+ value = _recurse_on_path_traits (func , subtrait , value , cwd )
590
559
return value
560
+
561
+
562
+ def rebase_path_traits (thistrait , value , cwd ):
563
+ """Rebase a BasePath-derived trait given an interface spec."""
564
+ return _recurse_on_path_traits (_rebase_path , thistrait , value , cwd )
565
+
566
+
567
+ def resolve_path_traits (thistrait , value , cwd ):
568
+ """Resolve a BasePath-derived trait given an interface spec."""
569
+ return _recurse_on_path_traits (_resolve_path , thistrait , value , cwd )
0 commit comments