render.py 3.05 KB
Newer Older
Robert McGibbon's avatar
Sphinx  
Robert McGibbon committed
1
2
3
4
5
6
7
8
9
10
11
12
from os.path import dirname, join, splitext
from glob import glob
import inspect

import jinja2
import simtk.openmm as mm
from simtk.openmm import app

def fullname(klass):
    return klass.__module__ + '.' + klass.__name__


13
14
15
16
17
#        'integrators': [],
#        'library_extras': [],
#         'forces': [],

def library_template_variables():
Robert McGibbon's avatar
Sphinx  
Robert McGibbon committed
18
19
    data = {
        'integrators': [],
20
21
        'library_extras': [],
        'forces': [],
Robert McGibbon's avatar
Sphinx  
Robert McGibbon committed
22
23
24
25
26
27
28
29
30
31
32
33
34
35
    }

    mm_klasses = inspect.getmembers(mm, predicate=inspect.isclass)

    # gather all Force subclasses
    for name, klass in mm_klasses:
        if issubclass(klass, mm.Force):
            data['forces'].append(fullname(klass))

    # gather all Integrator subclasses
    for _, klass in mm_klasses:
        if issubclass(klass, mm.Integrator):
            data['integrators'].append(fullname(klass))

36
37
38
39
40
41
42
43
44
    # gather all extra subclasses in simtk.openmm.openmm
    exclude = ['simtk.openmm.openmm.Platform', 'simtk.openmm.openmm.Context',
              'simtk.openmm.openmm.System', 'simtk.openmm.openmm.State']
    exclude.extend(data['forces'])
    exclude.extend(data['integrators'])
    exclude.extend([
        'simtk.openmm.openmm.SwigPyIterator',
        'simtk.openmm.openmm.OpenMMException'])

Robert McGibbon's avatar
Sphinx  
Robert McGibbon committed
45
46
    for _, klass in mm_klasses:
        full = fullname(klass)
47
48
49
50
        if full not in exclude and not klass.__name__[0].islower():
            data['library_extras'].append(full)

    return data
Robert McGibbon's avatar
Sphinx  
Robert McGibbon committed
51
52


53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
def app_template_variables():
    data = {
        'reporters': [],
        'fileclasses': [],
        'app_extras': [],
    }

    app_klasses = inspect.getmembers(app, predicate=inspect.isclass)

    # gather all Reporters
    for name, klass in app_klasses:
        if name.endswith('Reporter'):
            data['reporters'].append(fullname(klass))

    # gather all classes with "File" in the name
    for name, klass in app_klasses:
        if 'File' in name:
            data['fileclasses'].append(fullname(klass))

    # gather all extra subclasses in simtk.openmm.app
    exclude = ['simtk.openmm.app.topology.Topology',
               'simtk.openmm.app.modeller.Modeller',
               'simtk.openmm.app.forcefield.ForceField',
               'simtk.openmm.app.simulation.Simulation']
    exclude.extend(data['reporters'])
    exclude.extend(data['fileclasses'])

    for _, klass in app_klasses:
        full = fullname(klass)
        if full not in exclude and not klass.__name__[0].islower():
            data['app_extras'].append(full)
Robert McGibbon's avatar
Sphinx  
Robert McGibbon committed
84
85
86
87
88
89
90
91

    return data


def main():
    here = dirname(__file__)
    templateLoader = jinja2.FileSystemLoader(here)
    templateEnv = jinja2.Environment(loader=templateLoader)
92
93
    data = library_template_variables()
    data.update(app_template_variables())
Robert McGibbon's avatar
Sphinx  
Robert McGibbon committed
94
95
96
97
98
99
100
101
102
103
104
105
106

    for template_fn in glob(join(here, '*.jinja2')):
        output_fn = splitext(template_fn)[0]
        print('Rendering %s to %s...' % (template_fn, output_fn))

        template = templateEnv.get_template(template_fn)
        output_text = template.render(data)
        with open(output_fn, 'w') as f:
            f.write(output_text)


if __name__ == '__main__':
    main()