# Path: _stbt/config.py
# class ConfigurationError(Exception):
#     """An error with your stbt configuration file."""
#     pass
# 
# def get_config(section, key, default=NoDefault, type_=str):
#     """Read the value of ``key`` from ``section`` of the test-pack
#     configuration file.
# 
#     For example, if your configuration file looks like this::
# 
#         [test_pack]
#         stbt_version = 30
# 
#         [my_company_name]
#         backend_ip = 192.168.1.23
# 
#     then you can read the value from your test script like this::
# 
#         backend_ip = stbt.get_config("my_company_name", "backend_ip")
# 
#     This searches in the ``.stbt.conf`` file at the root of your test-pack, and
#     in the ``config/test-farm/<hostname>.conf`` file matching the hostname of
#     the stb-tester device where the script is running. Values in the
#     host-specific config file override values in ``.stbt.conf``. See
#     `Configuration files
#     <https://stb-tester.com/manual/advanced-configuration#configuration-files>`__
#     for more details.
# 
#     Test scripts can use ``get_config`` to read tags that you specify at
#     run-time: see `Automatic configuration keys
#     <https://stb-tester.com/manual/advanced-configuration#automatic-configuration-keys>`__.
# 
#     Raises `ConfigurationError` if the specified ``section`` or ``key`` is not
#     found, unless ``default`` is specified (in which case ``default`` is
#     returned).
# 
#     Changed in v32: Allow specifying ``None`` as the default value (previously
#     ``None`` would be treated as if you hadn't specified any default value).
#     """
# 
#     config = _config_init()
# 
#     try:
#         if type_ is bool:
#             return config.getboolean(section, key)
#         elif issubclass(type_, enum.Enum):
#             return _to_enum(type_, config.get(section, key), section, key)
#         else:
#             return type_(config.get(section, key))
#     except configparser.Error as e:
#         if default is NoDefault:
#             raise ConfigurationError(e.message)
#         else:
#             return default
#     except ValueError:
#         raise ConfigurationError("'%s.%s' invalid type (must be %s)" % (
#             section, key, type_.__name__))


import argparse
import collections
import contextlib
import curses
import curses.ascii
import io
import math
import os
import sys
import threading
import time
import _stbt.control
from textwrap import dedent
from _stbt.config import ConfigurationError, get_config


def test_load_keymap_with_duplicated_key():
    try:
        load_keymap(io.StringIO("o OK\no OPEN\n"))
        assert False, "load_keymap should have thrown ConfigurationError"
    except ConfigurationError:
        pass


def validate(keyname):
    if keyname in SPECIAL_CHARS.values():
        return
    try:
        ord(keyname)
    except TypeError:
        raise ValueError("Invalid keyboard key in the keymap file: " + keyname)


def test_validate():
    try:
        validate("Invalid")
        assert False
    except ValueError:
        pass


def default_keymap_file():
    config_dir = os.environ.get(
        'XDG_CONFIG_HOME', '%s/.config' % os.environ['HOME'])

# Path: _stbt/config.py
# class ConfigurationError(Exception):
#     """An error with your stbt configuration file."""
#     pass
# 
# def get_config(section, key, default=NoDefault, type_=str):
#     """Read the value of ``key`` from ``section`` of the test-pack
#     configuration file.
# 
#     For example, if your configuration file looks like this::
# 
#         [test_pack]
#         stbt_version = 30
# 
#         [my_company_name]
#         backend_ip = 192.168.1.23
# 
#     then you can read the value from your test script like this::
# 
#         backend_ip = stbt.get_config("my_company_name", "backend_ip")
# 
#     This searches in the ``.stbt.conf`` file at the root of your test-pack, and
#     in the ``config/test-farm/<hostname>.conf`` file matching the hostname of
#     the stb-tester device where the script is running. Values in the
#     host-specific config file override values in ``.stbt.conf``. See
#     `Configuration files
#     <https://stb-tester.com/manual/advanced-configuration#configuration-files>`__
#     for more details.
# 
#     Test scripts can use ``get_config`` to read tags that you specify at
#     run-time: see `Automatic configuration keys
#     <https://stb-tester.com/manual/advanced-configuration#automatic-configuration-keys>`__.
# 
#     Raises `ConfigurationError` if the specified ``section`` or ``key`` is not
#     found, unless ``default`` is specified (in which case ``default`` is
#     returned).
# 
#     Changed in v32: Allow specifying ``None`` as the default value (previously
#     ``None`` would be treated as if you hadn't specified any default value).
#     """
# 
#     config = _config_init()
# 
#     try:
#         if type_ is bool:
#             return config.getboolean(section, key)
#         elif issubclass(type_, enum.Enum):
#             return _to_enum(type_, config.get(section, key), section, key)
#         else:
#             return type_(config.get(section, key))
#     except configparser.Error as e:
#         if default is NoDefault:
#             raise ConfigurationError(e.message)
#         else:
#             return default
#     except ValueError:
#         raise ConfigurationError("'%s.%s' invalid type (must be %s)" % (
#             section, key, type_.__name__))


# Path: stbt_control.py
import argparse
import collections
import contextlib
import curses
import curses.ascii
import io
import math
import os
import sys
import threading
import time
import _stbt.control
from textwrap import dedent
from _stbt.config import ConfigurationError, get_config



def test_load_keymap_with_duplicated_key():
    try:
        load_keymap(io.StringIO("o OK\no OPEN\n"))
        assert False, "load_keymap should have thrown ConfigurationError"
    except ConfigurationError:
        pass


def validate(keyname):
    if keyname in SPECIAL_CHARS.values():
        return
    try:
        ord(keyname)
    except TypeError:
        raise ValueError("Invalid keyboard key in the keymap file: " + keyname)


def test_validate():
    try:
        validate("Invalid")
        assert False
    except ValueError:
        pass


def default_keymap_file():
    config_dir = os.environ.get(
        'XDG_CONFIG_HOME', '%s/.config' % os.environ['HOME'])

    return get_config("control", "keymap", "") or \