# Path: kano_settings/config_file.py
# def get_setting(variable):
# 
#     try:
#         value = read_json(settings_file)[variable]
#     except Exception:
#         if variable not in DEFAULTS:
#             logger.info("Defaults not found for variable: {}".format(variable))
#         value = DEFAULTS[variable]
#     return value
# 
# def set_setting(variable, value):
# 
#     if username == 'root':
#         return
# 
#     logger.debug(u"config_file / set_setting: {} {}".format(variable, value))
# 
#     data = read_json(settings_file)
#     if not data:
#         data = dict()
# 
#     data[variable] = value
#     write_json(settings_file, data)
#     chown_path(settings_file)


from kano.logging import logger
from kano.utils.shell import run_cmd
from kano.utils.file_operations import sed
from kano_settings.config_file import get_setting, set_setting
import kano_settings.system.keyboard_layouts as keyboard_layouts
import kano_settings.system.locale as locale


def is_changed(country_code, variant):
    continent = get_setting('Keyboard-continent-human')
    country = get_setting('Keyboard-country-human')

    stored_variant = get_setting('Keyboard-variant-human').lower()
    stored_layout = keyboard_layouts.layouts[continent][country].lower()

    if variant == 'generic':
        variant = ''

    return (country_code != stored_layout or variant != stored_variant)


def update_settings_keyboard_conf(continent_index, country_index, variant_index,
                                  continent, country, variant):
    logger.info('set_keyboard.update_config {} {} {} {} {} {}'.format(
        continent_index,
        country_index,
        variant_index,
        continent,
        country,
        variant
    ))

    # Add new configurations to config file.
    set_setting("Keyboard-continent-index", continent_index)
    set_setting("Keyboard-country-index", country_index)
    set_setting("Keyboard-variant-index", variant_index)

# Path: kano_settings/config_file.py
# def get_setting(variable):
# 
#     try:
#         value = read_json(settings_file)[variable]
#     except Exception:
#         if variable not in DEFAULTS:
#             logger.info("Defaults not found for variable: {}".format(variable))
#         value = DEFAULTS[variable]
#     return value
# 
# def set_setting(variable, value):
# 
#     if username == 'root':
#         return
# 
#     logger.debug(u"config_file / set_setting: {} {}".format(variable, value))
# 
#     data = read_json(settings_file)
#     if not data:
#         data = dict()
# 
#     data[variable] = value
#     write_json(settings_file, data)
#     chown_path(settings_file)


# Path: kano_settings/system/keyboard_config.py
from kano.logging import logger
from kano.utils.shell import run_cmd
from kano.utils.file_operations import sed
from kano_settings.config_file import get_setting, set_setting
import kano_settings.system.keyboard_layouts as keyboard_layouts
import kano_settings.system.locale as locale



def is_changed(country_code, variant):
    continent = get_setting('Keyboard-continent-human')
    country = get_setting('Keyboard-country-human')

    stored_variant = get_setting('Keyboard-variant-human').lower()
    stored_layout = keyboard_layouts.layouts[continent][country].lower()

    if variant == 'generic':
        variant = ''

    return (country_code != stored_layout or variant != stored_variant)


def update_settings_keyboard_conf(continent_index, country_index, variant_index,
                                  continent, country, variant):
    logger.info('set_keyboard.update_config {} {} {} {} {} {}'.format(
        continent_index,
        country_index,
        variant_index,
        continent,
        country,
        variant
    ))

    # Add new configurations to config file.
    set_setting("Keyboard-continent-index", continent_index)
    set_setting("Keyboard-country-index", country_index)
    set_setting("Keyboard-variant-index", variant_index)

    set_setting("Keyboard-continent-human", continent)