aboutsummaryrefslogtreecommitdiff
path: root/catapult/telemetry/telemetry/util/js_template.py
blob: 61f5d5f9b1d5d56e013d753e78db13fc78a407c0 (plain)
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
# Copyright 2016 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

import json
import re


RE_REPLACEMENT_FIELD = re.compile(r'{{(?P<field_spec>[^}]*)}}')
RE_FIELD_IDENTIFIER = re.compile(r'(?P<modifier>@)?(?P<name>\w+)$')


def RenderValue(value):
  """Convert a Python value to a string with its JavaScript representation."""
  return json.dumps(value, sort_keys=True)


def Render(template, **kwargs):
  """Helper method to interpolate Python values into JavaScript snippets.

  Placeholders in the template, field names enclosed in double curly braces,
  are replaced with the value of the corresponding named argument. Prefixing
  a field name with '@' causes the value to be inserted literally.


  For example:

    js_template.Render(
      'var {{ @var_name }} = f({{ x }}, {{ y }});',
      var_name='foo', x=42, y='hello')

  Returns:

    'var foo = f(42, "hello");'

  Args:
    template: A string with a JavaScript template, tagged with {{ fields }}
      to interpolate with values.
    **kwargs: Values to be interpolated in the template.
  """
  def interpolate(m):
    field_spec = m.group('field_spec').strip()
    field = RE_FIELD_IDENTIFIER.match(field_spec)
    if not field:
      raise KeyError(field_spec)
    value = kwargs[field.group('name')]
    if field.group('modifier') == '@':
      if not isinstance(value, str):
        raise ValueError('Literal value for %s must be a string' % field_spec)
      return value
    else:
      return RenderValue(value)

  return RE_REPLACEMENT_FIELD.sub(interpolate, template)