1   
  2   
  3   
  4   
  5   
  6   
  7   
  8   
  9   
 10   
 11   
 12   
 13   
 14   
 15   
 16   
 17   
 18  """Interface API, minimal implementation. 
 19   
 20  This is minimal Zope Interfaces API implementation, as required by PyXMPP, not add another dependencies. 
 21   
 22  If zope.interface package is available it will be used instead of this one. Never import this module directly.""" 
 23   
 24  __revision__="$Id: utils.py 647 2006-08-26 18:27:39Z jajcus $" 
 25  __docformat__="restructuredtext en" 
 26   
 27  import sys 
 28  from types import FunctionType 
 29   
 37   
 39      for interface in interfaces: 
 40          if not isinstance(interface, InterfaceClass): 
 41              raise TypeError, "Only interfaces may be implemented" 
 42   
 43      frame = sys._getframe(1) 
 44      locals = frame.f_locals 
 45   
 46      if (locals is frame.f_globals) or ('__module__' not in locals): 
 47          raise TypeError, "implements() may only be used in a class definition" 
 48   
 49      if "__implemented__" in locals: 
 50          raise TypeError, "implements() may be used only once" 
 51   
 52      locals["__implemented__"] = tuple(interfaces) 
  53   
 55      yield cls 
 56      for base in cls.__bases__: 
 57          for b in _whole_tree(base): 
 58              yield b 
  59   
 70   
 81   
 83 -    def __init__(self, name, bases = (), attrs = None, __doc__ = None, __module__ = None): 
  84          if __module__ is None: 
 85              if (attrs is not None and ('__module__' in attrs) and isinstance(attrs['__module__'], str)): 
 86                  __module__ = attrs['__module__'] 
 87                  del attrs['__module__'] 
 88              else: 
 89                  __module__ = sys._getframe(1).f_globals['__name__'] 
 90          if __doc__ is not None: 
 91              self.__doc__ = __doc__ 
 92          if attrs is not None and "__doc__" in attrs: 
 93              del attrs["__doc__"] 
 94          self.__module__ = __module__ 
 95          for base in bases: 
 96              if not isinstance(base, InterfaceClass): 
 97                  raise TypeError, 'Interface bases must be Interfaces' 
 98          if attrs is not None: 
 99              for aname, attr in attrs.items(): 
100                  if not isinstance(attr, Attribute) and type(attr) is not FunctionType: 
101                      raise TypeError, 'Interface attributes must be Attributes o functions (%r found in %s)' % (attr, aname) 
102          self.__bases__ = bases 
103          self.__attrs = attrs 
104          self.__name__ = name 
105          self.__identifier__ = "%s.%s" % (self.__module__, self.__name__) 
 106           
108          """Is the interface implemented by an object""" 
109          if self in providedBy(ob): 
110              return True 
111          return False 
 112   
114          """Do instances of the given class implement the interface?""" 
115          return self in implementedBy(cls) 
 116   
118          name = self.__name__ 
119          module = self.__module__ 
120          if module and module != "__main__": 
121              name = "%s.%s" % (module, name) 
122          return "<%s %s>" % (self.__class__.__name__, name)