def test_memoize_iterator():
    counter = [0]

    @memoize_iterator()
    def cached_function(_arg):
        for x in range(10):
            counter[0] += 1
            yield x

    with named_temporary_directory() as tmpdir, cache(tmpdir):
        uncached = list(itertools.islice(cached_function(1), 5))
        assert uncached == range(5)
        assert counter[0] == 5

        cached = list(itertools.islice(cached_function(1), 5))
        assert cached == range(5)
        assert counter[0] == 5

        partially_cached = list(itertools.islice(cached_function(1), 10))
        assert partially_cached == range(10)
        assert counter[0] == 15

        partially_cached = list(cached_function(1))
        assert partially_cached == range(10)
        assert counter[0] == 25

        cached = list(cached_function(1))
        assert cached == range(10)
        assert counter[0] == 25

        uncached = list(cached_function(2))
        assert uncached == range(10)
        assert counter[0] == 35
Example #2
0
def test_memoize_iterator():
    counter = [0]

    @memoize_iterator()
    def cached_function(_arg):
        for x in range(10):
            counter[0] += 1
            yield x

    with named_temporary_directory() as tmpdir, cache(tmpdir):
        uncached = list(itertools.islice(cached_function(1), 5))
        assert uncached == range(5)
        assert counter[0] == 5

        cached = list(itertools.islice(cached_function(1), 5))
        assert cached == range(5)
        assert counter[0] == 5

        partially_cached = list(itertools.islice(cached_function(1), 10))
        assert partially_cached == range(10)
        assert counter[0] == 15

        partially_cached = list(cached_function(1))
        assert partially_cached == range(10)
        assert counter[0] == 25

        cached = list(cached_function(1))
        assert cached == range(10)
        assert counter[0] == 25

        uncached = list(cached_function(2))
        assert uncached == range(10)
        assert counter[0] == 35
Example #3
0
def test_stbt_control_relay(stbt_control_relay_on_path):  # pylint: disable=unused-argument
    with named_temporary_directory("stbt-control-relay-test.XXXXXX") as tmpdir:

        def t(filename):
            return os.path.join(tmpdir, filename)

        proc = subprocess.Popen([
            "stbt-control-relay", "--socket",
            t("lircd.sock"), "file:" + t("one-file")
        ])
        with scoped_process(proc):
            wait_until(lambda: (os.path.exists(t("lircd.sock")) or proc.poll()
                                is not None))
            testcontrol = uri_to_control("lirc:%s:stbt-test" % t("lircd.sock"))

            testcontrol.press("KEY_LEFT")
            testcontrol.press("KEY_RIGHT")
            testcontrol.keydown("KEY_MENU")
            testcontrol.keyup("KEY_MENU")
            expected = dedent("""\
                KEY_LEFT
                KEY_RIGHT
                Holding KEY_MENU
                Released KEY_MENU
                """)

            assert expected == open(t("one-file")).read()
def test_stbt_control_relay(stbt_control_relay_on_path):  # pylint: disable=unused-argument
    with named_temporary_directory("stbt-control-relay-test.XXXXXX") as tmpdir:
        def t(filename):
            return os.path.join(tmpdir, filename)
        proc = subprocess.Popen(
            ["stbt-control-relay",
             "--input=lircd:" + t("lircd.sock"),
             "file:" + t("one-file"), "file:" + t("another")])
        with scoped_process(proc):
            wait_until(lambda: (
                os.path.exists(t("lircd.sock")) or proc.poll() is not None))
            testremote = uri_to_remote("lirc:%s:stbt" % t("lircd.sock"))

            testremote.press("KEY_UP")
            testremote.press("KEY_DOWN")
            expected = "KEY_UP\nKEY_DOWN\n"

            def filecontains(filename, text):
                try:
                    with open(t(filename)) as f:
                        return text == f.read()
                except OSError:
                    return None

            wait_until(lambda: (
                filecontains("one-file", expected) and
                filecontains("another", expected)))
            assert open(t("one-file")).read() == expected
            assert open(t("another")).read() == expected
