Source code for runway.cfngin.tokenize_userdata

"""Resources to tokenize userdata."""
import re
from typing import List

from troposphere import GetAtt, Ref

HELPERS = {"Ref": Ref, "Fn::GetAtt": GetAtt}

SPLIT_STRING = "(" + "|".join(rf"{h}\([^)]+\)" for h in HELPERS) + ")"
REPLACE_STRING = rf"(?P<helper>{'|'.join(HELPERS)})\((?P<args>['\"]?[^)]+['\"]?)+\)"


[docs]def cf_tokenize(raw_userdata: str) -> List[str]: """Parse UserData for Cloudformation helper functions. It breaks apart the given string at each recognized function (see ``HELPERS`` global variable) and instantiates the helper function objects in place of those. Args: raw_userdata: Unparsed userdata data string. Returns: A list of string parts that is useful when used with :func:`troposphere.Join` and :func:`troposphere.Base64` to produce userdata. Example: .. code-block: python Base64(Join('', cf_tokenize(userdata_string))) """ result: List[str] = [] parts = SPLIT_RE.split(raw_userdata) for part in parts: cf_func = if cf_func: args = [a.strip("'\" ") for a in"args").split(",")] result.append(HELPERS["helper")](*args).data) else: result.append(part) return result