Registed datastore environments with atexit.
[didex.git] / didex / store.py
index 8008ee5..a9bf0cd 100644 (file)
@@ -1,8 +1,8 @@
-import threading, pickle
+import threading, pickle, inspect, atexit
 from . import db, index, cache
 from .db import txnfun
 
-__all__ = ["environment", "store", "autostore"]
+__all__ = ["environment", "datastore", "autostore"]
 
 class environment(object):
     def __init__(self, *, path=None, getpath=None, recover=False):
@@ -22,11 +22,13 @@ class environment(object):
                 if self.path is None:
                     self.path = self.getpath()
                 self.bk = db.environment(self.path, recover=self.recover)
+                atexit.register(self.close)
             return self.bk
 
     def close(self):
         with self.lk:
             if self.bk is not None:
+                atexit.unregister(self.close)
                 self.bk.close()
                 self.bk = None
 
@@ -35,16 +37,17 @@ class storedesc(object):
 
 def storedescs(obj):
     t = type(obj)
-    ret = getattr(t, "__didex_attr", None)
+    ret = t.__dict__.get("__didex_attr")
     if ret is None:
         ret = []
-        for nm, val in t.__dict__.items():
-            if isinstance(val, storedesc):
-                ret.append((nm, val))
+        for st in inspect.getmro(t):
+            for nm, val in st.__dict__.items():
+                if isinstance(val, storedesc):
+                    ret.append((nm, val))
         t.__didex_attr = ret
     return ret
 
-class store(object):
+class datastore(object):
     def __init__(self, name, *, env=None, path=".", ncache=None):
         self.name = name
         self.lk = threading.Lock()
@@ -107,6 +110,7 @@ class autotype(type):
     def __call__(self, *args, **kwargs):
         new = super().__call__(*args, **kwargs)
         new.id = self.store.register(new)
+        self.store.update(new.id, vfy=new) # This doesn't feel too nice.
         return new
 
 class autostore(object, metaclass=autotype):