Google analytics in README
[wrigit.git] / util.py
blob1a4344540f9b06357ed2263bfed78bde84550621
1 import os, sys
2 from mako.lookup import TemplateLookup
4 from cfg import cfg
7 rfc3339 = "%Y-%m-%dT%H:%M:%S"
10 def mako():
11 templates = TemplateLookup(directories=[cfg['templatedir']])
12 def render(name, **namespace):
13 import cgi
14 return templates.get_template(name).render(
15 cgi=cgi,
16 **namespace)
17 return render
18 mako = mako()
20 # gnu make like functinality
21 def last_modified(filename):
22 return os.stat(filename).st_mtime
24 def make(target, deps, content_generator, *args, **kwargs):
25 if cfg['regen'] or not os.path.exists(target) or \
26 True in [last_modified(target) < last_modified(d) for d in deps]:
27 # requires update
28 print '>', target
29 content = content_generator(*args, **kwargs)
30 open(target, 'w').write(content)
32 def make_mako(target, deps, name, **additional_namespace):
33 """
34 Wrapper around make/mako. Introduces mako files itself as one of deps
35 """
36 templatefile = os.path.join(cfg['templatedir'], name)
38 namespace = sys._getframe(1).f_locals
39 namespace.update(additional_namespace)
41 return make(target, deps+[templatefile], mako, name, **namespace)
43 __all__ = ['rfc3339', 'mako', 'make', 'make_mako', 'last_modified']