easyvvuq.encoders.generic_template
1import os 2from string import Template 3import logging 4 5__copyright__ = """ 6 7 Copyright 2018 Robin A. Richardson, David W. Wright 8 9 This file is part of EasyVVUQ 10 11 EasyVVUQ is free software: you can redistribute it and/or modify 12 it under the terms of the Lesser GNU General Public License as published by 13 the Free Software Foundation, either version 3 of the License, or 14 (at your option) any later version. 15 16 EasyVVUQ is distributed in the hope that it will be useful, 17 but WITHOUT ANY WARRANTY; without even the implied warranty of 18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 19 Lesser GNU General Public License for more details. 20 21 You should have received a copy of the Lesser GNU General Public License 22 along with this program. If not, see <https://www.gnu.org/licenses/>. 23 24""" 25__license__ = "LGPL" 26 27 28def get_custom_template(template_txt, custom_delimiter='$'): 29 class CustomTemplate(Template): 30 delimiter = custom_delimiter 31 return CustomTemplate(template_txt) 32 33 34class GenericEncoder: 35 """GenericEncoder for substituting values into application template input. 36 37 Parameters 38 ---------- 39 40 Attributes 41 ---------- 42 43 """ 44 45 def __init__(self, template_fname, delimiter='$', target_filename="app_input.txt"): 46 self.delimiter = delimiter 47 self.target_filename = target_filename 48 self.template_fname = template_fname 49 50 def encode(self, params={}, target_dir=''): 51 """Substitutes `params` into a template application input, saves in 52 `target_dir` 53 54 Parameters 55 ---------- 56 params : dict 57 Parameter information in dictionary. 58 target_dir : str 59 Path to directory where application input will be written. 60 """ 61 62 try: 63 with open(self.template_fname, 'r') as template_file: 64 template_txt = template_file.read() 65 self.template = Template(template_txt) 66 except FileNotFoundError: 67 raise RuntimeError( 68 "the template file specified ({}) does not exist".format(self.template_fname)) 69 70 if not target_dir: 71 raise RuntimeError('No target directory specified to encoder') 72 73 str_params = {} 74 for key, value in params.items(): 75 str_params[key] = str(value) 76 77 try: 78 app_input_txt = self.template.substitute(str_params) 79 except KeyError as e: 80 self._log_substitution_failure(e) 81 82 # Write target input file 83 target_file_path = os.path.join(target_dir, self.target_filename) 84 with open(target_file_path, 'w') as fp: 85 fp.write(app_input_txt) 86 87 def _log_substitution_failure(self, exception): 88 reasoning = (f"\nFailed substituting into template " 89 f"{self.template_fname}.\n" 90 f"KeyError: {str(exception)}.\n") 91 logging.error(reasoning) 92 93 raise KeyError(reasoning)
def
get_custom_template(template_txt, custom_delimiter='$'):
class
GenericEncoder:
35class GenericEncoder: 36 """GenericEncoder for substituting values into application template input. 37 38 Parameters 39 ---------- 40 41 Attributes 42 ---------- 43 44 """ 45 46 def __init__(self, template_fname, delimiter='$', target_filename="app_input.txt"): 47 self.delimiter = delimiter 48 self.target_filename = target_filename 49 self.template_fname = template_fname 50 51 def encode(self, params={}, target_dir=''): 52 """Substitutes `params` into a template application input, saves in 53 `target_dir` 54 55 Parameters 56 ---------- 57 params : dict 58 Parameter information in dictionary. 59 target_dir : str 60 Path to directory where application input will be written. 61 """ 62 63 try: 64 with open(self.template_fname, 'r') as template_file: 65 template_txt = template_file.read() 66 self.template = Template(template_txt) 67 except FileNotFoundError: 68 raise RuntimeError( 69 "the template file specified ({}) does not exist".format(self.template_fname)) 70 71 if not target_dir: 72 raise RuntimeError('No target directory specified to encoder') 73 74 str_params = {} 75 for key, value in params.items(): 76 str_params[key] = str(value) 77 78 try: 79 app_input_txt = self.template.substitute(str_params) 80 except KeyError as e: 81 self._log_substitution_failure(e) 82 83 # Write target input file 84 target_file_path = os.path.join(target_dir, self.target_filename) 85 with open(target_file_path, 'w') as fp: 86 fp.write(app_input_txt) 87 88 def _log_substitution_failure(self, exception): 89 reasoning = (f"\nFailed substituting into template " 90 f"{self.template_fname}.\n" 91 f"KeyError: {str(exception)}.\n") 92 logging.error(reasoning) 93 94 raise KeyError(reasoning)
GenericEncoder for substituting values into application template input.
Parameters
- Attributes
- ----------
def
encode(self, params={}, target_dir=''):
51 def encode(self, params={}, target_dir=''): 52 """Substitutes `params` into a template application input, saves in 53 `target_dir` 54 55 Parameters 56 ---------- 57 params : dict 58 Parameter information in dictionary. 59 target_dir : str 60 Path to directory where application input will be written. 61 """ 62 63 try: 64 with open(self.template_fname, 'r') as template_file: 65 template_txt = template_file.read() 66 self.template = Template(template_txt) 67 except FileNotFoundError: 68 raise RuntimeError( 69 "the template file specified ({}) does not exist".format(self.template_fname)) 70 71 if not target_dir: 72 raise RuntimeError('No target directory specified to encoder') 73 74 str_params = {} 75 for key, value in params.items(): 76 str_params[key] = str(value) 77 78 try: 79 app_input_txt = self.template.substitute(str_params) 80 except KeyError as e: 81 self._log_substitution_failure(e) 82 83 # Write target input file 84 target_file_path = os.path.join(target_dir, self.target_filename) 85 with open(target_file_path, 'w') as fp: 86 fp.write(app_input_txt)
Substitutes params into a template application input, saves in
target_dir
Parameters
- params (dict): Parameter information in dictionary.
- target_dir (str): Path to directory where application input will be written.