# Path: _stbt/config.py
# class ConfigurationError(Exception):
#     """An error with your stbt configuration file."""
#     pass
# 
# Path: _stbt/logging.py
# def debug(msg):
#     """Print the given string to stderr if stbt run `--verbose` was given."""
#     if get_debug_level() > 0:
#         sys.stderr.write("%s\n" % (msg,))
# 
# Path: _stbt/utils.py
# @contextmanager
# def named_temporary_directory(
#         suffix='', prefix='tmp', dir=None):  # pylint:disable=redefined-builtin
#     dirname = tempfile.mkdtemp(suffix, prefix, dir)
#     try:
#         yield dirname
#     finally:
#         rmtree(dirname)
# 
# def to_bytes(text):
#     if isinstance(text, str):
#         return text.encode("utf-8", errors="backslashreplace")
#     elif isinstance(text, bytes):
#         return text
#     else:
#         raise TypeError("Unexpected type %s" % type(text))
# 
# def to_unicode(text):
#     if isinstance(text, bytes):
#         return text.decode("utf-8", errors="replace")
#     else:
#         return str(text)


import os
import re
import socket
import struct
import subprocess
import sys
import time
import requests
    import pytest
    import responses
    import pytest
    import responses
from distutils.spawn import find_executable
from . import irnetbox
from .config import ConfigurationError
from .logging import debug
from .utils import named_temporary_directory, to_bytes, to_unicode
    from .control_gpl import controls as gpl_controls
    from _stbt.android import AdbDevice
        from base64 import b64encode
    from unittest import SkipTest
    from .x11 import x_server

class LircControl(RemoteControl):
    """Send a key-press via a LIRC-enabled infrared blaster.

    See http://www.lirc.org/html/technical.html#applications
    """

    def __init__(self, control_name, connect_fn):
        self.control_name = control_name
        self._connect = connect_fn

    def press(self, key):
        s = self._connect()
        command = b"SEND_ONCE %s %s" % (to_bytes(self.control_name),
                                        to_bytes(key))
        s.sendall(command + b"\n")
        _read_lircd_reply(s, command)
        debug("Pressed %s" % key)

    def keydown(self, key):
        s = self._connect()
        command = b"SEND_START %s %s" % (to_bytes(self.control_name),
                                         to_bytes(key))
        s.sendall(command + b"\n")
        _read_lircd_reply(s, command)
        debug("Holding %s" % key)

    def keyup(self, key):
        s = self._connect()
        command = b"SEND_STOP %s %s" % (to_bytes(self.control_name),

# Path: _stbt/config.py
# class ConfigurationError(Exception):
#     """An error with your stbt configuration file."""
#     pass
# 
# Path: _stbt/logging.py
# def debug(msg):
#     """Print the given string to stderr if stbt run `--verbose` was given."""
#     if get_debug_level() > 0:
#         sys.stderr.write("%s\n" % (msg,))
# 
# Path: _stbt/utils.py
# @contextmanager
# def named_temporary_directory(
#         suffix='', prefix='tmp', dir=None):  # pylint:disable=redefined-builtin
#     dirname = tempfile.mkdtemp(suffix, prefix, dir)
#     try:
#         yield dirname
#     finally:
#         rmtree(dirname)
# 
# def to_bytes(text):
#     if isinstance(text, str):
#         return text.encode("utf-8", errors="backslashreplace")
#     elif isinstance(text, bytes):
#         return text
#     else:
#         raise TypeError("Unexpected type %s" % type(text))
# 
# def to_unicode(text):
#     if isinstance(text, bytes):
#         return text.decode("utf-8", errors="replace")
#     else:
#         return str(text)


# Path: _stbt/control.py
import os
import re
import socket
import struct
import subprocess
import sys
import time
import requests
    import pytest
    import responses
    import pytest
    import responses
from distutils.spawn import find_executable
from . import irnetbox
from .config import ConfigurationError
from .logging import debug
from .utils import named_temporary_directory, to_bytes, to_unicode
    from .control_gpl import controls as gpl_controls
    from _stbt.android import AdbDevice
        from base64 import b64encode
    from unittest import SkipTest
    from .x11 import x_server


class LircControl(RemoteControl):
    """Send a key-press via a LIRC-enabled infrared blaster.

    See http://www.lirc.org/html/technical.html#applications
    """

    def __init__(self, control_name, connect_fn):
        self.control_name = control_name
        self._connect = connect_fn

    def press(self, key):
        s = self._connect()
        command = b"SEND_ONCE %s %s" % (to_bytes(self.control_name),
                                        to_bytes(key))
        s.sendall(command + b"\n")
        _read_lircd_reply(s, command)
        debug("Pressed %s" % key)

    def keydown(self, key):
        s = self._connect()
        command = b"SEND_START %s %s" % (to_bytes(self.control_name),
                                         to_bytes(key))
        s.sendall(command + b"\n")
        _read_lircd_reply(s, command)
        debug("Holding %s" % key)

    def keyup(self, key):
        s = self._connect()
        command = b"SEND_STOP %s %s" % (to_bytes(self.control_name),

                                        to_bytes(key))