0,170,0,170,0,170,0,170,85,255,85,255,85,255,85,255 !ˆl–äY>”TÔD¨ ÅõÆEp/)‹FÿË 0Ê¿È#ˆl–Ðz¾”ji?u4 \¸î*bÑ¿Ì ƒu÷ÁËÀÉ6# Copyright (C) 2012 Canonical Ltd. # Copyright (C) 2012 Hewlett-Packard Development Company, L.P. # Copyright (C) 2012 Yahoo! Inc. # Copyright (C) 2016 Amazon.com, Inc. or its affiliates. # # Author: Scott Moser # Author: Juerg Haefliger # Author: Joshua Harlow # Author: Andrew Jorgensen # # This file is part of cloud-init. See LICENSE file for license information. # noqa: E402 import collections import logging import re import sys from typing import Any from jinja2 import TemplateSyntaxError from cloudinit import performance from cloudinit import type_utils as tu from cloudinit import util from cloudinit.atomic_helper import write_file # After bionic EOL, mypy==1.0.0 will be able to type-analyse dynamic # base types, substitute this by: # JUndefined: typing.Type JUndefined: Any try: from jinja2 import DebugUndefined as _DebugUndefined from jinja2 import Template as JTemplate JINJA_AVAILABLE = True JUndefined = _DebugUndefined except (ImportError, AttributeError): JINJA_AVAILABLE = False JUndefined = object LOG = logging.getLogger(__name__) TYPE_MATCHER = re.compile(r"##\s*template:(.*)", re.I) BASIC_MATCHER = re.compile(r"\$\{([A-Za-z0-9_.]+)\}|\$([A-Za-z0-9_.]+)") MISSING_JINJA_PREFIX = "CI_MISSING_JINJA_VAR/" class JinjaSyntaxParsingException(TemplateSyntaxError): def __init__( self, error: TemplateSyntaxError, ) -> None: super().__init__( error.message or "unknown syntax error", error.lineno, error.name, error.filename, ) self.source = error.source def __str__(self): """Avoid jinja2.TemplateSyntaxError multi-line __str__ format.""" return self.format_error_message( syntax_error=self.message, line_number=self.lineno, line_content=self.source.splitlines()[self.lineno - 2].strip(), ) @staticmethod def format_error_message( syntax_error: str, line_number: str, line_content: str = "", ) -> str: """Avoid jinja2.TemplateSyntaxError multi-line __str__ format.""" line_content = f": {line_content}" if line_content else "" return JinjaSyntaxParsingException.message_template.format( syntax_error=syntax_error, line_number=line_number, line_content=line_content, ) message_template = ( "Unable to parse Jinja template due to syntax error: " "{syntax_error} on line {line_number}{line_content}" ) # Mypy, and the PEP 484 ecosystem in general, does not support creating # classes with dynamic base types: https://stackoverflow.com/a/59636248 class UndefinedJinjaVariable(JUndefined): """Class used to represent any undefined jinja template variable.""" def __str__(self): return "%s%s" % (MISSING_JINJA_PREFIX, self._undefined_name) def __sub__(self, other): other = str(other).replace(MISSING_JINJA_PREFIX, "") raise TypeError( 'Undefined jinja variable: "{this}-{other}". Jinja tried' ' subtraction. Perhaps you meant "{this}_{other}"?'.format( this=self._undefined_name, other=other ) ) @performance.timed("Rendering basic template") def basic_render(content, params): """This does simple replacement of bash variable like templates. It identifies patterns like ${a} or $a and can also identify patterns like ${a.b} or $a.b which will look for a key 'b' in the dictionary rooted by key 'a'. """ def replacer(match): # Only 1 of the 2 groups will actually have a valid entry. name = match.group(1) if name is None: name = match.group(2) if name is None: raise RuntimeError("Match encountered but no valid group pre