-
Notifications
You must be signed in to change notification settings - Fork 1
/
template.py
74 lines (61 loc) · 2.43 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys
import os
import platform
import dotbot
def _inject():
# Find submodules
root_dir = os.path.dirname(os.path.realpath(__file__))
path_jinja = os.path.join(root_dir, 'lib/jinja/src/')
path_markupsafe = os.path.join(root_dir, 'lib/markupsafe/src')
# Update path
sys.path.insert(0, path_markupsafe)
sys.path.insert(0, path_jinja)
_inject()
from jinja2 import Environment, FileSystemLoader
class Template(dotbot.Plugin):
_directive = 'template'
def can_handle(self, directive):
return directive == self._directive
def handle(self, directive, data):
if directive == self._directive:
for temp in data:
target, opts = list(temp.items())[0]
self._log.info("Rendering template %s" % target)
try:
# Get target path
target = os.path.expanduser(target)
# Get parameters for render
params = self._parse_params(opts['params'])
# Load template and render
self._render_template(opts['source_file'], params, target)
except:
self._log.error("Could not render %s" % target)
return False
return True
else:
raise ValueError('Cannot handle this directive %s' % directive)
def _parse_params(self, params):
params = self._add_homedir(params)
params = self._parse_platform_specific(params)
return params
def _add_homedir(self, params):
params['HOME_DIR'] = os.environ['HOME']
return params
def _parse_platform_specific(self, params):
if '__UNAME__' in params:
uname = platform.system()
for k in params['__UNAME__'].keys():
params[k] = params['__UNAME__'][k][uname]
del params['__UNAME__']
return params
def _render_template(self, template_file, params, target):
# Load template
cwd = self._context.base_directory()
template_dir = os.path.dirname(os.path.abspath(cwd + '/' + template_file))
jinja_env = Environment(loader=FileSystemLoader(template_dir))
template = jinja_env.get_template(os.path.basename(template_file))
# Template and write output
with open(target, 'w') as target_file:
target_file.write(template.render(params))