Example #1
0
 def resolve(cls):
     if not cls._resolved:
         try:
             cls._resolved = globals()[cls._referenced]
         except KeyError:
             raise ModelDefinitionError('Undefined Model "{}"'.format(cls._referenced))
     return cls._resolved
Example #2
0
    def __new__(mcs, name, bases, attrs):
        klass = super(ModelMeta, mcs).__new__(mcs, name, bases, attrs)

        model_ref_cls = globals().get('_ModelReference')
        if name == '_ModelReference' or (model_ref_cls
                                         and issubclass(klass, model_ref_cls)):
            setattr(sys.modules[mcs.__module__], name, klass)
            return klass

        # Every model has to be bound to a topic except for the Model base class
        # Since it is using this class as meta class, we will dynamically get the class object from the globals
        model_cls = globals().get('Model')
        if model_cls and issubclass(klass, model_cls):
            topic = getattr(klass, 'topic', None)
            if not topic or not issubclass(topic, Topic):
                raise ModelDefinitionError(
                    'Missing topic in Model {}'.format(name))
            topic.messages = tuple(set(topic.messages + (klass, )))

        kls_attrs = (getattr(klass, 'fields', None) or {}).copy()
        kls_attrs.update({
            name: value
            for name, value in attrs.items()
            if isinstance(value, fields.Field)
        })
        klass.fields = kls_attrs.copy()

        setattr(sys.modules[mcs.__module__], name, klass)
        return klass
Example #3
0
    def __new__(mcs, name, bases, attrs):
        klass = super(ModelMeta, mcs).__new__(mcs, name, bases, attrs)

        # Every model has to be bound to a topic
        if klass.__name__ != 'Model' and issubclass(klass, ModelMeta):
            topic = getattr(klass, 'topic', None)
            if not topic or not issubclass(topic, Topic):
                raise ModelDefinitionError(
                    'Missing topic in Model {}'.format(name))
            topic.messages = tuple(set(topic.messages + (klass, )))

        kls_attrs = {
            name: value
            for name, value in attrs.items()
            if isinstance(value, fields.Field)
        }
        klass.fields = kls_attrs.copy()

        setattr(sys.modules[mcs.__module__], name, klass)
        return klass