def test_lookupByRemoteName(self): """Verify that a type can be looked up using the remote name. """ typeClass = annotation.getTypeClassByTypeName( 'test.Change' ) self.assertTrue(typeClass is not None)
def sequenceAttributeAdapter(seq): typeClass = seq.elementType try: return annotation.ArrayList(annotation.getTypeClassByTypeName( classRegistry[typeClass])) except KeyError: #return annotation.ArrayList(igwt.IType(typeClass)) # FIXME - special case for sequence of String if typeClass == String: return annotation.ArrayList(annotation.String) raise
def __new__(meta, className, bases, attributes): # start out by creating the actual class. klass = type.__new__(meta, className, bases, attributes) #print "attrs", repr(attributes) #print "bases", bases #print "register", className names = list() if not '__remote_name__' in attributes: raise Exception("remote name not specified") remote_name = attributes['__remote_name__'] superType = None if len(bases) != 0: superClassNames = list() for base in bases: if hasattr(base, '__remote_name__'): superClassNames.append(base.__remote_name__) if len(superClassNames) > 1: raise Exception("only one base allowed") #print "super class names", superClassNames if superClassNames: superClass = annotation.getTypeClassByTypeName( superClassNames[0] ) superType = superClass() #print "superClass", superType klass.superType = superType def getTypeName(): return remote_name klass.getTypeName = staticmethod(getTypeName) annotation.registerTypeClass(klass) # pick up all remote attributes: for attrname, attrval in attributes.iteritems(): if isinstance(attrval, annotation.RemoteAttribute): names.append(attrname) # fake a zope interface by providing the methods that we # require: names and get. class protocolClass: @staticmethod def names(): return names @staticmethod def get(name): return getattr(protocolClass, name) for name in names: setattr(protocolClass, name, attributes[name]) annotation.typeProtocolRegistry.register( klass, protocolClass ) return klass
def registerRemoteClass(cls, remoteName=None, superClassName=None): """ Register class C{cls} so that it can be identified as remote class C{remoteName}. """ if remoteName is None: try: remoteName = cls.__remote_name__ except AttributeError: pass if remoteName is None: raise Exception("remote name wasn't specified") _superType = None bases = cls.__bases__ if len(bases) != 0: superClassNames = list() if superClassName is not None: superClassNames.append(superClassName) for base in bases: if base in classRegistry: superClassNames.append(classRegistry[base]) if superClassNames: superClass = annotation.getTypeClassByTypeName( superClassNames[0] ) _superType = superClass() else: _superType = annotation.Object() classRegistry[cls] = remoteName #print "superType", _superType class type_class(annotation.Type): @staticmethod def getTypeName(): return remoteName superType = _superType annotation.registerTypeClass(type_class) attributes = {} for attribute in getAttributes(cls, recurse=False): attributes[attribute.name] = attribute print "class", remoteName, "has attributes", attributes.keys() class protocol_class(object): @staticmethod def names(): return sorted(attributes.keys()) @staticmethod def get(name): return annotation.RemoteAttribute( igwt.IType(attributes[name])) #annotation.typeProtocolRegistry.register(cls, protocol_class) annotation.registerTypeProtocol(type_class, protocol_class) class factory_class(object): def __init__(self, protocol_class): pass def buildInstance(self): return cls(_do_not_finalize=True) components.registerAdapter(factory_class, type_class, igwt.IInstanceFactory) annotation.registerTypeAdapter(type_class, cls)
def referenceAttributeAdapter(ref): typeClass = ref.referenceType return annotation.getTypeClassByTypeName(classRegistry[typeClass])