Return list of facetting variables

def parse_wrap_facets(facets):
    """
    Return list of facetting variables
    """
    valid_forms = ['~ var1', '~ var1 + var2']
    error_msg = ("Valid formula for 'facet_wrap' look like"
                 " {}".format(valid_forms))

    if isinstance(facets, (list, tuple)):
        return facets

    if not isinstance(facets, str):
        raise PlotnineError(error_msg)

    if '~' in facets:
        variables_pattern = r'(\w+(?:\s*\+\s*\w+)*|\.)'
        pattern = r'\s*~\s*{0}\s*'.format(variables_pattern)
        match = re.match(pattern, facets)
        if not match:
            raise PlotnineError(error_msg)

        facets = [var.strip() for var in match.group(1).split('+')]
    elif re.match(r'\w+', facets):
        # allow plain string as the variable name
        facets = [facets]
    else:
        raise PlotnineError(error_msg)

    return facets