@@ -525,54 +525,68 @@ def copyfile_input(inputs, output_dir):
525
525
526
526
527
527
# not sure if this might be useful for Function Task
528
- def template_update (inputs , output_dir , map_copyfiles = None ):
528
+ def template_update (inputs , output_dir , state_ind = None , map_copyfiles = None ):
529
529
"""
530
530
Update all templates that are present in the input spec.
531
531
532
532
Should be run when all inputs used in the templates are already set.
533
533
534
534
"""
535
- dict_ = attr .asdict (inputs )
535
+
536
+ inputs_dict_st = attr .asdict (inputs )
536
537
if map_copyfiles is not None :
537
- dict_ .update (map_copyfiles )
538
+ inputs_dict_st .update (map_copyfiles )
539
+
540
+ if state_ind is not None :
541
+ for k , v in state_ind .items ():
542
+ k = k .split ("." )[1 ]
543
+ inputs_dict_st [k ] = inputs_dict_st [k ][v ]
538
544
539
545
from .specs import attr_fields
540
546
541
547
fields_templ = [
542
548
fld for fld in attr_fields (inputs ) if fld .metadata .get ("output_file_template" )
543
549
]
550
+ dict_mod = {}
544
551
for fld in fields_templ :
545
552
if fld .type not in [str , ty .Union [str , bool ]]:
546
553
raise Exception (
547
554
f"fields with output_file_template"
548
555
" has to be a string or Union[str, bool]"
549
556
)
550
- dict_ [fld .name ] = template_update_single (
551
- field = fld , inputs = inputs , output_dir = output_dir
557
+ dict_mod [fld .name ] = template_update_single (
558
+ field = fld ,
559
+ inputs = inputs ,
560
+ inputs_dict_st = inputs_dict_st ,
561
+ output_dir = output_dir ,
552
562
)
553
- # using is and == so it covers list and numpy arrays
554
- updated_templ_dict = {
555
- k : v
556
- for k , v in dict_ .items ()
557
- if not (getattr (inputs , k ) is v or getattr (inputs , k ) == v )
558
- }
559
- return updated_templ_dict
563
+ # adding elements from map_copyfiles to fields with templates
564
+ if map_copyfiles :
565
+ dict_mod .update (map_copyfiles )
566
+ return dict_mod
560
567
561
568
562
- def template_update_single (field , inputs , output_dir = None , spec_type = "input" ):
569
+ def template_update_single (
570
+ field , inputs , inputs_dict_st = None , output_dir = None , spec_type = "input"
571
+ ):
563
572
"""Update a single template from the input_spec or output_spec
564
573
based on the value from inputs_dict
565
574
(checking the types of the fields, that have "output_file_template)"
566
575
"""
567
576
from .specs import File , MultiOutputFile , Directory
568
577
578
+ # if input_dict_st with state specific value is not available,
579
+ # the dictionary will be created from inputs object
580
+ if inputs_dict_st is None :
581
+ inputs_dict_st = attr .asdict (inputs )
582
+
569
583
if spec_type == "input" :
570
584
if field .type not in [str , ty .Union [str , bool ]]:
571
585
raise Exception (
572
586
f"fields with output_file_template"
573
587
"has to be a string or Union[str, bool]"
574
588
)
575
- inp_val_set = getattr ( inputs , field .name )
589
+ inp_val_set = inputs_dict_st [ field .name ]
576
590
if inp_val_set is not attr .NOTHING and not isinstance (inp_val_set , (str , bool )):
577
591
raise Exception (
578
592
f"{ field .name } has to be str or bool, but { inp_val_set } set"
@@ -589,13 +603,13 @@ def template_update_single(field, inputs, output_dir=None, spec_type="input"):
589
603
else :
590
604
raise Exception (f"spec_type can be input or output, but { spec_type } provided" )
591
605
# for inputs that the value is set (so the template is ignored)
592
- if spec_type == "input" and isinstance (getattr ( inputs , field .name ) , str ):
593
- return getattr ( inputs , field .name )
594
- elif spec_type == "input" and getattr ( inputs , field .name ) is False :
606
+ if spec_type == "input" and isinstance (inputs_dict_st [ field .name ] , str ):
607
+ return inputs_dict_st [ field .name ]
608
+ elif spec_type == "input" and inputs_dict_st [ field .name ] is False :
595
609
# if input fld is set to False, the fld shouldn't be used (setting NOTHING)
596
610
return attr .NOTHING
597
611
else : # inputs_dict[field.name] is True or spec_type is output
598
- value = _template_formatting (field , inputs )
612
+ value = _template_formatting (field , inputs , inputs_dict_st )
599
613
# changing path so it is in the output_dir
600
614
if output_dir and value is not attr .NOTHING :
601
615
# should be converted to str, it is also used for input fields that should be str
@@ -607,7 +621,7 @@ def template_update_single(field, inputs, output_dir=None, spec_type="input"):
607
621
return value
608
622
609
623
610
- def _template_formatting (field , inputs ):
624
+ def _template_formatting (field , inputs , inputs_dict_st ):
611
625
"""Formatting the field template based on the values from inputs.
612
626
Taking into account that the field with a template can be a MultiOutputFile
613
627
and the field values needed in the template can be a list -
@@ -633,7 +647,7 @@ def _template_formatting(field, inputs):
633
647
634
648
for fld in inp_fields :
635
649
fld_name = fld [1 :- 1 ] # extracting the name form {field_name}
636
- fld_value = getattr ( inputs , fld_name )
650
+ fld_value = inputs_dict_st [ fld_name ]
637
651
if fld_value is attr .NOTHING :
638
652
# if value is NOTHING, nothing should be added to the command
639
653
return attr .NOTHING
0 commit comments