def string_to_dict(var_string, allow_kv=True, require_dict=True):
<EOL>try:<EOL><INDENT>return_dict = yaml.load(var_string, Loader=yaml.SafeLoader)<EOL>if require_dict:<EOL><INDENT>assert type(return_dict) is dict<EOL><DEDENT><DEDENT>except (AttributeError, yaml.YAMLError, AssertionError):<EOL><INDENT>try:<EOL><INDENT>assert allow_kv<EOL>return_dict = parse_kv(var_string)<EOL><DEDENT>except Exception:<EOL><INDENT>raise exc.TowerCLIError(<EOL>'<STR_LIT>'<EOL>'<STR_LIT>' % var_string<EOL>)<EOL><DEDENT><DEDENT>return return_dict<EOL>
Returns a dictionary given a string with yaml or json syntax.
    If data is not present in a key: value format, then it return
    an empty dictionary.

    Attempts processing string by 3 different methods in order:
        1. as JSON      2. as YAML      3. as custom key=value syntax
    Throws an error if all of these fail in the standard ways.