~sylvain-pineau/checkbox/bug1091633

« back to all changes in this revision

Viewing changes to hwtest/ui.py

  • Committer: Marc Tardif
  • Date: 2007-09-29 20:37:52 UTC
  • mto: This revision was merged to the branch mainline in revision 28.
  • Revision ID: marc.tardif@canonical.com-20070929203752-5aarzgo5krdbuakk
Initial commit to refactor gui interface.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
class Ui(object):
2
 
    def __init__(self, application):
3
 
        self.application = application
4
 
 
5
 
    def show_intro(self):
6
 
        """Show the introduction page."""
7
 
        pass
8
 
 
9
 
    def show_tests(self):
10
 
        """Show each test page."""
11
 
        pass
12
 
 
13
 
    def show_send(self):
14
 
        """Show send page."""
15
 
        pass
16
 
 
17
 
    def main(self):
18
 
        """Main routine."""
19
 
        pass
 
1
import os
 
2
import optparse, gettext
 
3
from gettext import gettext as _
 
4
 
 
5
from hwtest.application import ApplicationManager
 
6
from hwtest.constants import SHARE_DIR
 
7
from hwtest.excluder import Excluder
 
8
 
 
9
 
 
10
DIRECTION_NEXT = 1
 
11
DIRECTION_PREVIOUS = 0
 
12
 
 
13
 
 
14
class UserInterface(object):
 
15
    '''Abstract base class for encapsulating the workflow and common code for
 
16
       any user interface implementation (like GTK, Qt, or CLI).
 
17
 
 
18
       A concrete subclass must implement all the abstract ui_* methods.'''
 
19
 
 
20
    def __init__(self):
 
21
        self.gettext_domain = 'hwtest'
 
22
        self.application = None
 
23
        self.tests = None
 
24
 
 
25
        gettext.textdomain(self.gettext_domain)
 
26
        self.parse_argv()
 
27
 
 
28
    def run_tests(self):
 
29
        # Determine category
 
30
        category = self.ui_present_categories(_("Hardware Categories"),
 
31
            _("Please specify the type of hardware being tested:"))
 
32
        exclude_func = lambda test, category=category: \
 
33
                       category not in test.categories
 
34
 
 
35
        # Iterate over tests
 
36
        manager_tests = self.application.get_tests()
 
37
        tests = Excluder(manager_tests, exclude_func, exclude_func)
 
38
 
 
39
        direction = DIRECTION_NEXT
 
40
        while tests.has_next():
 
41
            if direction == DIRECTION_NEXT:
 
42
                test = tests.next()
 
43
            elif direction == DIRECTION_PREVIOUS:
 
44
                test = tests.prev()
 
45
            else:
 
46
                raise Exception, "invalid direction: %s" % direction
 
47
            direction = self.ui_present_question(test, tests.has_prev(), tests.has_next())
 
48
 
 
49
        # Exchange test results
 
50
        error = None
 
51
        while True:
 
52
            secure_id = self.ui_present_exchange(error)
 
53
            self.application.report.secure_id = secure_id
 
54
            self.application.run()
 
55
            error = self.application.plugin_manager.get_error()
 
56
            if not error:
 
57
                break
 
58
 
 
59
    def run_argv(self):
 
60
        self.application = ApplicationManager().create(self.options)
 
61
        self.run_tests()
 
62
 
 
63
    def parse_argv(self):
 
64
        optparser = optparse.OptionParser('%prog [options]')
 
65
        optparser.add_option("-q", "--questions", metavar="FILE",
 
66
                          default=os.path.join(SHARE_DIR, "questions.txt"),
 
67
                          help="The file containing certification questions.")
 
68
        optparser.add_option("-d", "--data-path", metavar="PATH",
 
69
                          default="~/.hwtest",
 
70
                          help="The directory to store data files in.")
 
71
        optparser.add_option("-l", "--log", metavar="FILE",
 
72
                          help="The file to write the log to.")
 
73
        optparser.add_option("--log-level",
 
74
                          default="critical",
 
75
                          help="One of debug, info, warning, error or critical.")
 
76
 
 
77
        (self.options, self.args) = optparser.parse_args()
 
78
 
 
79
    def ui_present_categories(self, title, text):
 
80
        raise NotImplementedError, 'this function must be overridden by subclasses'
 
81
 
 
82
    def ui_present_question(self, test, has_prev, has_next):
 
83
        raise NotImplementedError, 'this function must be overridden by subclasses'
 
84
 
 
85
    def ui_present_exchange(self, error):
 
86
        raise NotImplementedError, 'this function must be overridden by subclasses'