home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2009 June / maximum-cd-2009-06.iso / DiscContents / digsby_setup.exe / lib / protocols / twisted_support.pyo (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2009-02-26  |  3.9 KB  |  95 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyo (Python 2.5)
  3.  
  4. __all__ = []
  5. from adapters import *
  6. from api import advise
  7. from interfaces import IOpenProtocol
  8. from weakref import WeakKeyDictionary
  9.  
  10. class TwistedAdaptMethod(object):
  11.     __slots__ = 'iface'
  12.     
  13.     def __init__(self, iface):
  14.         self.iface = iface
  15.  
  16.     
  17.     def __call__(self, obj):
  18.         if TwistedImplements(obj, self.iface):
  19.             return obj
  20.         
  21.         return self.iface(obj, None)
  22.  
  23.     
  24.     def im_func(self, ob, default):
  25.         meth = self.iface.__dict__.get('__adapt__')
  26.         if meth is None:
  27.             return default
  28.         
  29.         return meth(ob, default)
  30.  
  31.  
  32.  
  33. try:
  34.     from twisted.python.components import implements as TwistedImplements, MetaInterface as TwistedInterfaceClass, getInterfaces as TwistedGetInterfaces
  35. except ImportError:
  36.     TwistedInterfaceTypes = []
  37.  
  38. TwistedInterfaceClass.__adapt__ = property((lambda s: TwistedAdaptMethod(s)))
  39. TwistedInterfaceTypes = [
  40.     TwistedInterfaceClass]
  41.  
  42. class TwistedInterfaceAsProtocol(object):
  43.     __slots__ = 'iface'
  44.     advise(instancesProvide = [
  45.         IOpenProtocol], asAdapterForTypes = TwistedInterfaceTypes)
  46.     
  47.     def __init__(self, iface):
  48.         self.iface = iface
  49.  
  50.     
  51.     def __adapt__(self, obj):
  52.         return self.iface.__adapt__(obj)
  53.  
  54.     
  55.     def registerImplementation(self, klass, adapter = NO_ADAPTER_NEEDED, depth = 1):
  56.         oldImplements = TwistedGetInterfaces(klass)
  57.         if adapter is NO_ADAPTER_NEEDED:
  58.             klass.__implements__ = (self.iface, tuple(oldImplements))
  59.         elif adapter is DOES_NOT_SUPPORT:
  60.             if self.iface in oldImplements:
  61.                 oldImplements.remove(self.iface)
  62.                 klass.__implements__ = tuple(oldImplements)
  63.             
  64.         else:
  65.             raise TypeError('Twisted interfaces can only declare support, not adapters', self.iface, klass, adapter)
  66.  
  67.     
  68.     def addImpliedProtocol(self, proto, adapter = NO_ADAPTER_NEEDED, depth = 1):
  69.         iface = self.iface
  70.         (None, self.iface.adaptWith)((lambda o: adapter(o, iface)), proto)
  71.         listeners = iface.__dict__.get('_Protocol__listeners', { })
  72.         for listener in listeners.keys():
  73.             listener.newProtocolImplied(self, proto, adapter, depth)
  74.         
  75.  
  76.     
  77.     def registerObject(self, ob, adapter = NO_ADAPTER_NEEDED, depth = 1):
  78.         oldImplements = TwistedGetInterfaces(ob)
  79.         if adapter is NO_ADAPTER_NEEDED:
  80.             ob.__implements__ = (self.iface, tuple(oldImplements))
  81.         elif adapter is DOES_NOT_SUPPORT:
  82.             if self.iface in oldImplements:
  83.                 oldImplements.remove(self.iface)
  84.                 ob.__implements__ = tuple(oldImplements)
  85.             
  86.         else:
  87.             raise TypeError('Twisted interfaces can only declare support, not adapters', self.iface, ob, adapter)
  88.  
  89.     
  90.     def addImplicationListener(self, listener):
  91.         listeners = self.iface.__dict__.setdefault('_Protocol__listeners', WeakKeyDictionary())
  92.         listeners[listener] = True
  93.  
  94.  
  95.