stbt control: Use as control recorder with stbt record

A new listener type that integrates `stbt control` with `stbt record`.

Launching `stbt record` as `stbt record --control-recorder=stbt-control`
brings up `stbt control` to record keypresses using the PC keyboard.

The control URI is 'stbt-control:<keymap_file>' where <keymap_file> is
optional and defaults to ~/.config/stbt/control.conf.

stbt.py imports stbt-control to call its main loop, which generates
the bytecode cache 'stbt-controlc'; as stbt-control doesn't have '.py'
extension, it is not '.pyc' but gets the 'c' ending. This file is
cleaned up similarly to '.pyc' files.
stbt control: Wait between keypresses to avoid LIRC failure

If `stbt control` sends keypresses to the LIRC daemon more frequently
than one keypress in 0.2 seconds, LIRC refuses the connection and the
script exits with "Resource temporarily unavailable".

To make it possible to send repeated keypresses by pressing and holding a
button on the keyboard, sleep 0.2 seconds between sending two remote
control keys and flush the buffer of the received keyboard keys.
stbt record: Delete iterator to make exceptions visible

If `stbt record` is used with `--control-recorder=stbt-control`, and an
exception is raised from inside the loop of `stbt-record.record` before
`remote_input` iterator stops, the stack trace is printed before the
the iterator is being deleted. Deleting the iterator implies deleting
`term` object in `stbt-control.main_loop`, which calls the finally
block of `stbt-control.terminal` that clears the screen, therefore the
exception message is wiped out.

If `stbt record` decreases the reference counter of `remote_input` to
zero, the iterator is deleted and the terminal window is reset to its
initial state during clean-up before the exception message is printed.
stbt control: Allow mapping Escape key

In the original release of `stbt control` mapping Escape key was not
supported because of a short delay between Escape character being
received and the remote control key being sent, which makes its use
somewhat inconvenient.

Nevertheless, some users still prefer to use this key in their keymaps,
despite the mentioned inconvenience.
stbt-control: Raise custom exception if the terminal is too small

This is a follow-up to ac484dd0. That correction printed an error
message if the terminal was too small but didn't raise an exception. The
problem with that solution is that if the terminal is even too small to
print the error message then the tool quits with the unhelpful
`curses.error` exception.

Now the tool raises an exception every time the keymap doesn't fit the
terminal window, but with a helpful exception message.
stbt control: Parse command line arguments with `argparse`

`getopt` is swapped to `argparse` module in order to make the tool
consistent with other stbt tools.

As `argparse.ArgumentParser` generates the help message automatically,
the keymap file description can now be accessed with a separate command
line argument.
stbt control: Refactoring: Use curses.ascii constants

Using `curses.ascii` constants instead of `ord(...)` calls makes the code
more readable and also makes `SPECIAL_CHARS` dictionary more consistent.
Self-test for `stbt record --control-recorder=stbt-control`

This tests the functionality added in c554fc8f, which had the nice
side-effect of making the interactive mode of `stbt control` testable at
all!

The selftests now require `expect` (from the package of the same name).
Merge branch 'stbt-control-as-control-recorder'

* stbt-control-as-control-recorder:
  Self-test for `stbt record --control-recorder=stbt-control`
  stbt control: Refactoring: Use curses.ascii constants
  stbt control: Parse command line arguments with `argparse`
  stbt-control: Raise custom exception if the terminal is too small
  stbt control: Allow mapping Escape key
  stbt record: Delete iterator to make exceptions visible
  stbt control: Wait between keypresses to avoid LIRC failure
  stbt control: Use as control recorder with stbt record
Fix spelling in `wait_for_motion` docstring

Thanks to "bruce-one" <bryce@gibson-consulting.com.au> for pointing this
out.
test_record: Check that the screenshots were saved to disk

`stbt record` saves screenshots to disk each time the user presses a
remote control button. Now the unit test actually checks that those
screenshots are being saved to disk.
stbt record: Simplify logic in `record` function

This is mostly a refactoring, though there is one user-visible change:
The numbering of the saved images now starts from 1 instead of 0. This
isn't significant though: it won't affect existing scripts, and new
scripts will work too.

