Changeset 165:76a61f5aa343 for wokkel/compat.py
- Timestamp:
- Jan 22, 2012, 2:51:25 PM (10 years ago)
- Branch:
- default
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
wokkel/compat.py
r160 r165 13 13 from itertools import count 14 14 15 from twisted.internet import protocol 15 from twisted.python.deprecate import deprecatedModuleAttribute 16 from twisted.python.versions import Version 16 17 from twisted.words.protocols.jabber import xmlstream 17 18 class BootstrapMixin(object): 19 """ 20 XmlStream factory mixin to install bootstrap event observers. 21 22 This mixin is for factories providing 23 L{IProtocolFactory<twisted.internet.interfaces.IProtocolFactory>} to make 24 sure bootstrap event observers are set up on protocols, before incoming 25 data is processed. Such protocols typically derive from 26 L{utility.EventDispatcher}, like L{XmlStream}. 27 28 You can set up bootstrap event observers using C{addBootstrap}. The 29 C{event} and C{fn} parameters correspond with the C{event} and 30 C{observerfn} arguments to L{utility.EventDispatcher.addObserver}. 31 32 @since: 8.2. 33 @ivar bootstraps: The list of registered bootstrap event observers. 34 @type bootstrap: C{list} 35 """ 36 37 def __init__(self): 38 self.bootstraps = [] 39 40 41 def installBootstraps(self, dispatcher): 42 """ 43 Install registered bootstrap observers. 44 45 @param dispatcher: Event dispatcher to add the observers to. 46 @type dispatcher: L{utility.EventDispatcher} 47 """ 48 for event, fn in self.bootstraps: 49 dispatcher.addObserver(event, fn) 50 51 52 def addBootstrap(self, event, fn): 53 """ 54 Add a bootstrap event handler. 55 56 @param event: The event to register an observer for. 57 @type event: C{str} or L{xpath.XPathQuery} 58 @param fn: The observer callable to be registered. 59 """ 60 self.bootstraps.append((event, fn)) 61 62 63 def removeBootstrap(self, event, fn): 64 """ 65 Remove a bootstrap event handler. 66 67 @param event: The event the observer is registered for. 68 @type event: C{str} or L{xpath.XPathQuery} 69 @param fn: The registered observer callable. 70 """ 71 self.bootstraps.remove((event, fn)) 72 73 74 75 class XmlStreamServerFactory(BootstrapMixin, 76 protocol.ServerFactory): 77 """ 78 Factory for Jabber XmlStream objects as a server. 79 80 @since: 8.2. 81 @ivar authenticatorFactory: Factory callable that takes no arguments, to 82 create a fresh authenticator to be associated 83 with the XmlStream. 84 """ 85 86 protocol = xmlstream.XmlStream 87 88 def __init__(self, authenticatorFactory): 89 BootstrapMixin.__init__(self) 90 self.authenticatorFactory = authenticatorFactory 91 92 93 def buildProtocol(self, addr): 94 """ 95 Create an instance of XmlStream. 96 97 A new authenticator instance will be created and passed to the new 98 XmlStream. Registered bootstrap event observers are installed as well. 99 """ 100 authenticator = self.authenticatorFactory() 101 xs = self.protocol(authenticator) 102 xs.factory = self 103 self.installBootstraps(xs) 104 return xs 105 106 18 from twisted.words.protocols.jabber.xmlstream import XmlStreamServerFactory 19 from twisted.words.xish.xmlstream import BootstrapMixin 20 21 deprecatedModuleAttribute( 22 Version("Wokkel", 0, 7, 0), 23 "Use twisted.words.xish.xmlstream.BootstrapMixin instead.", 24 __name__, 25 "BootstrapMixin") 26 27 deprecatedModuleAttribute( 28 Version("Wokkel", 0, 7, 0), 29 "Use twisted.words.protocols.jabber.xmlstream.XmlStreamServerFactory " 30 "instead.", 31 __name__, 32 "XmlStreamServerFactory") 107 33 108 34 class IQ(xmlstream.IQ): … … 142 68 @ivar _container: The L{_ConstantsContainer} subclass this constant belongs 143 69 to; only set once the constant is initialized by that subclass. 70 71 @since: Twisted 12.0.0. 144 72 """ 145 73 def __init__(self): … … 185 113 objects representing named constants for a particular L{_ConstantsContainer} 186 114 subclass. 115 116 @since: Twisted 12.0.0. 187 117 """ 188 118 def __get__(self, oself, cls): … … 212 142 instances. This is initialized via the L{_EnumerantsInitializer} 213 143 descriptor the first time it is accessed. 144 145 @since: Twisted 12.0.0. 214 146 """ 215 147 _constantType = None … … 308 240 subclasses. Do not instantiate L{NamedConstant} elsewhere and do not 309 241 subclass it. 242 243 @since: Twisted 12.0.0. 310 244 """ 311 245 … … 316 250 A L{Names} subclass contains constants which differ only in their names and 317 251 identities. 252 253 @since: Twisted 12.0.0. 318 254 """ 319 255 _constantType = NamedConstant … … 328 264 L{ValueConstant} is only for use in the definition of L{Values} subclasses. 329 265 Do not instantiate L{ValueConstant} elsewhere and do not subclass it. 266 267 @since: Twisted 12.0.0. 330 268 """ 331 269 def __init__(self, value): … … 339 277 A L{Values} subclass contains constants which are associated with arbitrary 340 278 values. 279 280 @since: Twisted 12.0.0. 341 281 """ 342 282 _constantType = ValueConstant
Note: See TracChangeset
for help on using the changeset viewer.