"""Resources to tokenize userdata."""importrefromtroposphereimportGetAtt,RefHELPERS={"Ref":Ref,"Fn::GetAtt":GetAtt}SPLIT_STRING="("+"|".join(rf"{h}\([^)]+\)"forhinHELPERS)+")"REPLACE_STRING=rf"(?P<helper>{'|'.join(HELPERS)})\((?P<args>['\"]?[^)]+['\"]?)+\)"SPLIT_RE=re.compile(SPLIT_STRING)REPLACE_RE=re.compile(REPLACE_STRING)
[docs]defcf_tokenize(raw_userdata:str)->list[str]:"""Parse UserData for Cloudformation helper functions. http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/user-data.html http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/intrinsic-function-reference.html 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)forpartinparts:cf_func=REPLACE_RE.search(part)ifcf_func:args=[a.strip("'\" ")foraincf_func.group("args").split(",")]result.append(HELPERS[cf_func.group("helper")](*args).data)else:result.append(part)returnresult