X-Git-Url: http://dolda2000.com/gitweb/?p=didex.git;a=blobdiff_plain;f=didex%2Fvalues.py;h=bff26e7c4eae145fc71268695eaf7bcc2861dcf5;hp=a4473f079e19e5ca5556b746180a3f6e95491806;hb=9ef33548b36d539eac18cc28a4fc6835ff2d6ee6;hpb=73761d103993f8ec26bb61d1d405edde9efb5ddd diff --git a/didex/values.py b/didex/values.py index a4473f0..bff26e7 100644 --- a/didex/values.py +++ b/didex/values.py @@ -2,7 +2,7 @@ import threading from . import store, lib, index from .store import storedesc -__all__ = ["simple", "multi", "compound"] +__all__ = ["simple", "multi", "compound", "idlink"] class cursor(lib.closable): def __init__(self, bk, st): @@ -162,3 +162,21 @@ class compound(base): idx.remove(ival, id, tx=tx) idx.put(val, id, tx=tx) tx.postcommit(lambda: setattr(obj, self.iattr, val)) + +class idlink(object): + def __init__(self, name, atype): + self.atype = atype + self.battr = "__idlink_%s" % name + + def __get__(self, obj, cls): + if obj is None: return self + ret = self.atype.store.get(getattr(obj, self.battr)) + assert isinstance(ret, self.atype) + return ret + + def __set__(self, obj, val): + assert isinstance(val, self.atype) + setattr(obj, self.battr, val.id) + + def __delete__(self, obj): + delattr(obj, self.battr)