| 1 |
|
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
""" |
|---|
| 7 |
This module provides support for Twisted to interact with the glib/gtk2 |
|---|
| 8 |
mainloop. |
|---|
| 9 |
|
|---|
| 10 |
In order to use this support, simply do the following:: |
|---|
| 11 |
|
|---|
| 12 |
| from twisted.internet import gtk2reactor |
|---|
| 13 |
| gtk2reactor.install() |
|---|
| 14 |
|
|---|
| 15 |
Then use twisted.internet APIs as usual. The other methods here are not |
|---|
| 16 |
intended to be called directly. |
|---|
| 17 |
|
|---|
| 18 |
When installing the reactor, you can choose whether to use the glib |
|---|
| 19 |
event loop or the GTK+ event loop which is based on it but adds GUI |
|---|
| 20 |
integration. |
|---|
| 21 |
""" |
|---|
| 22 |
|
|---|
| 23 |
|
|---|
| 24 |
import sys |
|---|
| 25 |
from zope.interface import implements |
|---|
| 26 |
try: |
|---|
| 27 |
if not hasattr(sys, 'frozen'): |
|---|
| 28 |
|
|---|
| 29 |
import pygtk |
|---|
| 30 |
pygtk.require('2.0') |
|---|
| 31 |
except (ImportError, AttributeError): |
|---|
| 32 |
pass |
|---|
| 33 |
import gobject |
|---|
| 34 |
if hasattr(gobject, "threads_init"): |
|---|
| 35 |
|
|---|
| 36 |
|
|---|
| 37 |
|
|---|
| 38 |
gobject.threads_init() |
|---|
| 39 |
|
|---|
| 40 |
|
|---|
| 41 |
from twisted.python import log, runtime, failure |
|---|
| 42 |
from twisted.internet.interfaces import IReactorFDSet |
|---|
| 43 |
from twisted.internet import main, posixbase, error, selectreactor |
|---|
| 44 |
|
|---|
| 45 |
POLL_DISCONNECTED = gobject.IO_HUP | gobject.IO_ERR | gobject.IO_NVAL |
|---|
| 46 |
|
|---|
| 47 |
|
|---|
| 48 |
|
|---|
| 49 |
|
|---|
| 50 |
INFLAGS = gobject.IO_IN | POLL_DISCONNECTED |
|---|
| 51 |
OUTFLAGS = gobject.IO_OUT | POLL_DISCONNECTED |
|---|
| 52 |
|
|---|
| 53 |
def _our_mainquit(): |
|---|
| 54 |
|
|---|
| 55 |
|
|---|
| 56 |
|
|---|
| 57 |
|
|---|
| 58 |
|
|---|
| 59 |
import gtk |
|---|
| 60 |
if gtk.main_level(): |
|---|
| 61 |
gtk.main_quit() |
|---|
| 62 |
|
|---|
| 63 |
class Gtk2Reactor(posixbase.PosixReactorBase): |
|---|
| 64 |
""" |
|---|
| 65 |
GTK+-2 event loop reactor. |
|---|
| 66 |
|
|---|
| 67 |
@ivar _reads: A dictionary mapping L{FileDescriptor} instances to gtk |
|---|
| 68 |
INPUT_READ watch handles. |
|---|
| 69 |
|
|---|
| 70 |
@ivar _writes: A dictionary mapping L{FileDescriptor} instances to gtk |
|---|
| 71 |
INTPUT_WRITE watch handles. |
|---|
| 72 |
|
|---|
| 73 |
@ivar _simtag: A gtk timeout handle for the next L{simulate} call. |
|---|
| 74 |
""" |
|---|
| 75 |
implements(IReactorFDSet) |
|---|
| 76 |
|
|---|
| 77 |
def __init__(self, useGtk=True): |
|---|
| 78 |
self._simtag = None |
|---|
| 79 |
self._reads = {} |
|---|
| 80 |
self._writes = {} |
|---|
| 81 |
posixbase.PosixReactorBase.__init__(self) |
|---|
| 82 |
|
|---|
| 83 |
|
|---|
| 84 |
if getattr(gobject, "pygtk_version", ()) >= (2, 3, 91) and not useGtk: |
|---|
| 85 |
self.context = gobject.main_context_default() |
|---|
| 86 |
self.__pending = self.context.pending |
|---|
| 87 |
self.__iteration = self.context.iteration |
|---|
| 88 |
self.loop = gobject.MainLoop() |
|---|
| 89 |
self.__crash = self.loop.quit |
|---|
| 90 |
self.__run = self.loop.run |
|---|
| 91 |
else: |
|---|
| 92 |
import gtk |
|---|
| 93 |
self.__pending = gtk.events_pending |
|---|
| 94 |
self.__iteration = gtk.main_iteration |
|---|
| 95 |
self.__crash = _our_mainquit |
|---|
| 96 |
self.__run = gtk.main |
|---|
| 97 |
|
|---|
| 98 |
|
|---|
| 99 |
|
|---|
| 100 |
|
|---|
| 101 |
|
|---|
| 102 |
|
|---|
| 103 |
|
|---|
| 104 |
|
|---|
| 105 |
|
|---|
| 106 |
|
|---|
| 107 |
def input_add(self, source, condition, callback): |
|---|
| 108 |
if hasattr(source, 'fileno'): |
|---|
| 109 |
|
|---|
| 110 |
def wrapper(source, condition, real_s=source, real_cb=callback): |
|---|
| 111 |
return real_cb(real_s, condition) |
|---|
| 112 |
return gobject.io_add_watch(source.fileno(), condition, wrapper) |
|---|
| 113 |
else: |
|---|
| 114 |
return gobject.io_add_watch(source, condition, callback) |
|---|
| 115 |
|
|---|
| 116 |
def addReader(self, reader): |
|---|
| 117 |
if reader not in self._reads: |
|---|
| 118 |
self._reads[reader] = self.input_add(reader, INFLAGS, self.callback) |
|---|
| 119 |
|
|---|
| 120 |
def addWriter(self, writer): |
|---|
| 121 |
if writer not in self._writes: |
|---|
| 122 |
self._writes[writer] = self.input_add(writer, OUTFLAGS, self.callback) |
|---|
| 123 |
|
|---|
| 124 |
|
|---|
| 125 |
def getReaders(self): |
|---|
| 126 |
return self._reads.keys() |
|---|
| 127 |
|
|---|
| 128 |
|
|---|
| 129 |
def getWriters(self): |
|---|
| 130 |
return self._writes.keys() |
|---|
| 131 |
|
|---|
| 132 |
|
|---|
| 133 |
def removeAll(self): |
|---|
| 134 |
return self._removeAll(self._reads, self._writes) |
|---|
| 135 |
|
|---|
| 136 |
|
|---|
| 137 |
def removeReader(self, reader): |
|---|
| 138 |
if reader in self._reads: |
|---|
| 139 |
gobject.source_remove(self._reads[reader]) |
|---|
| 140 |
del self._reads[reader] |
|---|
| 141 |
|
|---|
| 142 |
def removeWriter(self, writer): |
|---|
| 143 |
if writer in self._writes: |
|---|
| 144 |
gobject.source_remove(self._writes[writer]) |
|---|
| 145 |
del self._writes[writer] |
|---|
| 146 |
|
|---|
| 147 |
doIterationTimer = None |
|---|
| 148 |
|
|---|
| 149 |
def doIterationTimeout(self, *args): |
|---|
| 150 |
self.doIterationTimer = None |
|---|
| 151 |
return 0 |
|---|
| 152 |
|
|---|
| 153 |
def doIteration(self, delay): |
|---|
| 154 |
|
|---|
| 155 |
|
|---|
| 156 |
|
|---|
| 157 |
|
|---|
| 158 |
log.msg(channel='system', event='iteration', reactor=self) |
|---|
| 159 |
if self.__pending(): |
|---|
| 160 |
self.__iteration(0) |
|---|
| 161 |
return |
|---|
| 162 |
|
|---|
| 163 |
if delay == 0: |
|---|
| 164 |
return |
|---|
| 165 |
self.doIterationTimer = gobject.timeout_add(int(delay * 1000), |
|---|
| 166 |
self.doIterationTimeout) |
|---|
| 167 |
|
|---|
| 168 |
self.__iteration(1) |
|---|
| 169 |
|
|---|
| 170 |
|
|---|
| 171 |
if self.doIterationTimer: |
|---|
| 172 |
|
|---|
| 173 |
gobject.source_remove(self.doIterationTimer) |
|---|
| 174 |
self.doIterationTimer = None |
|---|
| 175 |
|
|---|
| 176 |
def crash(self): |
|---|
| 177 |
posixbase.PosixReactorBase.crash(self) |
|---|
| 178 |
self.__crash() |
|---|
| 179 |
|
|---|
| 180 |
def run(self, installSignalHandlers=1): |
|---|
| 181 |
self.startRunning(installSignalHandlers=installSignalHandlers) |
|---|
| 182 |
gobject.timeout_add(0, self.simulate) |
|---|
| 183 |
if self._started: |
|---|
| 184 |
self.__run() |
|---|
| 185 |
|
|---|
| 186 |
def _doReadOrWrite(self, source, condition, faildict={ |
|---|
| 187 |
error.ConnectionDone: failure.Failure(error.ConnectionDone()), |
|---|
| 188 |
error.ConnectionLost: failure.Failure(error.ConnectionLost()), |
|---|
| 189 |
}): |
|---|
| 190 |
why = None |
|---|
| 191 |
didRead = None |
|---|
| 192 |
if condition & POLL_DISCONNECTED and \ |
|---|
| 193 |
not (condition & gobject.IO_IN): |
|---|
| 194 |
why = main.CONNECTION_LOST |
|---|
| 195 |
else: |
|---|
| 196 |
try: |
|---|
| 197 |
if condition & gobject.IO_IN: |
|---|
| 198 |
why = source.doRead() |
|---|
| 199 |
didRead = source.doRead |
|---|
| 200 |
if not why and condition & gobject.IO_OUT: |
|---|
| 201 |
|
|---|
| 202 |
|
|---|
| 203 |
if not source.disconnected and source.doWrite != didRead: |
|---|
| 204 |
why = source.doWrite() |
|---|
| 205 |
didRead = source.doWrite |
|---|
| 206 |
except: |
|---|
| 207 |
why = sys.exc_info()[1] |
|---|
| 208 |
log.msg('Error In %s' % source) |
|---|
| 209 |
log.deferr() |
|---|
| 210 |
|
|---|
| 211 |
if why: |
|---|
| 212 |
self._disconnectSelectable(source, why, didRead == source.doRead) |
|---|
| 213 |
|
|---|
| 214 |
def callback(self, source, condition): |
|---|
| 215 |
log.callWithLogger(source, self._doReadOrWrite, source, condition) |
|---|
| 216 |
self.simulate() |
|---|
| 217 |
return 1 |
|---|
| 218 |
|
|---|
| 219 |
def simulate(self): |
|---|
| 220 |
"""Run simulation loops and reschedule callbacks. |
|---|
| 221 |
""" |
|---|
| 222 |
if self._simtag is not None: |
|---|
| 223 |
gobject.source_remove(self._simtag) |
|---|
| 224 |
self.runUntilCurrent() |
|---|
| 225 |
timeout = min(self.timeout(), 0.1) |
|---|
| 226 |
if timeout is None: |
|---|
| 227 |
timeout = 0.1 |
|---|
| 228 |
|
|---|
| 229 |
self._simtag = gobject.timeout_add(int(timeout * 1010), self.simulate) |
|---|
| 230 |
|
|---|
| 231 |
|
|---|
| 232 |
class PortableGtkReactor(selectreactor.SelectReactor): |
|---|
| 233 |
""" |
|---|
| 234 |
Reactor that works on Windows. |
|---|
| 235 |
|
|---|
| 236 |
Sockets aren't supported by GTK+'s input_add on Win32. |
|---|
| 237 |
""" |
|---|
| 238 |
_simtag = None |
|---|
| 239 |
|
|---|
| 240 |
def crash(self): |
|---|
| 241 |
selectreactor.SelectReactor.crash(self) |
|---|
| 242 |
import gtk |
|---|
| 243 |
|
|---|
| 244 |
if gtk.main_level(): |
|---|
| 245 |
if hasattr(gtk, 'main_quit'): |
|---|
| 246 |
gtk.main_quit() |
|---|
| 247 |
else: |
|---|
| 248 |
gtk.mainquit() |
|---|
| 249 |
|
|---|
| 250 |
def run(self, installSignalHandlers=1): |
|---|
| 251 |
import gtk |
|---|
| 252 |
self.startRunning(installSignalHandlers=installSignalHandlers) |
|---|
| 253 |
gobject.timeout_add(0, self.simulate) |
|---|
| 254 |
|
|---|
| 255 |
if hasattr(gtk, 'main'): |
|---|
| 256 |
gtk.main() |
|---|
| 257 |
else: |
|---|
| 258 |
gtk.mainloop() |
|---|
| 259 |
|
|---|
| 260 |
def simulate(self): |
|---|
| 261 |
"""Run simulation loops and reschedule callbacks. |
|---|
| 262 |
""" |
|---|
| 263 |
if self._simtag is not None: |
|---|
| 264 |
gobject.source_remove(self._simtag) |
|---|
| 265 |
self.iterate() |
|---|
| 266 |
timeout = min(self.timeout(), 0.1) |
|---|
| 267 |
if timeout is None: |
|---|
| 268 |
timeout = 0.1 |
|---|
| 269 |
|
|---|
| 270 |
self._simtag = gobject.timeout_add(int(timeout * 1010), self.simulate) |
|---|
| 271 |
|
|---|
| 272 |
|
|---|
| 273 |
def install(useGtk=True): |
|---|
| 274 |
"""Configure the twisted mainloop to be run inside the gtk mainloop. |
|---|
| 275 |
|
|---|
| 276 |
@param useGtk: should glib rather than GTK+ event loop be |
|---|
| 277 |
used (this will be slightly faster but does not support GUI). |
|---|
| 278 |
""" |
|---|
| 279 |
reactor = Gtk2Reactor(useGtk) |
|---|
| 280 |
from twisted.internet.main import installReactor |
|---|
| 281 |
installReactor(reactor) |
|---|
| 282 |
return reactor |
|---|
| 283 |
|
|---|
| 284 |
def portableInstall(useGtk=True): |
|---|
| 285 |
"""Configure the twisted mainloop to be run inside the gtk mainloop. |
|---|
| 286 |
""" |
|---|
| 287 |
reactor = PortableGtkReactor() |
|---|
| 288 |
from twisted.internet.main import installReactor |
|---|
| 289 |
installReactor(reactor) |
|---|
| 290 |
return reactor |
|---|
| 291 |
|
|---|
| 292 |
if runtime.platform.getType() != 'posix': |
|---|
| 293 |
install = portableInstall |
|---|
| 294 |
|
|---|
| 295 |
|
|---|
| 296 |
__all__ = ['install'] |
|---|