# Path: kano/utils/shell.py
# def run_cmd(cmd, localised=False, unsudo=False):
#     '''
#     Executes cmd, returning stdout, stderr, return code
#     if localised is False, LC_ALL will be set to "C"
#     '''
#     env = os.environ.copy()
#     if not localised:
#         env['LC_ALL'] = 'C'
# 
#     if unsudo and \
#             'SUDO_USER' in os.environ and \
#             os.environ['SUDO_USER'] != 'root':
#         cmd = "sudo -u {} bash -c '{}' ".format(os.environ['SUDO_USER'], cmd)
# 
#     process = subprocess.Popen(cmd, shell=True, env=env,
#                                stdout=subprocess.PIPE, stderr=subprocess.PIPE,
#                                preexec_fn=restore_signals)
# 
#     stdout, stderr = process.communicate()
#     returncode = process.returncode
#     return stdout, stderr, returncode
# 
# def run_bg(cmd, localised=False, unsudo=False):
#     '''
#     Starts cmd program in the background
#     '''
#     env = os.environ.copy()
#     if not localised:
#         env['LC_ALL'] = 'C'
# 
#     if unsudo and \
#             'SUDO_USER' in os.environ and \
#             os.environ['SUDO_USER'] != 'root':
#         cmd = "sudo -u {} bash -c '{}' ".format(os.environ['SUDO_USER'], cmd)
# 
#     s = subprocess.Popen(cmd, shell=True, env=env)
#     return s
# 
# def run_cmd_log(cmd, localised=False, unsudo=False):
#     '''
#     Wrapper against run_cmd but Kano Logging executuion and return code
#     '''
# 
#     from kano.logging import logger
# 
#     out, err, rv = run_cmd(cmd, localised, unsudo)
#     logger.info("Command: {}".format(cmd))
# 
#     if len(out.strip()) > 0:
#         logger.info(out)
# 
#     if len(err.strip()) > 0:
#         logger.error(err)
# 
#     logger.info("Return value: {}".format(rv))
# 
#     return out, err, rv


import os
from kano.utils.shell import run_cmd, run_bg, run_cmd_log
    from kano.logging import logger
            from kano_settings.system.audio import is_HDMI
        from math import log10
    from kano.logging import logger
        if percent == 0:
            return -11000

        min_allowed = -4000
        max_allowed = 400
        percent = percent / 100.
        millibel = min_allowed + (max_allowed - min_allowed) * percent

    return int(millibel)


def get_volume():

    percent = 100

    cmd = "amixer | head -n 6 | grep -Po '(\d{1,3})(?=%)'"  # noqa
    output, _, _ = run_cmd(cmd)

    try:
        percent = int(output.strip())
    except Exception:
        msg = 'amixer format bad for percent, output: {}'.format(output)
        logger.error(msg)
        pass

    return percent


def set_volume(percent):
    cmd = 'amixer set Master {}%'.format(percent)

# Path: kano/utils/shell.py
# def run_cmd(cmd, localised=False, unsudo=False):
#     '''
#     Executes cmd, returning stdout, stderr, return code
#     if localised is False, LC_ALL will be set to "C"
#     '''
#     env = os.environ.copy()
#     if not localised:
#         env['LC_ALL'] = 'C'
# 
#     if unsudo and \
#             'SUDO_USER' in os.environ and \
#             os.environ['SUDO_USER'] != 'root':
#         cmd = "sudo -u {} bash -c '{}' ".format(os.environ['SUDO_USER'], cmd)
# 
#     process = subprocess.Popen(cmd, shell=True, env=env,
#                                stdout=subprocess.PIPE, stderr=subprocess.PIPE,
#                                preexec_fn=restore_signals)
# 
#     stdout, stderr = process.communicate()
#     returncode = process.returncode
#     return stdout, stderr, returncode
# 
# def run_bg(cmd, localised=False, unsudo=False):
#     '''
#     Starts cmd program in the background
#     '''
#     env = os.environ.copy()
#     if not localised:
#         env['LC_ALL'] = 'C'
# 
#     if unsudo and \
#             'SUDO_USER' in os.environ and \
#             os.environ['SUDO_USER'] != 'root':
#         cmd = "sudo -u {} bash -c '{}' ".format(os.environ['SUDO_USER'], cmd)
# 
#     s = subprocess.Popen(cmd, shell=True, env=env)
#     return s
# 
# def run_cmd_log(cmd, localised=False, unsudo=False):
#     '''
#     Wrapper against run_cmd but Kano Logging executuion and return code
#     '''
# 
#     from kano.logging import logger
# 
#     out, err, rv = run_cmd(cmd, localised, unsudo)
#     logger.info("Command: {}".format(cmd))
# 
#     if len(out.strip()) > 0:
#         logger.info(out)
# 
#     if len(err.strip()) > 0:
#         logger.error(err)
# 
#     logger.info("Return value: {}".format(rv))
# 
#     return out, err, rv


# Path: kano/utils/audio.py
import os
from kano.utils.shell import run_cmd, run_bg, run_cmd_log
    from kano.logging import logger
            from kano_settings.system.audio import is_HDMI
        from math import log10
    from kano.logging import logger

        if percent == 0:
            return -11000

        min_allowed = -4000
        max_allowed = 400
        percent = percent / 100.
        millibel = min_allowed + (max_allowed - min_allowed) * percent

    return int(millibel)


def get_volume():

    percent = 100

    cmd = "amixer | head -n 6 | grep -Po '(\d{1,3})(?=%)'"  # noqa
    output, _, _ = run_cmd(cmd)

    try:
        percent = int(output.strip())
    except Exception:
        msg = 'amixer format bad for percent, output: {}'.format(output)
        logger.error(msg)
        pass

    return percent


def set_volume(percent):
    cmd = 'amixer set Master {}%'.format(percent)

    run_cmd(cmd)