File: apply_template.py

package info (click to toggle)
chemfp 1.0-1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 1,580 kB
  • sloc: python: 9,390; ansic: 2,363; makefile: 110
file content (40 lines) | stat: -rw-r--r-- 979 bytes parent folder | download
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
import sys
import os
from os.path import join, exists, getmtime
import inspect

import chemfp
import chemfp.arena
import chemfp.bitops

name = sys.argv[1]
template_name = name + ".txt"
rst_name = name + ".rst"

from jinja2 import Template, Environment, BaseLoader, TemplateNotFound

class LocalDirLoader(BaseLoader):
    def __init__(self):
        self.path = "."
    
    def get_source(self, environment, template):
        path = join(self.path, template)
        if not exists(path):
            raise TemplateNotFound(template)
        mtime = getmtime(path)
        with file(path) as f:
            source = f.read().decode('utf-8')
        return source, path, lambda: mtime == getmtime(path)
        

def docstring(f):
    return inspect.getdoc(f)

env = Environment(loader = LocalDirLoader())
env.filters['docstring'] = docstring
template = env.get_template(template_name)

page = template.render({"chemfp": chemfp})

with open(rst_name, "w") as f:
    f.write(page)