# 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 load_keymap(keymap_file):
    keymap = collections.OrderedDict()
    for line in keymap_file:
        items = line.split("//")[0].split()
        if len(items) < 2:
            continue
        if items[0] in keymap:
            raise ConfigurationError(
                "Multiple remote control keys assigned to keyboard key '%s' "
                "in the keymap file" % items[0])
        if len(items) == 2:
            keymap[items[0]] = (items[1],) * 2
        else:
            keymap[items[0]] = (items[1], " ".join(items[2:]))
        validate(items[0])
    return keymap


def test_load_keymap():
    keymap = load_keymap(io.StringIO(
        "Backspace  BACK  Go back to previous\n"  # Test display name
        "Enter  OK  //Move forward\n"))  # Test comments
    assert keymap["Backspace"] == ("BACK", "Go back to previous")
    assert keymap["Enter"] == ("OK", "OK")


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"

# 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 load_keymap(keymap_file):
    keymap = collections.OrderedDict()
    for line in keymap_file:
        items = line.split("//")[0].split()
        if len(items) < 2:
            continue
        if items[0] in keymap:
            raise ConfigurationError(
                "Multiple remote control keys assigned to keyboard key '%s' "
                "in the keymap file" % items[0])
        if len(items) == 2:
            keymap[items[0]] = (items[1],) * 2
        else:
            keymap[items[0]] = (items[1], " ".join(items[2:]))
        validate(items[0])
    return keymap


def test_load_keymap():
    keymap = load_keymap(io.StringIO(
        "Backspace  BACK  Go back to previous\n"  # Test display name
        "Enter  OK  //Move forward\n"))  # Test comments
    assert keymap["Backspace"] == ("BACK", "Go back to previous")
    assert keymap["Enter"] == ("OK", "OK")


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: