~testing-cabal/ubuntu/oneiric/tribunal/daily-build-packaging

« back to all changes in this revision

Viewing changes to tribunal/app.py

  • Committer: Jelmer Vernooij
  • Date: 2010-08-23 22:28:24 UTC
  • mfrom: (161.2.1 upstream)
  • Revision ID: jelmer@samba.org-20100823222824-wre9thghcd7pee55
New upstream snapshot.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2009, 2010 Martin Pool
 
2
#
 
3
# Licensed under the Apache License, Version 2.0 (the "License"); you may not
 
4
# use this file except in compliance with the License. 
 
5
#
 
6
# You may obtain a copy of the License at
 
7
# http://www.apache.org/licenses/LICENSE-2.0 
 
8
#
 
9
# Unless required by applicable law or agreed to in writing, software
 
10
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 
11
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 
12
# License for the specific language governing permissions and limitations
 
13
# under the License. 
 
14
#
 
15
# Part of Tribunal <https://launchpad.net/tribunal>
 
16
 
 
17
 
 
18
"""Main tribunal test app object."""
 
19
 
 
20
import optparse
 
21
import os.path
 
22
import sys
 
23
 
 
24
import gtk
 
25
import gtk.glade
 
26
 
 
27
import tribunal
 
28
from tribunal import (
 
29
    eventloop,
 
30
    util,
 
31
    window,
 
32
    )
 
33
from tribunal.source import (
 
34
    FileTestSource,
 
35
    NoStreamPresent,
 
36
    TestRepositorySource,
 
37
    )
 
38
 
 
39
    
 
40
class ViewSubunitApp(object):
 
41
    """Application object. 
 
42
 
 
43
    Receives events from the window.
 
44
    """
 
45
    # the separation of concerns from the Window is: if we later support
 
46
    # multiple windows looking at different results, there will be just one
 
47
    # app across all of them
 
48
    
 
49
    def __init__(self):
 
50
        self._event_loop = eventloop.GtkLoop()
 
51
 
 
52
    def _construct_and_show_window(self):
 
53
        glade_filename = util.sibpath(tribunal.__file__, 'ui.glade')
 
54
        glade_xml = gtk.glade.XML(glade_filename)
 
55
        self._window = window.SubunitWindow(glade_xml)
 
56
        glade_xml.signal_autoconnect(self)
 
57
        self._set_default_icons()
 
58
        self._window.show_all()
 
59
 
 
60
    def on_quit_menuitem_activate(self, widget):
 
61
        self._event_loop.stop()
 
62
 
 
63
    def on_subunit_window_destroy(self, widget):
 
64
        self._event_loop.stop()
 
65
 
 
66
    def run(self, argv):
 
67
        """Run the app: create a window, load the file, handle input.
 
68
        
 
69
        Return a return code.
 
70
        """
 
71
 
 
72
        parser = optparse.OptionParser()
 
73
        parser.add_option('--testr',
 
74
            help='read from a testrepository in the given directory')
 
75
 
 
76
        opts, args = parser.parse_args(argv)
 
77
 
 
78
        if opts.testr:
 
79
            source = TestRepositorySource(opts.testr)            
 
80
        elif args == ['-']:
 
81
            source = FileTestSource(sys.stdin)
 
82
        elif len(args) == 1:
 
83
            if os.path.isdir(args[0]):
 
84
                source = TestRepositorySource(".")
 
85
            else:
 
86
                source = FileTestSource(file(args[0], 'rb'))
 
87
        elif os.path.exists(os.path.join(args[0], ".testr.conf")):
 
88
            dialog = gtk.Dialog(title="No testrepository present",
 
89
                parent=None,
 
90
                buttons=(gtk.STOCK_YES, gtk.RESPONSE_YES,
 
91
                        gtk.STOCK_NO, gtk.RESPONSE_NO))
 
92
            label = gtk.Label("No test repository is present. Create one?")
 
93
            dialog.vbox.pack_start(label, True, True, 0)
 
94
            label.show()
 
95
            resp = dialog.run()
 
96
            dialog.destroy()
 
97
            if resp == gtk.RESPONSE_YES:
 
98
                import testrepository.repository.file    
 
99
                factory = testrepository.repository.file.RepositoryFactory()
 
100
                factory.initialise(".")
 
101
                source = TestRepositorySource(".")
 
102
            else:
 
103
                sys.exit(1)
 
104
        else:
 
105
            print "usage: tribunal-subunit SUBUNIT_FILE"
 
106
            print "       tribunal-subunit --testr DIR"
 
107
            sys.exit(1)
 
108
 
 
109
        self._construct_and_show_window()
 
110
        self._window.set_source(source)
 
111
        try:
 
112
            stream = source.get_subunit_stream()
 
113
        except NoStreamPresent:
 
114
            pass
 
115
        else:
 
116
            self._window.load_from_subunit_stream(stream)
 
117
        self._event_loop.start()
 
118
 
 
119
    def _set_default_icons(self):
 
120
        # XXX: maybe should use pkgresources?
 
121
        icon_pixbuf = gtk.gdk.pixbuf_new_from_file(
 
122
            os.path.join(os.path.dirname(tribunal.__file__), 'gavel_64x64.jpg'))
 
123
        gtk.window_set_default_icon_list(icon_pixbuf)
 
124
 
 
125