pytest-gui / pytest_gui / gtkui / result.py

import gtk

from pygtkhelpers.delegates import SlaveView, WindowView
from pygtkhelpers.ui.objectlist import ObjectTree, Column, Cell
import logging
log = logging.getLogger('resultview')

outcome_to_stock = {
    'collected': gtk.STOCK_ADD,
    'running': gtk.STOCK_EXECUTE,
    'passed': gtk.STOCK_APPLY,
    'failed': gtk.STOCK_CANCEL,
    'skipped': gtk.STOCK_MEDIA_FORWARD,
}


class CollectResultView(SlaveView):

    def __init__(self, master):
        SlaveView.__init__(self)
        self.master = master

    def create_ui(self):

        self.collected = set()
        self.executed = set()

        self.filter_words = []
        self.filter_entry = gtk.Entry()

        self.widget.pack_start(self.filter_entry, expand=False)

        self.hbox = gtk.HBox()
        self.run_tests = gtk.Button("Run")
        self.run_collect = gtk.Button("Recollect")
        self.hbox.add(self.run_tests)
        self.hbox.add(self.run_collect)
        self.widget.pack_start(self.hbox, expand=False)

        self.collection = ObjectTree([
            Column('test_id', cells=[
                Cell('state',
                     format_func=outcome_to_stock.get,
                     use_stock=True),
                Cell('test_id'),
            ]),
            # Column('state'),
        ])
        self.collection.set_headers_visible(False)
        self.collection.set_visible_func(self._filter_function)
        self.collection.set_size_request(300, 400)
        self.scrolled_window = gtk.ScrolledWindow()
        self.scrolled_window.add(self.collection)
        self.widget.add(self.scrolled_window)

        self.progress = gtk.ProgressBar()
        self.widget.pack_start(self.progress, expand=False)

    def _filter_function(self, item):
        return all(x in item.test_id for x in self.filter_words)

    def on_filter_entry__changed(self, entry):
        self.filter_words = entry.get_text().split()
        self.collection.model_filter.refilter()

    def on_collection__item_activated(self, coll, item):
        tbw = TracebackWindow(item)
        tbw.show()

    def report_collect(self, item):
        if item not in self.collection:
            self.collection.append(item)
        self.collection.update(item)
        self.progress.pulse()
        self.collected.add(item)

    def report_result(self, item):
        if item not in self.collection:
            self.collection.append(item)
        self.collection.update(item)
        if item.state == 'collected':
            self.executed.discard(item)
            self.collected.add(item)
        else:
            self.collected.discard(item)
            self.executed.add(item)

        # partially incorrect
        executed = sum(1 for x in self.executed if self._filter_function(x))
        collected = sum(1 for x in self.collected if self._filter_function(x))
        sum_all = collected + executed
        fraction = float(executed) / sum_all
        log.debug('%s %s %s %s', fraction, collected, executed, sum_all)
        self.progress.set_fraction(fraction)
        self.progress.set_text('%s of %s' % (executed, sum_all))

    report_start = report_result

    def on_run_tests__clicked(self, _):
        selected_tests = filter(self._filter_function, self.collection)
        test_ids = [x.test_id for x in selected_tests]
        self.master.run_once(test_ids=test_ids)

    def on_run_collect__clicked(self, btn):
        self.master.run_once(collectonly=True)


class TracebackWindow(WindowView):
    def __init__(self, result):
        self.result = result
        WindowView.__init__(self)

    def create_ui(self):
        self.tv = gtk.TextView()
        self.tv.show()
                                        #xxx weird for skip/xfail
        self.tv.get_buffer().set_text(self.result.report.longrepr)
        self.widget.add(self.tv)
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.