def new(identity, value_info, getter=None, setter=None, deleter=None, meta=None): cls_name = utils.mk_class_name(identity, "Attribute") cls = MetaAttribute(cls_name, (_DynAttribute, ), {"__slots__": ()}) cls.annotate_identity(identity) cls.annotate_value(value_info) cls.apply_class_meta(meta) if getter is not None: Action = action.MetaAction.new("get." + identity, ActionCategories.retrieve, effects=[getter], result_info=value_info, is_idempotent=True) cls.annotate_action(u"get", Action) if setter is not None: def _set_attribute(value, context): d = setter(value, context) # attribute setter return the validate value d.addCallback(defer.override_result, value) return d Action = action.MetaAction.new("set." + identity, ActionCategories.update, effects=[_set_attribute], value_info=value_info, result_info=value_info, is_idempotent=True) cls.annotate_action(u"set", Action) if deleter is not None: Action = action.MetaAction.new('delete.' + identity, ActionCategories.delete, effects=[ deleter, effect.static_value('')], result_info=value_module.String(), is_idempotent=True) cls.annotate_action(u"del", Action) return cls
def model_from_dict(values, source, type_name): result = dict() if not values: return result for name, v in values.iteritems(): v_info = None if isinstance(v, int): v_info = value.Integer() elif isinstance(v, (str, unicode)): v_info = value.String() if v_info: iden = type_name + '.' + name Attribute = attribute.MetaAttribute.new( iden, v_info, getter=effect.static_value(v)) result[name] = Attribute(source) return result