File: templates.py

package info (click to toggle)
brian 2.9.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,872 kB
  • sloc: python: 51,820; cpp: 2,033; makefile: 108; sh: 72
file content (291 lines) | stat: -rw-r--r-- 9,766 bytes parent folder | download | duplicates (2)
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
"""
Handles loading templates from a directory.
"""

import re
from collections.abc import Mapping

from jinja2 import (
    ChoiceLoader,
    Environment,
    PackageLoader,
    StrictUndefined,
    TemplateNotFound,
)

from brian2.utils.stringtools import get_identifiers, indent, strip_empty_lines

__all__ = ["Templater"]

AUTOINDENT_START = "%%START_AUTOINDENT%%"
AUTOINDENT_END = "%%END_AUTOINDENT%%"


def autoindent(code):
    if isinstance(code, list):
        code = "\n".join(code)
    if not code.startswith("\n"):
        code = f"\n{code}"
    if not code.endswith("\n"):
        code = f"{code}\n"
    return AUTOINDENT_START + code + AUTOINDENT_END


def autoindent_postfilter(code):
    lines = code.split("\n")
    outlines = []
    addspaces = 0
    for line in lines:
        if AUTOINDENT_START in line:
            if addspaces > 0:
                raise SyntaxError("Cannot nest autoindents")
            addspaces = line.find(AUTOINDENT_START)
            line = line.replace(AUTOINDENT_START, "")
        if AUTOINDENT_END in line:
            line = line.replace(AUTOINDENT_END, "")
            addspaces = 0
        outlines.append(" " * addspaces + line)
    return "\n".join(outlines)


def variables_to_array_names(variables, access_data=True):
    from brian2.devices.device import get_device

    device = get_device()
    names = [device.get_array_name(var, access_data=access_data) for var in variables]
    return names


class LazyTemplateLoader:
    """
    Helper object to load templates only when they are needed.
    """

    def __init__(self, environment, extension):
        self.env = environment
        self.extension = extension
        self._templates = {}

    def get_template(self, name):
        if name not in self._templates:
            try:
                template = CodeObjectTemplate(
                    self.env.get_template(name + self.extension),
                    self.env.loader.get_source(self.env, name + self.extension)[0],
                )
            except TemplateNotFound:
                try:
                    # Try without extension as well (e.g. for makefiles)
                    template = CodeObjectTemplate(
                        self.env.get_template(name),
                        self.env.loader.get_source(self.env, name)[0],
                    )
                except TemplateNotFound:
                    raise KeyError(f'No template with name "{name}" found.')
            self._templates[name] = template
        return self._templates[name]


class Templater:
    """
    Class to load and return all the templates a `CodeObject` defines.

    Parameters
    ----------

    package_name : str, tuple of str
        The package where the templates are saved. If this is a tuple then each template will be searched in order
        starting from the first package in the tuple until the template is found. This allows for derived templates
        to be used. See also `~Templater.derive`.
    extension : str
        The file extension (e.g. ``.pyx``) used for the templates.
    env_globals : dict (optional)
        A dictionary of global values accessible by the templates. Can be used for providing utility functions.
        In all cases, the filter 'autoindent' is available (see existing templates for example usage).
    templates_dir : str, tuple of str, optional
        The name of the directory containing the templates. Defaults to ``'templates'``.

    Notes
    -----
    Templates are accessed using ``templater.template_base_name`` (the base name is without the file extension).
    This returns a `CodeObjectTemplate`.
    """

    def __init__(
        self, package_name, extension, env_globals=None, templates_dir="templates"
    ):
        if isinstance(package_name, str):
            package_name = (package_name,)
        if isinstance(templates_dir, str):
            templates_dir = (templates_dir,)
        loader = ChoiceLoader(
            [
                PackageLoader(name, t_dir)
                for name, t_dir in zip(package_name, templates_dir)
            ]
        )
        self.env = Environment(
            loader=loader,
            trim_blocks=True,
            lstrip_blocks=True,
            undefined=StrictUndefined,
        )
        self.env.globals["autoindent"] = autoindent
        self.env.filters["autoindent"] = autoindent
        self.env.filters["variables_to_array_names"] = variables_to_array_names
        if env_globals is not None:
            self.env.globals.update(env_globals)
        else:
            env_globals = {}
        self.env_globals = env_globals
        self.package_names = package_name
        self.templates_dir = templates_dir
        self.extension = extension
        self.templates = LazyTemplateLoader(self.env, extension)

    def __getattr__(self, item):
        return self.templates.get_template(item)

    def derive(
        self, package_name, extension=None, env_globals=None, templates_dir="templates"
    ):
        """
        Return a new Templater derived from this one, where the new package name and globals overwrite the old.
        """
        if extension is None:
            extension = self.extension
        if isinstance(package_name, str):
            package_name = (package_name,)
        if env_globals is None:
            env_globals = {}
        if isinstance(templates_dir, str):
            templates_dir = (templates_dir,)
        package_name = package_name + self.package_names
        templates_dir = templates_dir + self.templates_dir
        new_env_globals = self.env_globals.copy()
        new_env_globals.update(**env_globals)
        return Templater(
            package_name,
            extension=extension,
            env_globals=new_env_globals,
            templates_dir=templates_dir,
        )


