core: Do less work when traversing stack to find template
core: Do less work when traversing stack to find template

When looking for a template image we work our way up the stack looking for that
image relative to the modules in the stack.  This allows users to name template
images by filename relative to the file that the template image name appears in.

Previously we used `inspect.stack` to do this.  This traverses the stack calling
`inspect.getframeinfo` for each frame in the stack, returning a list of stack
frames and the info attached to them.  We were then ourselves calling
`getframeinfo` on the returned stack frames to find the filename.

This implementation does less work by stopping inspecting frames when we've
found the one we're looking for and only calling `getframeinfo` once per frame.
We use a frame iterator that we've implemented ourselves based on the
implementation of `inspect.getouterframes`.

This fixes an odd error I was seeing with some doctests I'd written:

    Traceback (most recent call last):
      File "/usr/lib/python2.7/doctest.py", line 1315, in __run
        compileflags, 1) in test.globs
      File "<doctest example_selftest.auto_selftest_Dialog[0]>", line 1, in <module>
        Dialog(frame=f("frame-object-with-dialog-different-background.png"))
      File "/home/william-manley/Projects/stb-tester/_stbt/core.py", line 1254, in __repr__
        args = ", ".join(("%s=%r" % x) for x in self._iter_attrs())
      File "/home/william-manley/Projects/stb-tester/_stbt/core.py", line 1254, in <genexpr>
        args = ", ".join(("%s=%r" % x) for x in self._iter_attrs())
      File "/home/william-manley/Projects/stb-tester/_stbt/core.py", line 1258, in _iter_attrs
        if self:
      File "/home/william-manley/Projects/stb-tester/_stbt/core.py", line 1266, in __nonzero__
        return bool(self.is_visible)
      File "/home/william-manley/Projects/stb-tester/_stbt/core.py", line 1199, in inner
        self._FrameObject__frame_object_cache[fn] = fn(self)
      File "/home/william-manley/Projects/stb-tester/tests/selftest-example/selftest/auto_selftest/tests/../../../tests/example.py", line 10, in is_visible
        return bool(stbt.match('info.png', frame=self._frame))
      File "/home/william-manley/Projects/stb-tester/stbt/__init__.py", line 140, in match
        return _dut.match(image, frame, match_parameters, region)
      File "/home/william-manley/Projects/stb-tester/_stbt/core.py", line 636, in match
        template = _load_template(image)
      File "/home/william-manley/Projects/stb-tester/_stbt/core.py", line 482, in _load_template
        template_name = _find_path(template)
      File "/home/william-manley/Projects/stb-tester/_stbt/core.py", line 2215, in _find_path
        for caller in inspect.stack()[2:]:
      File "/usr/lib/python2.7/inspect.py", line 1060, in stack
        return getouterframes(sys._getframe(1), context)
      File "/usr/lib/python2.7/inspect.py", line 1038, in getouterframes
        framelist.append((frame,) + getframeinfo(frame, context))
      File "/usr/lib/python2.7/inspect.py", line 1009, in getframeinfo
        filename = getsourcefile(frame) or getfile(frame)
      File "/usr/lib/python2.7/inspect.py", line 454, in getsourcefile
        if hasattr(getmodule(object, filename), '__loader__'):
      File "/usr/lib/python2.7/inspect.py", line 483, in getmodule
        file = getabsfile(object, _filename)
      File "/usr/lib/python2.7/inspect.py", line 467, in getabsfile
        return os.path.normcase(os.path.abspath(_filename))
      File "/usr/lib/python2.7/posixpath.py", line 364, in abspath
        cwd = os.getcwd()
    OSError: [Errno 2] No such file or directory

Really odd.

LewisHaley/stb-tester,LewisHaley/stb-tester,stb-tester/stb-tester,martynjarvis/stb-tester,stb-tester/stb-tester,LewisHaley/stb-tester,LewisHaley/stb-tester,stb-tester/stb-tester,stb-tester/stb-tester,martynjarvis/stb-tester,martynjarvis/stb-tester,LewisHaley/stb-tester,martynjarvis/stb-tester,martynjarvis/stb-tester,LewisHaley/stb-tester,martynjarvis/stb-tester,martynjarvis/stb-tester,LewisHaley/stb-tester