forked from niess/python-appimage
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtemplate.py
More file actions
41 lines (28 loc) · 898 Bytes
/
Copy pathtemplate.py
File metadata and controls
41 lines (28 loc) · 898 Bytes
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
import os
import re
import shutil
from .fs import make_tree
from .log import debug
__all__ = ['copy_template', 'load_template']
_template_pattern = re.compile('[{][{][ ]*([^{} ]+)[ ]*[}][}]')
def load_template(path, **kwargs):
'''Load a template file and substitue keywords
'''
with open(path) as f:
template = f.read()
def matcher(m):
tag = m.group(1)
try:
return kwargs[tag]
except KeyError:
return tag
return _template_pattern.sub(matcher, template)
def copy_template(path, destination, **kwargs):
'''Copy a template file and substitue keywords
'''
txt = load_template(path, **kwargs)
debug('COPY', '%s as %s', os.path.basename(path), destination)
make_tree(os.path.dirname(destination))
with open(destination, 'w') as f:
f.write(txt)
shutil.copymode(path, destination)