/usr/lib/python2.7/dist-packages/protocols/twisted_support.py is in python-protocols 1.0a.svn20070625-7.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 | """Declaration support for Twisted Interfaces"""
__all__ = []
from adapters import *
from api import advise
from interfaces import IOpenProtocol
from weakref import WeakKeyDictionary
# Twisted uses an approach to __adapt__ that is largely incompatible with
# PEP 246, so we have to jump through some twisty hoops to convince it to work
# for our purposes, without breaking Twisted's test suite.
class TwistedAdaptMethod(object):
"""__adapt__ implementation for Twisted interfaces"""
__slots__ = 'iface'
def __init__(self,iface):
self.iface = iface
def __call__(self, obj):
# This is the __adapt__ method that you get
# for ISomething.__adapt__()...
if TwistedImplements(obj, self.iface):
return obj
# Get Twisted to try and adapt
return self.iface(obj, None)
def im_func(self, ob, default):
# And this is what MetaInterface.__call__ calls when
# it goes after __adapt__.im_func!
meth = self.iface.__dict__.get('__adapt__')
if meth is None:
return default
return meth(ob,default)
# Monkeypatch Twisted Interfaces
try:
from twisted.python.components import \
implements as TwistedImplements, \
MetaInterface as TwistedInterfaceClass, \
getInterfaces as TwistedGetInterfaces
except ImportError:
TwistedInterfaceTypes = []
else:
# Force all Twisted interfaces to have an __adapt__ method
TwistedInterfaceClass.__adapt__ = property(lambda s: TwistedAdaptMethod(s))
TwistedInterfaceTypes = [TwistedInterfaceClass]
class TwistedInterfaceAsProtocol(object):
__slots__ = 'iface'
advise(
instancesProvide=[IOpenProtocol],
asAdapterForTypes=TwistedInterfaceTypes,
)
def __init__(self, iface):
self.iface = iface
def __adapt__(self, obj):
return self.iface.__adapt__(obj)
def registerImplementation(self,klass,adapter=NO_ADAPTER_NEEDED,depth=1):
oldImplements = TwistedGetInterfaces(klass)
if adapter is NO_ADAPTER_NEEDED:
klass.__implements__ = self.iface, tuple(oldImplements)
elif adapter is DOES_NOT_SUPPORT:
if self.iface in oldImplements:
oldImplements.remove(self.iface)
klass.__implements__ = tuple(oldImplements)
else:
raise TypeError(
"Twisted interfaces can only declare support, not adapters",
self.iface, klass, adapter
)
def addImpliedProtocol(self, proto, adapter=NO_ADAPTER_NEEDED, depth=1):
iface = self.iface
# XXX need to ensure 'proto' is usable w/Twisted!
self.iface.adaptWith(lambda o: adapter(o, iface), proto)
# XXX is the above sufficient?
# XXX What are Twisted's adapter override semantics?
listeners = iface.__dict__.get('_Protocol__listeners',{})
for listener in listeners.keys(): # Must use keys()!
listener.newProtocolImplied(self, proto, adapter, depth)
def registerObject(self, ob, adapter=NO_ADAPTER_NEEDED, depth=1):
oldImplements = TwistedGetInterfaces(ob)
if adapter is NO_ADAPTER_NEEDED:
ob.__implements__ = self.iface, tuple(oldImplements)
elif adapter is DOES_NOT_SUPPORT:
if self.iface in oldImplements:
oldImplements.remove(self.iface)
ob.__implements__ = tuple(oldImplements)
else:
raise TypeError(
"Twisted interfaces can only declare support, not adapters",
self.iface, ob, adapter
)
def addImplicationListener(self, listener):
listeners = self.iface.__dict__.setdefault(
'_Protocol__listeners',WeakKeyDictionary()
)
listeners[listener] = True
|