~landscape/zope3/newer-from-ztk

« back to all changes in this revision

Viewing changes to src/twisted/scripts/manhole.py

  • Committer: Thomas Hervé
  • Date: 2009-07-08 13:52:04 UTC
  • Revision ID: thomas@canonical.com-20090708135204-df5eesrthifpylf8
Remove twisted copy

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (c) 2001-2004 Twisted Matrix Laboratories.
2
 
# See LICENSE for details.
3
 
 
4
 
 
5
 
"""Start a L{twisted.manhole} client.
6
 
 
7
 
@var toolkitPreference: A list of all toolkits we have front-ends for, with
8
 
   the ones we most prefer to use listed first.
9
 
@type toolkitPreference: list of strings
10
 
"""
11
 
 
12
 
import sys
13
 
 
14
 
from twisted.python import usage
15
 
 
16
 
# Prefer gtk2 because it is the way of the future!
17
 
toolkitPreference = ('gtk2', 'gtk1')
18
 
 
19
 
class NoToolkitError(usage.UsageError):
20
 
    wantToolkits = toolkitPreference
21
 
    def __str__(self):
22
 
        return (
23
 
            "I couldn't find any of these toolkits installed, and I need "
24
 
            "one of them to run: %s" % (', '.join(self.wantToolkits),))
25
 
 
26
 
def bestToolkit():
27
 
    """The most-preferred available toolkit.
28
 
 
29
 
    @returntype: string
30
 
    """
31
 
    avail = getAvailableToolkits()
32
 
    for v in toolkitPreference:
33
 
        if v in avail:
34
 
            return v
35
 
    else:
36
 
        raise NoToolkitError
37
 
 
38
 
_availableToolkits = None
39
 
def getAvailableToolkits():
40
 
    """Autodetect available toolkits.
41
 
 
42
 
    @returns: A list of usable toolkits.
43
 
    @returntype: list of strings
44
 
    """
45
 
    global _availableToolkits
46
 
    # use cached result
47
 
    if _availableToolkits is not None:
48
 
        return _availableToolkits
49
 
 
50
 
    avail = []
51
 
 
52
 
    # Recent GTK.
53
 
    try:
54
 
        import pygtk
55
 
    except:
56
 
        pass
57
 
    else:
58
 
        gtkvers = pygtk._get_available_versions().keys()
59
 
        for v in gtkvers:
60
 
            frontend = {'1.2': 'gtk1',
61
 
                        '2.0': 'gtk2'}.get(v)
62
 
            if frontend is not None:
63
 
                avail.append(frontend)
64
 
 
65
 
    if not avail:
66
 
        # Older GTK
67
 
        try:
68
 
            # WARNING: It's entirely possible that this does something crappy,
69
 
            # such as running gtk_init, which may have undesirable side
70
 
            # effects if that's not the toolkit we end up using.
71
 
            import gtk
72
 
        except:
73
 
            pass
74
 
        else:
75
 
            avail.append('gtk1')
76
 
 
77
 
    # There may be some "middle gtk" that got left out -- that is, a
78
 
    # version of pygtk 1.99.x that happened before the pygtk module
79
 
    # with its _get_available_versions was introduced.  Chances are
80
 
    # that the gtk2 front-end wouldn't work with it anyway, but it may
81
 
    # get mis-identified it as gtk1. :(
82
 
 
83
 
    _availableToolkits = avail
84
 
    return avail
85
 
 
86
 
 
87
 
def run():
88
 
    config = MyOptions()
89
 
    try:
90
 
        config.parseOptions()
91
 
    except usage.UsageError, e:
92
 
        print str(e)
93
 
        print str(config)
94
 
        sys.exit(1)
95
 
 
96
 
    try:
97
 
        run = getattr(sys.modules[__name__], 'run_' + config.opts['toolkit'])
98
 
    except AttributeError:
99
 
        print "Sorry, no support for toolkit %r." % (config.opts['toolkit'],)
100
 
        sys.exit(1)
101
 
 
102
 
    run(config)
103
 
 
104
 
    from twisted.internet import reactor
105
 
    reactor.run()
106
 
 
107
 
def run_gtk1(config):
108
 
    # Put these off until after we parse options, so we know what reactor
109
 
    # to install.
110
 
    from twisted.internet import gtkreactor
111
 
    gtkreactor.install()
112
 
    from twisted.spread.ui import gtkutil
113
 
 
114
 
    # Put this off until after we parse options, or else gnome eats them.
115
 
    # (http://www.daa.com.au/pipermail/pygtk/2002-December/004051.html)
116
 
    sys.argv[:] = ['manhole']
117
 
    from twisted.manhole.ui import gtkmanhole
118
 
 
119
 
    i = gtkmanhole.Interaction()
120
 
    lw = gtkutil.Login(i.connected,
121
 
                       i.client,
122
 
                       initialUser=config.opts['user'],
123
 
                       initialPassword=config.opts['password'],
124
 
                       initialService=config.opts['service'],
125
 
                       initialHostname=config.opts['host'],
126
 
                       initialPortno=config.opts['port'],
127
 
                       initialPerspective=config.opts['perspective'])
128
 
 
129
 
    i.loginWindow = lw
130
 
 
131
 
    lw.show_all()
132
 
 
133
 
 
134
 
def run_gtk2(config):
135
 
    # Put these off until after we parse options, so we know what reactor
136
 
    # to load.
137
 
    from twisted.internet import gtk2reactor
138
 
    gtk2reactor.install()
139
 
    from twisted.spread.ui import gtk2util
140
 
 
141
 
    # Put this off until after we parse options, or else gnome eats them.
142
 
    sys.argv[:] = ['manhole']
143
 
    from twisted.manhole.ui import gtk2manhole
144
 
 
145
 
    o = config.opts
146
 
    defaults = {
147
 
        'host': o['host'],
148
 
        'port': o['port'],
149
 
        'identityName': o['user'],
150
 
        'password': o['password'],
151
 
        'serviceName': o['service'],
152
 
        'perspectiveName': o['perspective']
153
 
        }
154
 
    w = gtk2manhole.ManholeWindow()
155
 
    w.setDefaults(defaults)
156
 
    w.login()
157
 
 
158
 
 
159
 
# from twisted.spread import pb
160
 
# can't do that, it installs a reactor.  grr.
161
 
pbportno = 8787
162
 
 
163
 
class MyOptions(usage.Options):
164
 
    optParameters=[("user", "u", "guest", "username"),
165
 
                   ("password", "w", "guest"),
166
 
                   ("service", "s", "twisted.manhole", "PB Service"),
167
 
                   ("host", "h", "localhost"),
168
 
                   ("port", "p", str(pbportno)),
169
 
                   ("perspective", "P", "",
170
 
                    "PB Perspective to ask for "
171
 
                    "(if different than username)"),
172
 
                   ("toolkit", "t", bestToolkit(),
173
 
                    "Front-end to use; one of %s"
174
 
                    % (' '.join(getAvailableToolkits()),)),
175
 
                   ]
176
 
 
177
 
    #zsh_altArgDescr = {"foo":"use this description for foo instead"}
178
 
    #zsh_multiUse = ["foo", "bar"]
179
 
    #zsh_mutuallyExclusive = [("foo", "bar"), ("bar", "baz")]
180
 
    zsh_actions = {"host":"_hosts",
181
 
                   "toolkit":"(gtk1 gtk2)"}
182
 
    #zsh_actionDescr = {"logfile":"log file name", "random":"random seed"}
183
 
 
184
 
if __name__ == '__main__':
185
 
    run()