Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorBenjamin Saller <bcsaller@objectrealms.net>2007-07-16 22:47:36 (GMT)
committer Benjamin Saller <bcsaller@objectrealms.net>2007-07-16 22:47:36 (GMT)
commitd26583e7d16cc3d91dfd58f8dced8969a5425d64 (patch)
treea76dcb501ca64b6eb7912505501774e1fd402229
parentd750a77ffeb6da5ecd61824f7a8653b0c0ba68e9 (diff)
relax model restriction by defaulting new keys as strings or recording their types
-rw-r--r--src/olpc/datastore/model.py20
-rw-r--r--src/olpc/datastore/xapianindex.py7
-rw-r--r--tests/test_model.py24
3 files changed, 45 insertions, 6 deletions
diff --git a/src/olpc/datastore/model.py b/src/olpc/datastore/model.py
index fbad65f..b6e0829 100644
--- a/src/olpc/datastore/model.py
+++ b/src/olpc/datastore/model.py
@@ -108,19 +108,31 @@ class Model(object):
m.fieldnames = self.fieldnames[:]
return m
- def fromstring(self, key, value):
+ def fromstring(self, key, value, allowAddition=False):
"""create a property from the key name by looking it up in the
model."""
kind = None
if ':' in key: key, kind = key.split(':', 1)
-
- mkind = self.fields[key][1]
+ added = False
+ field = self.fields.get(key)
+ if field: mkind = field[1]
+ elif allowAddition:
+ # create a new field, this will force a change in the
+ # model
+ # and in turn should add a new field action
+ if not kind: kind = "string"
+ self.addField(key,kind)
+ mkind = kind
+ added = True
+ else:
+ raise KeyError("no field specification for %s" % key)
+
if kind and mkind:
if kind != mkind: raise ValueError("""Specified wire
encoding for property %s was %s, expected %s""" %(key, kind, mkind))
kind = mkind
- return Property(key, value, kind)
+ return Property(key, value, kind), added
def addField(self, key, kind, overrides=None):
diff --git a/src/olpc/datastore/xapianindex.py b/src/olpc/datastore/xapianindex.py
index 7effc4c..49448d7 100644
--- a/src/olpc/datastore/xapianindex.py
+++ b/src/olpc/datastore/xapianindex.py
@@ -236,7 +236,9 @@ class IndexManager(object):
"""
d = {}
for k,v in props.iteritems():
- p = self.datamodel.fromstring(k, v)
+ p, added = self.datamodel.fromstring(k, v,
+ allowAddition=True)
+ if added is True: self.fields.add(p.key)
d[p.key] = p
return d
@@ -245,6 +247,7 @@ class IndexManager(object):
Props must contain the following:
key -> Property()
"""
+ operation = UPDATE
#
# Version handling
#
@@ -265,7 +268,7 @@ class IndexManager(object):
doc = secore.UnprocessedDocument()
add = doc.fields.append
fp = None
- operation = UPDATE
+
filestuff = None
if filename:
diff --git a/tests/test_model.py b/tests/test_model.py
index dc49dac..618c5cb 100644
--- a/tests/test_model.py
+++ b/tests/test_model.py
@@ -92,6 +92,30 @@ class Test(unittest.TestCase):
c = ds.get(uid)
assert c.get_property('keep') == 0
+ ds.stop()
+
+ def test_randomproperty(self):
+ # specifying items not in the model, with and w/o type
+ # qualifiers
+ ds = DataStore()
+ ds.registerBackend(backingstore.FileBackingStore)
+
+ ds.mount(DEFAULT_STORE)
+
+ uid = ds.create({'title' : 'Random Extras', 'foobar' : 'baz',
+ 'incept:date' : datetime.datetime.now().isoformat()})
+
+ ds.complete_indexing()
+
+ ds.update(uid, {'title' : 'Random Extras the sequel', 'foobar' : 'whodofoodo',
+ 'incept:date' : datetime.datetime.now().isoformat()})
+
+ ds.complete_indexing()
+
+ assert ds.find('whodofoodo')[1] == 1
+
+ ds.stop()
+
def test_suite():
suite = unittest.TestSuite()
suite.addTest(unittest.makeSuite(Test))