import re


def get_pagetext_mkiconf(pagetext):
    """Return the mkiconf vars found on most dashboard pages.

    These variables are largely the same as administered orgs, but could
    be useful elsewhere. Keeping this here is in case I could use this of
    scraping method later. Check the regex below for the expected string.
    The format will look like this:

        Mkiconf.action_name = "new_wired_status";
        Mkiconf.log_errors = false;
        Mkiconf.eng_log_enabled = false;
        Mkiconf.on_mobile_device = false;

    Essentially  Mkiconf.<property> = <JSON>;

    Args:
        pagetext (string): Text of a webpage
    Returns:
        (dict) All available Mkiconf vars.

    """
    mki_lines = re.findall(' Mkiconf[!-:<-~]* =[ -:<-~]*;', pagetext)
    mki_dict = {}
    for line in mki_lines:
        mki_string = \
            re.findall(r'[0-9a-zA-Z_\[\]\"]+\s*=\s[ -:<-~]*;', line)[0]
        # mki_key = <property>, mki_value = <JSON>
        mki_key, mki_value = mki_string.split(' = ', 1)
        if mki_value[-1] == ';':  # remove trailing ;
            mki_value = mki_value[:-1]
        # If the value is double quoted, remove both "s
        if mki_value[0] == '"' and mki_value[-1] == '"':
            mki_value = mki_value[1:-1]
        mki_dict[mki_key] = mki_value

    return mki_dict