def test_stbt_control_relay(stbt_control_relay_on_path):  # pylint: disable=unused-argument
    with named_temporary_directory("stbt-control-relay-test.XXXXXX") as tmpdir:
        def t(filename):
            return os.path.join(tmpdir, filename)
        proc = subprocess.Popen(
            ["stbt-control-relay",
             "--socket", t("lircd.sock"),
             "file:" + t("one-file")])
        with scoped_process(proc):
            wait_until(lambda: (
                os.path.exists(t("lircd.sock")) or proc.poll() is not None))
            testcontrol = uri_to_control("lirc:%s:stbt-test" % t("lircd.sock"))

            testcontrol.press("KEY_LEFT")
            testcontrol.press("KEY_RIGHT")
            testcontrol.keydown("KEY_MENU")
            testcontrol.keyup("KEY_MENU")
            expected = dedent("""\
                KEY_LEFT
                KEY_RIGHT
                Holding KEY_MENU
                Released KEY_MENU
                """)

            assert expected == open(t("one-file")).read()
def installed_stbt_control_relay():
    with named_temporary_directory("stbt-control-relay-install.XXXXXX") as tmp:
        oldprefix = open(srcdir(".stbt-prefix")).read()
        subprocess.check_call(
            ["make", "prefix=%s" % tmp, "install-stbt-control-relay"],
            cwd=srcdir())
        open(srcdir(".stbt-prefix"), 'w').write(oldprefix)

        os.environ['PATH'] = "%s/bin:%s" % (tmp, os.environ['PATH'])
        yield "%s/bin/stbt-control-relay" % tmp
Example #7
0
def temporary_config(config):
    with named_temporary_directory(prefix="stbt-test-ocr") as d:
        original_env = os.environ.get("STBT_CONFIG_FILE", "")
        os.environ["STBT_CONFIG_FILE"] = "%s/stbt.conf:%s" % (d, original_env)
        for key, value in config.items():
            section, option = key.split(".")
            _stbt.config.set_config(section, option, value)
        try:
            yield
        finally:
            os.environ["STBT_CONFIG_FILE"] = original_env
            _stbt.config._config_init(force=True)  # pylint:disable=protected-access
Example #8
0
def temporary_config(config):
    with named_temporary_directory(prefix="stbt-test-ocr") as d:
        original_env = os.environ.get("STBT_CONFIG_FILE", "")
        os.environ["STBT_CONFIG_FILE"] = "%s/stbt.conf:%s" % (d, original_env)
        for key, value in config.items():
            section, option = key.split(".")
            _stbt.config.set_config(section, option, value)
        try:
            yield
        finally:
            os.environ["STBT_CONFIG_FILE"] = original_env
            _stbt.config._config_init(force=True)  # pylint:disable=protected-access
Example #9
0
def temporary_config(contents):
    with named_temporary_directory(prefix="stbt-test-config") as d:
        original_env = os.environ.get("STBT_CONFIG_FILE", "")
        filename = os.path.join(d, "stbt.conf")
        os.environ["STBT_CONFIG_FILE"] = ":".join([filename, original_env])
        with open(filename, "w") as f:
            f.write(dedent(contents))
        _config_init(force=True)
        try:
            yield
        finally:
            os.environ["STBT_CONFIG_FILE"] = original_env
            _config_init(force=True)
Example #10
0
def temporary_config(contents):
    with named_temporary_directory(prefix="stbt-test-config") as d:
        original_env = os.environ.get("STBT_CONFIG_FILE", "")
        filename = os.path.join(d, "stbt.conf")
        os.environ["STBT_CONFIG_FILE"] = ":".join([filename, original_env])
        with open(filename, "w") as f:
            f.write(dedent(contents))
        _config_init(force=True)
        try:
            yield
        finally:
            os.environ["STBT_CONFIG_FILE"] = original_env
            _config_init(force=True)
Example #11
0
def lircd():
    with named_temporary_directory("stbt-lirc-test") as tmpdir:
        socket = os.path.join(tmpdir, "lircd.socket")
        logfile = os.path.join(tmpdir, "lircd.log")
        proc = subprocess.Popen(
            ["lircd", "--nodaemon", "--loglevel=info", "--logfile=/dev/stderr",
             "--driver=file", "--device", logfile,  # lircd output
             "--output", socket,  # lircd reads instructions from here
             "--pidfile=%s/lircd.pid" % tmpdir,
             _find_file("Apple_TV.lircd.conf")])
        wait_until(lambda: (
            os.path.exists(socket) or proc.poll() is not None))

        with scoped_process(proc):
            yield namedtuple("Lircd", "socket logfile")(socket, logfile)
