Source code for runway.cfngin.tokenize_userdata

"""Resources to tokenize userdata."""
import re

from troposphere import GetAtt, Ref

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

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


[docs]def cf_tokenize(raw_userdata): """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 (str): Unparsed userdata data string. Returns: List[str]: 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 = [] 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