forked from gooofy/kaldi-adapt-lm
-
Notifications
You must be signed in to change notification settings - Fork 2
/
tools.py
55 lines (47 loc) · 1.57 KB
/
tools.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
55
"""Tools to handle system operations (copy, mkdir, simlink, etc.) and read templates"""
import os
import shutil
import errno
import logging
def symlink(targetfn, linkfn):
try:
os.symlink(targetfn, linkfn)
except OSError as e:
if e.errno == errno.EEXIST:
logging.debug('symlink %s -> %s already exists' % (targetfn, linkfn))
def mkdirs(path):
try:
os.makedirs(path)
except OSError as exception:
if exception.errno != errno.EEXIST:
raise
def copy_file (src, dst):
logging.debug("copying %s to %s" % (src, dst))
shutil.copy(src, dst)
def render_template(template_file, dst_file, **kwargs):
"""Copy template and substitute template strings
File `template_file` is copied to `dst_file`. Then, each template variable
is replaced by a value. Template variables are of the form
{{val}}
Example:
Contents of template_file:
VAR1={{val1}}
VAR2={{val2}}
VAR3={{val3}}
render_template(template_file, output_file, val1="hello", val2="world")
Contents of output_file:
VAR1=hello
VAR2=world
VAR3={{val3}}
:param template_file: Path to the template file.
:param dst_file: Path to the destination file.
:param kwargs: Keys correspond to template variables.
:return:
"""
with open(template_file) as f:
template_text = f.read()
dst_text = template_text
for key, value in kwargs.items():
dst_text = dst_text .replace("{{" + key + "}}", value)
with open(dst_file, "wt") as f:
f.write(dst_text)