Example #12
0
def installed_stbt_control_relay():
    with named_temporary_directory("stbt-control-relay-install.XXXXXX") as tmp:
        try:
            oldprefix = open(srcdir(".stbt-prefix")).read()
        except IOError:
            oldprefix = None
        subprocess.check_call(
            ["make", "prefix=%s" % tmp, "install-stbt-control-relay"],
            cwd=srcdir())
        if oldprefix is not None:
            open(srcdir(".stbt-prefix"), 'w').write(oldprefix)
        else:
            os.unlink(srcdir(".stbt-prefix"))

        os.environ['PATH'] = "%s/bin:%s" % (tmp, os.environ['PATH'])
        yield "%s/bin/stbt-control-relay" % tmp
def installed_stbt_control_relay():
    with named_temporary_directory("stbt-control-relay-install.XXXXXX") as tmp:
        try:
            oldprefix = open(srcdir(".stbt-prefix")).read()
        except IOError:
            oldprefix = None
        subprocess.check_call(
            ["make", "prefix=%s" % tmp, "install-stbt-control-relay"],
            cwd=srcdir())
        if oldprefix is not None:
            open(srcdir(".stbt-prefix"), 'w').write(oldprefix)
        else:
            os.unlink(srcdir(".stbt-prefix"))

        os.environ['PATH'] = "%s/bin:%s" % (tmp, os.environ['PATH'])
        yield "%s/bin/stbt-control-relay" % tmp
Example #14
0
def test_memoize_iterator_on_empty_iterator():
    counter = [0]

    @memoize_iterator()
    def cached_function():
        counter[0] += 1
        if False:  # pylint:disable=using-constant-test
            yield

    with named_temporary_directory() as tmpdir, cache(tmpdir):
        uncached = list(cached_function())
        assert uncached == []
        assert counter[0] == 1

        cached = list(cached_function())
        assert cached == []
        assert counter[0] == 1
Example #15
0
def _check_cache_behaviour(func):
    from timeit import Timer

    timer = Timer(func)
    uncached_result = func()
    uncached_time = min(timer.repeat(10, number=1))

    with named_temporary_directory() as tmpdir, cache(tmpdir):
        # Prime the cache
        func()
        cached_time = min(timer.repeat(10, number=1))
        cached_result = func()

    print "%s with cache: %s" % (func.__name__, cached_time)
    print "%s without cache: %s" % (func.__name__, uncached_time)

    return cached_time, uncached_time, cached_result, uncached_result
def test_memoize_iterator_on_empty_iterator():
    counter = [0]

    @memoize_iterator()
    def cached_function():
        counter[0] += 1
        if False:
            yield

    with named_temporary_directory() as tmpdir, cache(tmpdir):
        uncached = list(cached_function())
        assert uncached == []
        assert counter[0] == 1

        cached = list(cached_function())
        assert cached == []
        assert counter[0] == 1
def _check_cache_behaviour(func):
    from timeit import Timer

    timer = Timer(func)
    uncached_result = func()
    uncached_time = timer.timeit(number=5) / 5.

    with named_temporary_directory() as tmpdir, cache(tmpdir):
        # Prime the cache
        func()
        cached_time = timer.timeit(number=5) / 5.
        cached_result = func()

    print "%s with cache: %s" % (func.__name__, cached_time)
    print "%s without cache: %s" % (func.__name__, uncached_time)

    return cached_time, uncached_time, cached_result, uncached_result
def test_stbt_control_relay(stbt_control_relay_on_path):  # pylint: disable=unused-argument
    with named_temporary_directory("stbt-control-relay-test.XXXXXX") as tmpdir:
        def t(filename):
            return os.path.join(tmpdir, filename)
        proc = subprocess.Popen(
            ["stbt-control-relay",
             "--socket", t("lircd.sock"),
             "file:" + t("one-file")])
        with scoped_process(proc):
            wait_until(lambda: (
                os.path.exists(t("lircd.sock")) or proc.poll() is not None))
            testremote = uri_to_remote("lirc:%s:stbt" % t("lircd.sock"))

            testremote.press("KEY_UP")
            testremote.press("KEY_DOWN")
            expected = "KEY_UP\nKEY_DOWN\n"

            assert open(t("one-file")).read() == expected
Example #19
0
def test_that_cache_speeds_up_ocr():
    with named_temporary_directory() as tmpdir, \
            imgproc_cache.setup_cache(tmpdir):
        _test_that_cache_speeds_up_ocr()