def _escape_template_string(template):
    # type: (str) -> str
    """Escape the '$' in template strings unless followed by '{'."""
    # See https://docs.python.org/2/library/string.html#template-strings
    template = template.replace('${', '#{')
    template = template.replace('$', '$$')
    return template.replace('#{', '${')