home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2011 October / maximum-cd-2011-10.iso / DiscContents / digsby_setup.exe / lib / protocols / generate.pyo (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2011-06-22  |  5.7 KB  |  178 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyo (Python 2.6)
  3.  
  4. from interfaces import Protocol, allocate_lock, Interface
  5. from advice import metamethod, supermeta
  6. from api import declareAdapterForProtocol, declareAdapterForType
  7. from api import declareAdapter, adapt
  8. from adapters import NO_ADAPTER_NEEDED
  9. __all__ = [
  10.     'protocolForType',
  11.     'protocolForURI',
  12.     'sequenceOf',
  13.     'IBasicSequence',
  14.     'URIProtocol',
  15.     'TypeSubset',
  16.     'WeakSubset',
  17.     'ADAPT_SEQUENCE',
  18.     'SequenceProtocol']
  19.  
  20. class URIProtocol(Protocol):
  21.     
  22.     def __init__(self, uri):
  23.         self.URI = uri
  24.         Protocol.__init__(self)
  25.  
  26.     
  27.     def __repr__(self):
  28.         return 'URIProtocol(%r)' % self.URI
  29.  
  30.     
  31.     def __reduce__(self):
  32.         return (protocolForURI, (self.URI,))
  33.  
  34.  
  35.  
  36. class TypeSubset(Protocol):
  37.     
  38.     def __init__(self, baseType, methods):
  39.         self.baseType = baseType
  40.         self.methods = methods
  41.         Protocol.__init__(self)
  42.  
  43.     
  44.     def __repr__(self):
  45.         return 'TypeSubset(%r,%r)' % (self.baseType, self.methods)
  46.  
  47.     
  48.     def __reduce__(self):
  49.         return (protocolForType, (self.baseType, self.methods, False))
  50.  
  51.  
  52.  
  53. class WeakSubset(TypeSubset, object):
  54.     __metaclass__ = type
  55.     
  56.     def __adapt__(self, ob):
  57.         result = supermeta(TypeSubset, self).__adapt__(ob)
  58.         if result is not None:
  59.             return result
  60.         for name in self.methods:
  61.             if not hasattr(ob, name):
  62.                 return None
  63.         else:
  64.             return ob
  65.         return hasattr(ob, name)
  66.  
  67.     __adapt__ = metamethod(__adapt__)
  68.     
  69.     def __repr__(self):
  70.         return 'WeakSubset(%r,%r)' % (self.baseType, self.methods)
  71.  
  72.     
  73.     def __reduce__(self):
  74.         return (protocolForType, (self.baseType, self.methods, True))
  75.  
  76.  
  77.  
  78. class SequenceProtocol(Protocol):
  79.     
  80.     def __init__(self, baseProtocol):
  81.         self.baseProtocol = baseProtocol
  82.         Protocol.__init__(self)
  83.  
  84.     
  85.     def __repr__(self):
  86.         return 'sequenceOf(%r)' % self.baseProtocol
  87.  
  88.     
  89.     def __reduce__(self):
  90.         return (sequenceOf, (self.baseProtocol,))
  91.  
  92.  
  93.  
  94. class IBasicSequence(Interface):
  95.     
  96.     def __iter__():
  97.         pass
  98.  
  99.  
  100. declareAdapter(NO_ADAPTER_NEEDED, provides = [
  101.     IBasicSequence], forTypes = [
  102.     list,
  103.     tuple])
  104.  
  105. def ADAPT_SEQUENCE(ob, proto):
  106.     marker = object()
  107.     out = []
  108.     proto = proto.baseProtocol
  109.     for item in ob:
  110.         item = adapt(item, proto, marker)
  111.         if item is marker:
  112.             return None
  113.         out.append(item)
  114.     
  115.     return out
  116.  
  117. __registryLock = allocate_lock()
  118. registry = { }
  119.  
  120. def protocolForURI(uri):
  121.     __registryLock.acquire()
  122.     
  123.     try:
  124.         return registry[uri]
  125.     except KeyError:
  126.         proto = registry[uri] = URIProtocol(uri)
  127.         return proto
  128.     finally:
  129.         __registryLock.release()
  130.  
  131.  
  132.  
  133. def protocolForType(baseType, methods = (), implicit = False):
  134.     methods = []([ (k, k) for k in methods ]).keys()
  135.     methods.sort()
  136.     methods = tuple(methods)
  137.     key = (baseType, methods, not (not implicit))
  138.     return __protocolForType(key)
  139.  
  140.  
  141. def sequenceOf(baseProtocol):
  142.     key = (sequenceOf, baseProtocol)
  143.     __registryLock.acquire()
  144.     
  145.     try:
  146.         return registry[key]
  147.     except KeyError:
  148.         proto = registry[key] = SequenceProtocol(baseProtocol)
  149.         declareAdapterForProtocol((proto,), (lambda o: ADAPT_SEQUENCE(o, proto)), IBasicSequence)
  150.         return proto
  151.     finally:
  152.         __registryLock.release()
  153.  
  154.  
  155.  
  156. def __protocolForType(key):
  157.     __registryLock.acquire()
  158.     
  159.     try:
  160.         return registry[key]
  161.     except KeyError:
  162.         (baseType, methods, implicit) = key
  163.         if implicit:
  164.             proto = WeakSubset(baseType, methods)
  165.         else:
  166.             proto = TypeSubset(baseType, methods)
  167.         registry[key] = proto
  168.     finally:
  169.         __registryLock.release()
  170.  
  171.     if implicit:
  172.         impliedBy = __protocolForType((baseType, methods, False))
  173.         declareAdapterForProtocol(proto, NO_ADAPTER_NEEDED, impliedBy)
  174.     
  175.     declareAdapterForType(proto, NO_ADAPTER_NEEDED, baseType)
  176.     return proto
  177.  
  178.