render.py 4.56 KB
Newer Older
Robert McGibbon's avatar
Robert McGibbon committed
1
2
3
4
5
"""
The function of this script is to render the Jinja2 templates in the current
directory into input files for sphinx. It introspects the OpenMM Python module
to find all of the classes and formats them for inclusion into the templates.
"""
Robert McGibbon's avatar
Robert McGibbon committed
6
from os.path import dirname, join, splitext, basename
Robert McGibbon's avatar
Sphinx  
Robert McGibbon committed
7
8
9
10
from glob import glob
import inspect

import jinja2
11
12
import openmm
import openmm.app
Robert McGibbon's avatar
Sphinx  
Robert McGibbon committed
13

Robert McGibbon's avatar
Robert McGibbon committed
14
15


Robert McGibbon's avatar
Sphinx  
Robert McGibbon committed
16
17
18
19
def fullname(klass):
    return klass.__module__ + '.' + klass.__name__


20
def library_template_variables():
Robert McGibbon's avatar
Robert McGibbon committed
21
22
23
    """Create the data structure available to the Jinja2 renderer when
    filling in the templates.

24
    This function extracts all of classes in ``openmm.openmm`` and returns
Robert McGibbon's avatar
Robert McGibbon committed
25
26
27
28
29
30
    a dictionary with them grouped into three lists, the integrators, the forces,
    and the remainder (library_extras).

    A couple core classes are skipped, because they're included manually in the
    template.
    """
Robert McGibbon's avatar
Sphinx  
Robert McGibbon committed
31
32
    data = {
        'integrators': [],
33
        'forces': [],
Robert McGibbon's avatar
Robert McGibbon committed
34
        'library_extras': [],
35
        'units': [],
Robert McGibbon's avatar
Sphinx  
Robert McGibbon committed
36
37
    }

38
    mm_klasses = inspect.getmembers(openmm, predicate=inspect.isclass)
Robert McGibbon's avatar
Robert McGibbon committed
39

Robert McGibbon's avatar
Sphinx  
Robert McGibbon committed
40
41
42

    # gather all Force subclasses
    for name, klass in mm_klasses:
43
        if issubclass(klass, openmm.openmm.Force):
Robert McGibbon's avatar
Sphinx  
Robert McGibbon committed
44
45
46
47
            data['forces'].append(fullname(klass))

    # gather all Integrator subclasses
    for _, klass in mm_klasses:
48
        if issubclass(klass, openmm.openmm.Integrator):
Robert McGibbon's avatar
Sphinx  
Robert McGibbon committed
49
50
            data['integrators'].append(fullname(klass))

51
    # gather all extra subclasses in openmm.openmm
Robert McGibbon's avatar
Robert McGibbon committed
52
53

    # core classes that are already included in library.rst.jinja2
54
55
    exclude = ['openmm.openmm.Platform', 'openmm.openmm.Context',
              'openmm.openmm.System', 'openmm.openmm.State']
56
57
    exclude.extend(data['forces'])
    exclude.extend(data['integrators'])
Robert McGibbon's avatar
Robert McGibbon committed
58
59

    # these classes are useless and not worth documenting.
60
    exclude.extend([
61
        'openmm.openmm.SwigPyIterator',
Peter Eastman's avatar
Peter Eastman committed
62
63
64
        'openmm.openmm.OpenMMException',
        'openmm.openmm.ComputationWrapper',
        'openmm.openmm.PythonForceProxy'])
65

Robert McGibbon's avatar
Sphinx  
Robert McGibbon committed
66
67
    for _, klass in mm_klasses:
        full = fullname(klass)
68
69
70
        if full not in exclude and not klass.__name__[0].islower():
            data['library_extras'].append(full)

71
    # gather units related classes
72
    unit_klasses = inspect.getmembers(openmm.unit, predicate=inspect.isclass)
73
74
75
    for name, klass in unit_klasses:
        data['units'].append(fullname(klass))

76
    return data
Robert McGibbon's avatar
Sphinx  
Robert McGibbon committed
77
78


79
def app_template_variables():
Robert McGibbon's avatar
Robert McGibbon committed
80
81
82
    """Create the data structure available to the Jinja2 renderer when
    filling in the templates.

83
    This function extracts all of classes in ``openmm.app`` and returns
Robert McGibbon's avatar
Robert McGibbon committed
84
85
86
87
88
89
    a dictionary with them grouped into three lists, the reporters, the
    classes with the word "File" in the name, and the remainder.

    Four classes are skipped (see exclude), because they're included manually
    in the template.
    """
90
91
92
93
94
    data = {
        'reporters': [],
        'fileclasses': [],
        'app_extras': [],
    }
95
    app_klasses = inspect.getmembers(openmm.app, predicate=inspect.isclass)
96
97
98
99
100
101
102
103

    # 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:
104
        if 'File' in name or 'CharmmParameterSet' in name:
105
106
            data['fileclasses'].append(fullname(klass))

107
108
109
110
111
112
113
114
    # gather all extra subclasses in openmm.app
    exclude = ['openmm.app.topology.Topology',
               'openmm.app.topology.Chain',
               'openmm.app.topology.Residue',
               'openmm.app.topology.Atom',
               'openmm.app.modeller.Modeller',
               'openmm.app.forcefield.ForceField',
               'openmm.app.simulation.Simulation']
115
116
117
118
119
120
121
    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
122
123
124
125
126
127
128
129

    return data


def main():
    here = dirname(__file__)
    templateLoader = jinja2.FileSystemLoader(here)
    templateEnv = jinja2.Environment(loader=templateLoader)
130
131
    data = library_template_variables()
    data.update(app_template_variables())
Robert McGibbon's avatar
Sphinx  
Robert McGibbon committed
132

Robert McGibbon's avatar
Robert McGibbon committed
133
    for template_fn in map(basename, glob(join(here, '*.jinja2'))):
Robert McGibbon's avatar
Sphinx  
Robert McGibbon committed
134
135
136
137
138
139
140
141
142
143
144
        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()