/
template.py
54 lines (43 loc) · 1.81 KB
/
template.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
"""
This module defines a simple concrete implementation of the InputGenerator class that can be
used to facilitate writing large numbers of input files based on a template.
"""
from __future__ import annotations
from pathlib import Path
from string import Template
from monty.io import zopen
from pymatgen.io.core import InputGenerator, InputSet
__author__ = "Ryan Kingsbury"
__email__ = "RKingsbury@lbl.gov"
__status__ = "Development"
__date__ = "October 2021"
class TemplateInputGen(InputGenerator):
"""
Concrete implementation of InputGenerator that is based on a single template input
file with variables.
This class is provided as a low-barrier way to support new codes and to provide
an intuitive way for users to transition from manual scripts to pymatgen I/O
classes.
"""
def get_input_set( # type: ignore
self, template: str | Path, variables: dict | None = None, filename: str = "input.txt"
):
"""
Args:
template: the input file template containing variable strings to be
replaced.
variables: dict of variables to replace in the template. Keys are the
text to replaced with the values, e.g. {"TEMPERATURE": 298} will
replace the text $TEMPERATURE in the template. See Python's
Template.safe_substitute() method documentation for more details.
filename: name of the file to be written
"""
self.template = template
self.variables = variables or {}
self.filename = filename
# load the template
with zopen(self.template, "r") as f:
template_str = f.read()
# replace all variables
self.data = Template(template_str).safe_substitute(**self.variables)
return InputSet({self.filename: self.data})