# 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/boot_config.py
# def set_config_value(self, name, value=None, config_filter=Filter.ALL):
#     self.set_state_writable()
#     self.temp_config.set_value(name, value, config_filter)
# 
# def end_config_transaction():
#     _trans().close()
# 
# Path: kano_settings/paths.py
# ASOUND_CONF_PATH = join(ASOUND_CONF_BASE_PATH, ASOUND_CONF_NAME)


from kano.utils.shell import run_cmd, run_bg
from kano.logging import logger
from kano_peripherals.wrappers.detection import is_pi_hat_plugged
from kano_settings.config_file import get_setting, set_setting
from kano_settings.boot_config import set_config_value, end_config_transaction
from kano_settings.paths import ASOUND_CONF_PATH
        from kano_settings.system.display import get_edid
    through the jack. This is the only instance we need to prevent jack audio.

    Returns:
        True if audio can be set to play through jack, False otherwise
    """
    return (not is_pi_hat_plugged(retry_count=0))


def set_to_HDMI(HDMI, force=False):
    '''
    Set audio output to HDMI if supported by the display,
    otherwise set it to Analogue output.

    Returns 'HDMI' or 'Analogue', whichever was applied.
    '''

    if not is_hdmi_audio_supported() and not force:
        HDMI = False

    # 1 analog
    # 2 hdmi

    # These are the changes we'll apply if they have changed from what they were
    if HDMI:
        amixer_cmd = hdmi_cmd
        set_config_value('hdmi_ignore_edid_audio', None)
        set_config_value('hdmi_drive', 2)
        config = _("HDMI")
    else:
        amixer_cmd = analogue_cmd

# 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/boot_config.py
# def set_config_value(self, name, value=None, config_filter=Filter.ALL):
#     self.set_state_writable()
#     self.temp_config.set_value(name, value, config_filter)
# 
# def end_config_transaction():
#     _trans().close()
# 
# Path: kano_settings/paths.py
# ASOUND_CONF_PATH = join(ASOUND_CONF_BASE_PATH, ASOUND_CONF_NAME)


# Path: kano_settings/system/audio.py
from kano.utils.shell import run_cmd, run_bg
from kano.logging import logger
from kano_peripherals.wrappers.detection import is_pi_hat_plugged
from kano_settings.config_file import get_setting, set_setting
from kano_settings.boot_config import set_config_value, end_config_transaction
from kano_settings.paths import ASOUND_CONF_PATH
        from kano_settings.system.display import get_edid

    through the jack. This is the only instance we need to prevent jack audio.

    Returns:
        True if audio can be set to play through jack, False otherwise
    """
    return (not is_pi_hat_plugged(retry_count=0))


def set_to_HDMI(HDMI, force=False):
    '''
    Set audio output to HDMI if supported by the display,
    otherwise set it to Analogue output.

    Returns 'HDMI' or 'Analogue', whichever was applied.
    '''

    if not is_hdmi_audio_supported() and not force:
        HDMI = False

    # 1 analog
    # 2 hdmi

    # These are the changes we'll apply if they have changed from what they were
    if HDMI:
        amixer_cmd = hdmi_cmd
        set_config_value('hdmi_ignore_edid_audio', None)
        set_config_value('hdmi_drive', 2)
        config = _("HDMI")
    else:
        amixer_cmd = analogue_cmd

        set_config_value('hdmi_ignore_edid_audio', 1)