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