This repository has been archived by the owner on Jul 21, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
/
templates.py
executable file
·66 lines (56 loc) · 2.17 KB
/
templates.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
#!/usr/bin/env python2
import re
import os
import os.path
import sys
PREPROCESS_RE = re.compile("^\s*#")
INCLUDE_RE = re.compile('^\s*#include\s*"([-\w]+)"')
class Templates (object):
def __init__(self, templatedirs, extension):
self.extension = extension
self.cached_templates = {}
self.templatedirs = templatedirs
def get(self, name):
if name in self.cached_templates:
return self.cached_templates[name]
for templatedir in self.templatedirs:
if self.has_template_in(templatedir, name):
return self.get_in(templatedir, name)
elif self.has_template_in(templatedir, name, 'DEFAULT', 'txt'):
return self.get_in(templatedir, name, 'DEFAULT', 'txt')
return ""
def has_template_in(self, templatedir, name, subdir=None, extension=None):
# FIXME better test
return os.path.exists(self.get_template_filename(templatedir, name,
subdir,
extension))
def get_in(self, templatedir, name, subdir=None, extension=None):
filename = self.get_template_filename(templatedir, name, subdir, extension)
f = open(filename, "r")
template = self.read_template(f);
f.close()
self.cached_templates[name] = template
return template
def read_template(self, f):
res = ""
for line in f.readlines():
if PREPROCESS_RE.match(line):
res += self.preprocessline(line)
else:
res += line
return res
def preprocessline(self, line):
m = INCLUDE_RE.match(line)
if m:
return self.get(m.group(1))
else:
return line
def get_template_filename(self, templatedir, name, subdir=None, extension=None):
if not subdir:
subdir = self.extension
if not extension:
extension = self.extension
return os.path.join(templatedir, subdir, name + "." + extension)
if __name__ == '__main__':
templates = Templates(['templates'], sys.argv[1])
print templates.get(sys.argv[2])