Skip to content

[gyb] add the moral equivalent of an #include directive #8265

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 22, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 36 additions & 3 deletions utils/gyb.py
Original file line number Diff line number Diff line change
Expand Up @@ -550,11 +550,13 @@ def next_token(self):
self.token_kind = None


_default_line_directive = '// ###sourceLocation'

class ExecutionContext(object):

"""State we pass around during execution of a template"""

def __init__(self, line_directive='// ###sourceLocation',
def __init__(self, line_directive=_default_line_directive,
**local_bindings):
self.local_bindings = local_bindings
self.line_directive = line_directive
Expand Down Expand Up @@ -744,6 +746,36 @@ def __str__(self, indent=''):
) + '\n' + indent + '}'
return s + self.format_children(indent)

def expand(filename, line_directive=_default_line_directive, **local_bindings):
r"""Return the contents of the givepn template file, executed with the given
local bindings.

>>> from tempfile import NamedTemporaryFile
>>> f = NamedTemporaryFile()
>>> f.write(
... '''---
... % for i in range(int(x)):
... a pox on ${i} for epoxy
... % end
... ''')
>>> f.flush()
>>> result = expand(
... f.name, line_directive='//#sourceLocation', x=2
... ).replace(
... '"%s"' % f.name, '"dummy.file"')
>>> print(result, end='')
//#sourceLocation(file: "dummy.file", line: 1)
---
//#sourceLocation(file: "dummy.file", line: 3)
a pox on 0 for epoxy
//#sourceLocation(file: "dummy.file", line: 3)
a pox on 1 for epoxy

"""
with open(filename) as f:
t = parse_template(filename, f.read())
return execute_template(
t, line_directive=line_directive, **local_bindings)

def parse_template(filename, text=None):
r"""Return an AST corresponding to the given template file.
Expand Down Expand Up @@ -994,7 +1026,8 @@ def parse_template(filename, text=None):
return Block(ParseContext(filename, text))


def execute_template(ast, line_directive='', **local_bindings):
def execute_template(
ast, line_directive=_default_line_directive, **local_bindings):
r"""Return the text generated by executing the given template AST.

Keyword arguments become local variable bindings in the execution context
Expand Down Expand Up @@ -1139,7 +1172,7 @@ def succ(a):
if args.test or args.verbose_test:
import doctest
selfmod = sys.modules[__name__]
if doctest.testmod(selfmod, verbose=args.verbose_test).failed:
if doctest.testmod(selfmod, verbose=args.verbose_test or None).failed:
sys.exit(1)

bindings = dict(x.split('=', 1) for x in args.defines)
Expand Down