The motivation for this, in addition to making the code clearer and
shorter, is to allow the next commit (which see).

Why do we need two nested `try` statements? We want to take one final
screenshot on `KeyboardInterrupt` (when the user presses Control-C), or
if the `remote_input` iteration ends (when using
`--control-recorder=file:///dev/stdin` and the user presses Control-D).
But we don't want the `except KeyboardInterrupt` block to take another
screenshot if the user pressed Control-C during the
`write_wait_for_match` triggered from `remote_input` iteration ending.
I'll be able to eliminate the outer `try` in the next commit.
stbt record: Avoid explicit `del`

Commit 47c56330 added the line `del remote_input` so that the destructor
of the `remote_input` iterator (i.e. `stbt-control.main_loop`) is run
(including the `curses` cleanup in the `terminal` context manager of
`stbt-control`, which clears the screen) *before* any unhandled
exception backtraces are printed to the terminal.

This current commit avoids the explicit `del` by not holding any
references to the `stbt-control.main_loop` iterator; if an exception is
raised in `record` (e.g. from `stbt.get_frame` or `stbt.press`) then the
`stbt-control.main_loop` iterator is destroyed as soon as the exception
leaves the `record` stack frame. (This of course relies on CPython's
deterministic destruction, but so did the previous solution with the
explicit `del`; `del` only decrements a reference count.)

Specifically, `remote_input` was a local variable inside the `record`
function. The raised exception contains a reference to each frame on the
stack, and the frame for the `record` function contained a reference to
`remote_input`, so `remote_input` wasn't destroyed until the exception
object itself was destroyed.
stbt record: Fail early if no video is available

For example, if the user's `source_pipeline` configuration is incorrect,
or if the system under test is switched off.

Without this, `stbt record` waits for the user to have entered *two*
button-presses, then silently waits 10 seconds (for `get_frame` to raise
`NoVideo`) before exiting. Now it still takes 10 seconds to fail, but
that happens at the beginning; when there *is* video it won't delay for
10 seconds, only as long as it needs to find video. If you're using
`--control_recorder=stbt-control` you won't see the prompt for input
until video is established.

I don't want to reduce the default 10 second timeout because some video
capture devices can take several seconds to start delivering video,
especially if they haven't been "warmed up" by a recent capture.
test_wait_for_match_no_match: Don't test for image path search behaviour

That is covered by the tests
test_wait_for_match_searches_in_script_directory,
test_press_until_match_searches_in_script_directory,
test_detect_match_searches_in_script_directory,
test_detect_match_searches_in_library_directory, and
test_detect_match_searches_in_caller_directory.

There is no need to complicate this test; it only needs to test the
behaviour of `wait_for_match` when it doesn't find a match.

Also check the exit status of `stbt-run` (without the `&&`, the test
would have passed as long as `screenshot.png` is created).
test_wait_for_match_match_method_param_affects_first_pass: Fix false negative

The first `stbt-run`'s exit status was being ignored, which masked an
error in the call to `wait_for_match`. Furthermore, I had to disable the
second pass (`confirm_method="none"`) to get an observable difference
between the two first-pass methods, because even when the first pass
was giving an incorrect match, the second pass was correctly detecting
that it shouldn't match. This test wants to prove that with a different
first-pass method you can get an incorrect "match" verdict.
test_detect_match_reports_match_position: Cleanup: Line length & whitespace
tests: Run each test in a separate "scratch" directory

Many of these tests create files in the current working directory, like
`screenshot.png` or a `stbt-debug` directory. This change stops those
tests from polluting the directory containing the source code checkout.
It also makes many tests slightly shorter, avoiding the explicit `cd
"$scratchdir"`.
make check-pylint: Hide pygobject deprecation messages

The following warning was printed for each file analysed by pylint:

    ** Message: pygobject_register_sinkfunc is deprecated (GstObject)

This deprecated function isn't directly called by any of our code, so
there is nothing we can do about it (except maybe porting to GStreamer
1.0).
test_wait_for_match_erode_passes_affects_match: Cleanup: Remove unneeded "$scratchdir"

This was missed from commit e3563141.