6.2.3.2. Source

Start python section to interscript/core/protocols.py[1 /1 ]
     1: #line 38 "protocols.ipk"
     2: #-------------- protocol.py ---------------------------
     3: import types
     4: 
     5: type_protocols = {
     6:   types.NoneType : [],
     7:   types.TypeType : ['type','immutable'],
     8:   types.IntType : ['integer','number','immutable'],
     9:   types.LongType : ['integer','number','immutable'],
    10:   types.FloatType : ['number','immutable'],
    11:   types.StringType : ['string','immutable','filename','url'],
    12:   types.TupleType : ['sequence','immutable'],
    13:   types.ListType : ['sequence','mutable'],
    14:   types.DictType : ['map','mutable'],
    15:   types.FunctionType : ['function'],
    16:   types.LambdaType : ['function'],
    17:   types.CodeType : ['code'],
    18:   types.ClassType : ['class'],
    19:   types.InstanceType : ['instance'],
    20:   types.MethodType : ['function'],
    21:   types.BuiltinFunctionType: ['function'],
    22:   types.ModuleType: ['module'],
    23:   types.FileType: ['file'],
    24:   types.XRangeType: ['range'],
    25:   types.TracebackType: ['traceback'],
    26:   types.FrameType: ['frame'],
    27:   types.SliceType: ['slice'],
    28:   types.EllipsisType: ['ellipsis']
    29: }
    30: try:
    31:   type_protocols[types.ComplexType]='number'
    32: except NameError:
    33:   pass
    34: 
    35: class provides_attr:
    36:   def __init__(self,name):
    37:     self.name = name
    38: 
    39: def isclass(obj):
    40:   return type(obj) is types.ClassType
    41: 
    42: def isinstancetype(obj):
    43:   return type(obj) is types.InstanceType
    44: 
    45: def classof(obj):
    46:   if isinstancetype(obj):
    47:     return obj.__class__
    48:   else:
    49:     return None
    50: 
    51: def add_obj_proto(object,protocol):
    52:   if hasattr(object,'__protocols__'):
    53:     getattr(object,'__protocols__').append(protocol)
    54:   else:
    55:     setattr(object,'__protocols__',[protocol])
    56: 
    57: def add_obj_protos(object,protocols):
    58:   for p in protocols: add_obj_protos(object,p)
    59: 
    60: def add_class_proto(cls,protocol):
    61:   if hasattr(cls,'__class_protocols__'):
    62:     getattr(cls,'__class_protocols__').append(protocol)
    63:   else:
    64:     setattr(cls,'__class_protocols__',[protocol])
    65: 
    66: def add_class_protos(object,protocols):
    67:   for p in protocols: add_class_protos(object,p)
    68: 
    69: def add_type_protos(object,protocols):
    70:   for p in protocols: add_type_protos(object,p)
    71: 
    72: def add_type_proto(typ, protocol):
    73:   if type_protocols.has_key(typ):
    74:     type_protocols[typ].append(protocol)
    75:   else:
    76:     type_protocols[typ] = [protocol]
    77: 
    78: def has_class_proto(cls,protocol):
    79:   if cls is protocol: return 1
    80:   if hasattr(cls,'__class_protocols__'):
    81:     if protocol in getattr(cls,'__class_protocols__'): return 1
    82:   return 0
    83: 
    84: def has_type_proto(object,protocol):
    85:   typ = type(object)
    86:   if typ is protocol: return 1
    87:   if type_protocols.has_key(typ):
    88:     if protocol in type_protocols[typ]: return 1
    89:   return 0
    90: 
    91: def has_protocol(object,protocol):
    92:   if hasattr(object,'__protocols__'):
    93:     if protocol in getattr(object,'__protocols__'): return 1
    94: 
    95:   cls = classof(object)
    96:   if cls:
    97:     v = has_class_proto(cls, protocol)
    98:     if v: return 1
    99:     for base in cls.__bases__:
   100:       if has_class_proto(base,protocol): return 1
   101:   if has_type_proto(object,protocol): return 1
   102:   if type(protocol) is types.InstanceType:
   103:     if protocol.__class__ is provides_attr:
   104:       if hasattr(object,protocol.name): return 1
   105:   return 0
   106: 
   107: def has_protocols(object,protocols):
   108:   for p in protocols:
   109:     if not has_protocol(object,p): return 0
   110:   return 1
   111: 
End python section to interscript/core/protocols.py[1]