Commits

Sean Summers  committed 8070409

cleaned up api

  • Participants
  • Parent commits 36c9556

Comments (0)

Files changed (1)

File db4o/db4o.py

 import com.db4o.config.ConfigScope
 import com.db4o.Db4oEmbedded.newConfiguration
 import com.db4o.Db4oEmbedded.openFile
-import com.db4o.internal.StoredClassImpl
-import com.db4o.reflect.generic.GenericObject
 
 def readonlyConfiguration():
     ''' create a read only DB4O configuration object (to feed db()) '''
     return config
 
 def db(db4ofilename, db4oConfig = None):
-    ''' return db4o container for a file, optionally with a configuration (readonly, if not provided) '''
+    ''' return db4o container for a file, optionally with a configuration (readonly, if not provided)
+        db4o container has the following:
+        .storedClasses -- all classes known to the container
+        .storedClass(classname) -- returns StoredClass for a given string
+         |.storedFields for classFields
+         |.parentStoredClass for immediate parent class
+         L.IDs for a list of all instance IDs
+    '''
     if not db4oConfig: db4oConfig = readonlyConfiguration()
     return com.db4o.Db4oEmbedded.openFile(db4oConfig, db4ofilename)
 
-def dbClasses(db, blacklist = BLACKLIST):
-    ''' generate StoredClass list for a given db4o container, filtering out blacklist '''
+def all(db):
+    ''' return all storedClass instances in this container
+        .IDs for all instance IDs
+    '''
+    return db.queryByExample(None)
+
+def objCount(db, storedClass = None):
+    ''' return a count of objects in this container, for a specified class (or all) '''
+    return len(storedClass.IDs if storedClass else all(db).IDs)
+
+def byID(db, id, depth = 2):
+    ''' return an activated object by ID '''
+    o = db.getByID(id)
+    db.activate(o, depth)
+    return o
+
+def userClasses(db, blacklist = BLACKLIST):
+    ''' generate StoredClass list for a given db4o container, filtering out system/jvm classes '''
     return (c for c in db.storedClasses() if not c.name.startswith(blacklist))
 
-def objClass(db, o):
-    ''' return StoredClass for a given class name '''
-    return o if isinstance(o, com.db4o.internal.StoredClassImpl) else db.storedClass(o)
+def path(classObject):
+    ''' generate full class history '''
+    c = getattr(classObject, 'genericClass', classObject)
+    while c.declaredFields if hasattr(c, 'declaredFields') else hasattr(c, 'storedFields'):
+        yield c
+        c = getattr(c, 'superclass', getattr(c, 'parentStoredClass', None))
 
-def objClassFields(db, o):
-    ''' return instance fields for a given StoredClass '''
-    return objClass(db, o).storedFields
+def fields(classObject):
+    ''' return all fields for a given StoredClass (including parent fields [heritage/index order]) '''
+    fields = []
+    for cls in reversed(tuple(path(classObject))):
+        fields.extend(getattr(cls, 'declaredFields', getattr(cls,'storedFields', None)))
+    return fields
 
-def objClasspath(db, o):
-    ''' generate full class lineage for a given StoredClass '''
-    c = objClass(db, o)
-    while c:
-        yield c
-        c = c.parentStoredClass
+def valueOf(field, instance):
+    ''' given a field and instance, return a tuple of values, recursing if necessary '''
+    v = field.get(instance)
+    try:
+        if v.class.name.startswith('com.db4o.reflect.generic.'): # com.db4o.reflect.generic.GenericObject
+            v = tuple(valueOf(f, v) for f in v.genericClass.declaredFields)
+    except AttributeError: pass
+    return (field.name, v)
 
-def objFields(db, o):
-    ''' return all fields for a given StoredClass (including parent fields [heritage order]) '''
-    fields = []
-    [fields.extend(cls.storedFields) for cls in reversed(tuple(objClasspath(db, o)))]
-    return tuple(fields)
+def objTuple(instance):
+    ''' return a tuple for an object, suitable for dict()
+        input is (com.db4o.internal.StoredClassImpl, com.db4o.reflect.generic.GenericObject) from byID
+    '''
+    return [valueOf(f, instance) for f in fields(instance)]
 
-def objByID(db, oid, depth = 2):
-    ''' return an activated object by ID '''
-    obj = db.getByID(oid)
-    db.activate(obj, depth)
-    return obj
-
-def objAll(db):
-    ''' return all objects stored in this container '''
-    return db.queryByExample(None)
-
-def objIDs(db, o = None):
-    ''' return a list of all object IDs in this container, for a specified class (or all) '''
-    return objClass(db, o).IDs
-    
-def objCount(db, o = None):
-    ''' return a count of objects in this container, for a specified class (or all) '''
-    return len(objIDs(db, o) if o else objAll(db).IDs)
-
-def fieldValue(field, obj):
-    ''' given a field and object, return a tuple of values, recursing if necessary '''
-    value = field.get(obj)
-    if isinstance(value, com.db4o.reflect.generic.GenericObject):
-        value = tuple(fieldValue(f, value) for f in value.genericClass.declaredFields)
-    return (field.name, value)
-
-def objTuple(db, o):
-    ''' return a tuple for an object, suitable for dict() '''
-    if not isinstance(o, (com.db4o.internal.StoredClassImpl, com.db4o.reflect.generic.GenericObject)):
-        o = objByID(db, o)
-    return [fieldValue(f, o) for f in objFields(db, o)]
-