class CodeObjectTemplate:
    """
    Single template object returned by `Templater` and used for final code generation

    Should not be instantiated by the user, but only directly by `Templater`.

    Notes
    -----

    The final code is obtained from this by calling the template (see `~CodeObjectTemplater.__call__`).
    """

    def __init__(self, template, template_source):
        self.template = template
        self.template_source = template_source
        #: The set of variables in this template
        self.variables = set()
        #: The indices over which the template iterates completely
        self.iterate_all = set()
        #: Read-only variables that are changed by this template
        self.writes_read_only = set()
        # This is the bit inside {} for USES_VARIABLES { list of words }
        specifier_blocks = re.findall(
            r"\bUSES_VARIABLES\b\s*\{(.*?)\}", template_source, re.M | re.S
        )
        # Same for ITERATE_ALL
        iterate_all_blocks = re.findall(
            r"\bITERATE_ALL\b\s*\{(.*?)\}", template_source, re.M | re.S
        )
        # And for WRITES_TO_READ_ONLY_VARIABLES
        writes_read_only_blocks = re.findall(
            r"\bWRITES_TO_READ_ONLY_VARIABLES\b\s*\{(.*?)\}",
            template_source,
            re.M | re.S,
        )
        #: Does this template allow writing to scalar variables?
        self.allows_scalar_write = "ALLOWS_SCALAR_WRITE" in template_source

        for block in specifier_blocks:
            self.variables.update(get_identifiers(block))
        for block in iterate_all_blocks:
            self.iterate_all.update(get_identifiers(block))
        for block in writes_read_only_blocks:
            self.writes_read_only.update(get_identifiers(block))

    def __call__(self, scalar_code, vector_code, **kwds):
        """
        Return a usable code block or blocks from this template.

        Parameters
        ----------
        scalar_code : dict
            Dictionary of scalar code blocks.
        vector_code : dict
            Dictionary of vector code blocks
        **kwds
            Additional parameters to pass to the template

        Notes
        -----

        Returns either a string (if macros were not used in the template), or a `MultiTemplate` (if macros were used).
        """
        if (
            scalar_code is not None
            and len(scalar_code) == 1
            and list(scalar_code)[0] is None
        ):
            scalar_code = scalar_code[None]
        if (
            vector_code is not None
            and len(vector_code) == 1
            and list(vector_code)[0] is None
        ):
            vector_code = vector_code[None]
        kwds["scalar_code"] = scalar_code
        kwds["vector_code"] = vector_code
        module = self.template.make_module(kwds)
        if len([k for k in module.__dict__ if not k.startswith("_")]):
            return MultiTemplate(module)
        else:
            return autoindent_postfilter(str(module))


class MultiTemplate(Mapping):
    """
    Code generated by a `CodeObjectTemplate` with multiple blocks

    Each block is a string stored as an attribute with the block name. The
    object can also be accessed as a dictionary.
    """

    def __init__(self, module):
        self._templates = {}
        for k, f in module.__dict__.items():
            if not k.startswith("_"):
                s = autoindent_postfilter(str(f()))
                setattr(self, k, s)
                self._templates[k] = s

    def __getitem__(self, item):
        return self._templates[item]

    def __iter__(self):
        return iter(self._templates)

    def __len__(self):
        return len(self._templates)

    def __str__(self):
        s = ""
        for k, v in list(self._templates.items()):
            s += f"{k}:\n"
            s += f"{strip_empty_lines(indent(v))}\n"
        return s

    __repr__ = __str__