/usr/share/pyshared/protocols/classic.py is in python-protocols 1.0a.svn20070625-5build1.
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 205 206 207 208 209 210 211 212 213 214 215 216 217 218 | """Declaration support for Python built-in types"""
__all__ = ['ProviderMixin']
from types import FunctionType, ModuleType, InstanceType, ClassType
from adapters import *
from api import declareImplementation, advise, declareAdapterForObject, adapt
from interfaces import *
from new import instancemethod
from advice import getMRO, metamethod, mkRef
class ProviderMixin:
"""Mixin to support per-instance declarations"""
advise(
instancesProvide=[IOpenProvider, IImplicationListener]
)
def declareProvides(self,protocol,adapter=NO_ADAPTER_NEEDED,depth=1):
registry = self.__dict__.get('__protocols_provided__')
if registry is None:
self.__protocols_provided__ = registry = {}
if updateWithSimplestAdapter(registry,protocol,adapter,depth):
adapt(protocol,IOpenProtocol).addImplicationListener(self)
return True
declareProvides = metamethod(declareProvides)
def newProtocolImplied(self, srcProto, destProto, adapter, depth):
registry = self.__dict__.get('__protocols_provided__',())
if srcProto not in registry:
return
baseAdapter, d = registry[srcProto]
adapter = composeAdapters(baseAdapter,srcProto,adapter)
declareAdapterForObject(
destProto, adapter, self, depth+d
)
newProtocolImplied = metamethod(newProtocolImplied)
def __conform__(self,protocol):
for cls in getMRO(self):
conf = cls.__dict__.get('__protocols_provided__',())
if protocol in conf:
return conf[protocol][0](self)
__conform__ = metamethod(__conform__)
class conformsRegistry(dict):
"""Helper type for objects and classes that need registration support"""
def __call__(self, protocol):
# This only gets called for non-class objects
if protocol in self:
subject = self.subject()
if subject is not None:
return self[protocol][0](subject)
def findImplementation(self, subject, protocol, checkSelf=True):
for cls in getMRO(subject):
conf = cls.__dict__.get('__conform__')
if conf is None:
continue
if not isinstance(conf,conformsRegistry):
raise TypeError(
"Incompatible __conform__ in base class", conf, cls
)
if protocol in conf:
return conf[protocol][0](subject)
def newProtocolImplied(self, srcProto, destProto, adapter, depth):
subject = self.subject()
if subject is None or srcProto not in self:
return
baseAdapter, d = self[srcProto]
adapter = composeAdapters(baseAdapter,srcProto,adapter)
declareAdapterForObject(
destProto, adapter, subject, depth+d
)
def __hash__(self):
# Need this because dictionaries aren't hashable, but we need to
# be referenceable by a weak-key dictionary
return id(self)
def __get__(self,ob,typ=None):
if ob is not None:
raise AttributeError(
"__conform__ registry does not pass to instances"
)
# Return a bound method that adds the retrieved-from class to the
return instancemethod(self.findImplementation, typ, type(typ))
def __getstate__(self):
return self.subject(), self.items()
def __setstate__(self,(subject,items)):
self.clear()
self.update(dict(items))
self.subject = mkRef(subject)
class MiscObjectsAsOpenProvider(object):
"""Supply __conform__ registry for funcs, modules, & classic instances"""
advise(
instancesProvide=[IOpenProvider],
asAdapterForTypes=[
FunctionType, ModuleType, InstanceType, ClassType, type, object
]
)
def __init__(self,ob):
obs = list(getMRO(ob))
for item in obs:
try:
reg = item.__dict__.get('__conform__')
if reg is None and obs==[ob]:
# Make sure we don't obscure a method from the class!
reg = getattr(item,'__conform__',None)
except AttributeError:
raise TypeError(
"Only objects with dictionaries can use this adapter",
ob
)
if reg is not None and not isinstance(reg,conformsRegistry):
raise TypeError(
"Incompatible __conform__ on adapted object", ob, reg
)
reg = ob.__dict__.get('__conform__')
if reg is None:
reg = ob.__conform__ = self.newRegistry(ob)
self.ob = ob
self.reg = reg
def declareProvides(self, protocol, adapter=NO_ADAPTER_NEEDED, depth=1):
if updateWithSimplestAdapter(self.reg, protocol, adapter, depth):
adapt(protocol,IOpenProtocol).addImplicationListener(self.reg)
return True
def newRegistry(self,subject):
# Create a registry that's also set up for inheriting declarations
reg = conformsRegistry()
reg.subject = mkRef(subject)
return reg
|