-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcjrh_template.py
38 lines (32 loc) · 1.22 KB
/
cjrh_template.py
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
""" Subclass for string.Template: adds placeholders() method. """
import string
__version__ = '2017.10.18'
class Template(string.Template):
"""
Very thin subclass of ``string.Template``.
"""
def placeholders(self, allow_repeats=False):
"""
Return the names of the template variables in the template. Note that
this returns a generator.
The ``allow_repeats`` argument will cause the iteration to skip any
repeated variable names.
:type allow_repeats: bool
:return: A generator of all the named vars
:rtype: generator
"""
seen = set()
for match in self.pattern.finditer(self.template):
named = match.group('named') or match.group('braced')
if not allow_repeats:
if named in seen:
continue
else:
# Structured like this so that we don't incur the memory
# cost of storing previously-seen items when repeats are
# allowed.
seen.add(named)
if named is None:
# Seems to be necessary for things like "$(blah)".
continue
yield named