2
2
3
3
import math
4
4
import os
5
- import shutil
6
5
import warnings
7
6
from collections .abc import Callable
8
7
from dataclasses import dataclass
@@ -59,8 +58,6 @@ class TransformFunctionDefinition:
59
58
60
59
@dataclass
61
60
class GenKwConfig (ParameterConfig ):
62
- template_file : str | None
63
- output_file : str | None
64
61
transform_function_definitions : list [TransformFunctionDefinition ]
65
62
forward_init_file : str | None = None
66
63
@@ -85,6 +82,36 @@ def __contains__(self, item: str) -> bool:
85
82
def __len__ (self ) -> int :
86
83
return len (self .transform_functions )
87
84
85
+ @classmethod
86
+ def templates_from_config (
87
+ cls , gen_kw : list [str | dict [str , str ]]
88
+ ) -> tuple [str , str ] | None :
89
+ gen_kw_key = cast (str , gen_kw [0 ])
90
+ positional_args = cast (list [str ], gen_kw [:- 1 ])
91
+
92
+ if len (positional_args ) == 4 :
93
+ output_file = positional_args [2 ]
94
+ parameter_file_context = positional_args [3 ][0 ]
95
+ template_file = _get_abs_path (positional_args [1 ][0 ])
96
+ if not os .path .isfile (template_file ):
97
+ raise ConfigValidationError .with_context (
98
+ f"No such template file: { template_file } " , positional_args [1 ]
99
+ )
100
+ elif Path (template_file ).stat ().st_size == 0 :
101
+ token = getattr (parameter_file_context , "token" , parameter_file_context )
102
+ ConfigWarning .deprecation_warn (
103
+ f"The template file for GEN_KW ({ gen_kw_key } ) is empty. If templating is not needed, you "
104
+ f"can use GEN_KW with just the distribution file instead: GEN_KW { gen_kw_key } { token } " ,
105
+ positional_args [1 ],
106
+ )
107
+ if output_file .startswith ("/" ):
108
+ raise ConfigValidationError .with_context (
109
+ f"Output file cannot have an absolute path { output_file } " ,
110
+ positional_args [2 ],
111
+ )
112
+ return template_file , output_file
113
+ return None
114
+
88
115
@classmethod
89
116
def from_config_list (cls , gen_kw : list [str | dict [str , str ]]) -> Self :
90
117
gen_kw_key = cast (str , gen_kw [0 ])
@@ -99,26 +126,9 @@ def from_config_list(cls, gen_kw: list[str | dict[str, str]]) -> Self:
99
126
if len (positional_args ) == 2 :
100
127
parameter_file_contents = positional_args [1 ][1 ]
101
128
parameter_file_context = positional_args [1 ][0 ]
102
- template_file = None
103
- output_file = None
104
129
elif len (positional_args ) == 4 :
105
- output_file = positional_args [2 ]
106
130
parameter_file_contents = positional_args [3 ][1 ]
107
131
parameter_file_context = positional_args [3 ][0 ]
108
- template_file = _get_abs_path (positional_args [1 ][0 ])
109
- if not os .path .isfile (template_file ):
110
- errors .append (
111
- ConfigValidationError .with_context (
112
- f"No such template file: { template_file } " , positional_args [1 ]
113
- )
114
- )
115
- elif Path (template_file ).stat ().st_size == 0 :
116
- token = getattr (parameter_file_context , "token" , parameter_file_context )
117
- ConfigWarning .deprecation_warn (
118
- f"The template file for GEN_KW ({ gen_kw_key } ) is empty. If templating is not needed, you "
119
- f"can use GEN_KW with just the distribution file instead: GEN_KW { gen_kw_key } { token } " ,
120
- positional_args [1 ],
121
- )
122
132
123
133
else :
124
134
raise ConfigValidationError (
@@ -185,8 +195,6 @@ def from_config_list(cls, gen_kw: list[str | dict[str, str]]) -> Self:
185
195
return cls (
186
196
name = gen_kw_key ,
187
197
forward_init = forward_init ,
188
- template_file = template_file ,
189
- output_file = output_file ,
190
198
forward_init_file = init_file ,
191
199
transform_function_definitions = transform_function_definitions ,
192
200
update = update_parameter ,
@@ -358,21 +366,21 @@ def parse_value(value: float | int | str) -> float | int | str:
358
366
if tf .use_log
359
367
}
360
368
361
- if self .template_file is not None and self .output_file is not None :
362
- target_file = substitute_runpath_name (
363
- self .output_file , real_nr , ensemble .iteration
364
- )
365
- target_file = target_file .removeprefix ("/" )
366
- (run_path / target_file ).parent .mkdir (exist_ok = True , parents = True )
367
- template_file_path = (
368
- ensemble .experiment .mount_point / Path (self .template_file ).name
369
- )
370
- with open (template_file_path , encoding = "utf-8" ) as f :
371
- template = f .read ()
372
- for key , value in data .items ():
373
- template = template .replace (f"<{ key } >" , f"{ value :.6g} " )
374
- with open (run_path / target_file , "w" , encoding = "utf-8" ) as f :
375
- f .write (template )
369
+ # if self.template_file is not None and self.output_file is not None:
370
+ # target_file = substitute_runpath_name(
371
+ # self.output_file, real_nr, ensemble.iteration
372
+ # )
373
+ # target_file = target_file.removeprefix("/")
374
+ # (run_path / target_file).parent.mkdir(exist_ok=True, parents=True)
375
+ # template_file_path = (
376
+ # ensemble.experiment.mount_point / Path(self.template_file).name
377
+ # )
378
+ # with open(template_file_path, encoding="utf-8") as f:
379
+ # template = f.read()
380
+ # for key, value in data.items():
381
+ # template = template.replace(f"<{key}>", f"{value:.6g}")
382
+ # with open(run_path / target_file, "w", encoding="utf-8") as f:
383
+ # f.write(template)
376
384
if log10_data :
377
385
return {self .name : data , f"LOG10_{ self .name } " : log10_data }
378
386
else :
@@ -542,13 +550,13 @@ def _parse_transform_function_definition(
542
550
calc_func = PRIOR_FUNCTIONS [t .param_name ],
543
551
)
544
552
545
- def save_experiment_data (self , experiment_path : Path ) -> None :
546
- if self .template_file :
547
- incoming_template_file_path = Path (self .template_file )
548
- template_file_path = Path (
549
- experiment_path / incoming_template_file_path .name
550
- )
551
- shutil .copyfile (incoming_template_file_path , template_file_path )
553
+ # def save_experiment_data(self, experiment_path: Path) -> None:
554
+ # if self.template_file:
555
+ # incoming_template_file_path = Path(self.template_file)
556
+ # template_file_path = Path(
557
+ # experiment_path / incoming_template_file_path.name
558
+ # )
559
+ # shutil.copyfile(incoming_template_file_path, template_file_path)
552
560
553
561
554
562
@dataclass
0 commit comments