# 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
            for keysym in ['KEY_T', 'KEY_O', 'KEY_U', 'KEY_C', 'KEY_H',
                           'KEY_SPACE',
                           'g', 'o', 'o', 'd',
                           'KEY_OK']:
                r.press(keysym)
            if os.path.exists(tmp + '/good'):
                break
            time.sleep(0.5)
        with open(tmp + '/xterm.log', 'r') as log:
            for line in log:
                print("xterm.log: " + line, end=' ')
        assert os.path.exists(tmp + '/good')


def test_uri_to_control():
    global IRNetBoxControl  # pylint:disable=global-variable-undefined
    orig_IRNetBoxControl = IRNetBoxControl
    try:
        # pylint:disable=redefined-outer-name
        def IRNetBoxControl(hostname, port, output, config):
            return ":".join([hostname, str(port or '10001'), output, config])
        out = uri_to_control("irnetbox:localhost:1234:1:conf")
        assert out == "localhost:1234:1:conf", (
            "Failed to parse uri with irnetbox port. Output was '%s'" % out)
        out = uri_to_control("irnetbox:localhost:1:conf")
        assert out == "localhost:10001:1:conf", (
            "Failed to parse uri without irnetbox port. Output was '%s'" % out)
        try:
            uri_to_control("irnetbox:localhost::1:conf")
            assert False, "Uri with empty field should have raised"

# 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

            for keysym in ['KEY_T', 'KEY_O', 'KEY_U', 'KEY_C', 'KEY_H',
                           'KEY_SPACE',
                           'g', 'o', 'o', 'd',
                           'KEY_OK']:
                r.press(keysym)
            if os.path.exists(tmp + '/good'):
                break
            time.sleep(0.5)
        with open(tmp + '/xterm.log', 'r') as log:
            for line in log:
                print("xterm.log: " + line, end=' ')
        assert os.path.exists(tmp + '/good')


def test_uri_to_control():
    global IRNetBoxControl  # pylint:disable=global-variable-undefined
    orig_IRNetBoxControl = IRNetBoxControl
    try:
        # pylint:disable=redefined-outer-name
        def IRNetBoxControl(hostname, port, output, config):
            return ":".join([hostname, str(port or '10001'), output, config])
        out = uri_to_control("irnetbox:localhost:1234:1:conf")
        assert out == "localhost:1234:1:conf", (
            "Failed to parse uri with irnetbox port. Output was '%s'" % out)
        out = uri_to_control("irnetbox:localhost:1:conf")
        assert out == "localhost:10001:1:conf", (
            "Failed to parse uri without irnetbox port. Output was '%s'" % out)
        try:
            uri_to_control("irnetbox:localhost::1:conf")
            assert False, "Uri with empty field should have raised"

        except ConfigurationError: