Add activity objects to the data store
This commit is contained in:
parent
e023d1c345
commit
e586cd66c0
@ -20,6 +20,7 @@ import dbus, dbus.glib, gobject
|
|||||||
import logging
|
import logging
|
||||||
import sqlite
|
import sqlite
|
||||||
import dbus_helpers
|
import dbus_helpers
|
||||||
|
import string
|
||||||
|
|
||||||
have_sugar = False
|
have_sugar = False
|
||||||
try:
|
try:
|
||||||
@ -28,6 +29,21 @@ try:
|
|||||||
except ImportError:
|
except ImportError:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
def is_hex(s):
|
||||||
|
return s.strip(string.hexdigits) == ''
|
||||||
|
|
||||||
|
ACTIVITY_ID_LEN = 40
|
||||||
|
|
||||||
|
def validate_activity_id(actid):
|
||||||
|
"""Validate an activity ID."""
|
||||||
|
if not isinstance(actid, str) and not isinstance(actid, unicode):
|
||||||
|
return False
|
||||||
|
if len(actid) != ACTIVITY_ID_LEN:
|
||||||
|
return False
|
||||||
|
if not is_hex(actid):
|
||||||
|
return False
|
||||||
|
return True
|
||||||
|
|
||||||
|
|
||||||
_DS_SERVICE = "org.laptop.sugar.DataStore"
|
_DS_SERVICE = "org.laptop.sugar.DataStore"
|
||||||
_DS_DBUS_INTERFACE = "org.laptop.sugar.DataStore"
|
_DS_DBUS_INTERFACE = "org.laptop.sugar.DataStore"
|
||||||
@ -75,9 +91,21 @@ class DataStoreDBusHelper(dbus.service.Object):
|
|||||||
return _create_op(self._parent.get(uid))
|
return _create_op(self._parent.get(uid))
|
||||||
|
|
||||||
@dbus.service.method(_DS_DBUS_INTERFACE,
|
@dbus.service.method(_DS_DBUS_INTERFACE,
|
||||||
in_signature="aya{sv}", out_signature="o")
|
in_signature="s", out_signature="o")
|
||||||
def create(self, data, prop_dict):
|
def getActivityObject(self, activity_id):
|
||||||
uid = self._parent.create(data, prop_dict)
|
if not validate_activity_id(activity_id):
|
||||||
|
raise ValueError("invalid activity id")
|
||||||
|
return _create_op(self._parent.get_activity_object(activity_id))
|
||||||
|
|
||||||
|
@dbus.service.method(_DS_DBUS_INTERFACE,
|
||||||
|
in_signature="aya{sv}s", out_signature="o")
|
||||||
|
def create(self, data, prop_dict, activity_id):
|
||||||
|
if len(activity_id):
|
||||||
|
if not validate_activity_id(activity_id):
|
||||||
|
raise ValueError("invalid activity id")
|
||||||
|
else:
|
||||||
|
activity_id = None
|
||||||
|
uid = self._parent.create(data, prop_dict, activity_id)
|
||||||
return _create_op(uid)
|
return _create_op(uid)
|
||||||
|
|
||||||
@dbus.service.method(_DS_DBUS_INTERFACE,
|
@dbus.service.method(_DS_DBUS_INTERFACE,
|
||||||
@ -181,6 +209,7 @@ class DataStore(object):
|
|||||||
self._dbcx.commit()
|
self._dbcx.commit()
|
||||||
curs.execute('CREATE TABLE objects (' \
|
curs.execute('CREATE TABLE objects (' \
|
||||||
'uid INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, ' \
|
'uid INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, ' \
|
||||||
|
'activity_id VARCHAR(50), ' \
|
||||||
'data BLOB' \
|
'data BLOB' \
|
||||||
');')
|
');')
|
||||||
curs.execute('CREATE TABLE properties (' \
|
curs.execute('CREATE TABLE properties (' \
|
||||||
@ -202,10 +231,24 @@ class DataStore(object):
|
|||||||
return uid
|
return uid
|
||||||
raise NotFoundError("Object %d was not found." % uid)
|
raise NotFoundError("Object %d was not found." % uid)
|
||||||
|
|
||||||
def create(self, data, prop_dict=None):
|
def get_activity_object(self, activity_id):
|
||||||
|
curs = self._dbcx.cursor()
|
||||||
|
curs.execute("SELECT uid FROM objects WHERE activity_id='%s';" % activity_id)
|
||||||
|
res = curs.fetchall()
|
||||||
|
self._dbcx.commit()
|
||||||
|
if len(res) > 0:
|
||||||
|
del curs
|
||||||
|
return res[0][0]
|
||||||
|
del curs
|
||||||
|
raise NotFoundError("Object for activity %s was not found." % activity_id)
|
||||||
|
|
||||||
|
def create(self, data, prop_dict=None, activity_id=None):
|
||||||
curs = self._dbcx.cursor()
|
curs = self._dbcx.cursor()
|
||||||
data = sqlite.encode(_get_data_as_string(data))
|
data = sqlite.encode(_get_data_as_string(data))
|
||||||
curs.execute("INSERT INTO objects (uid, data) VALUES (NULL, '%s');" % data)
|
if not activity_id:
|
||||||
|
curs.execute("INSERT INTO objects (uid, data) VALUES (NULL, '%s');" % data)
|
||||||
|
else:
|
||||||
|
curs.execute("INSERT INTO objects (uid, data, activity_id) VALUES (NULL, '%s', '%s');" % (data, activity_id))
|
||||||
curs.execute("SELECT last_insert_rowid();")
|
curs.execute("SELECT last_insert_rowid();")
|
||||||
rows = curs.fetchall()
|
rows = curs.fetchall()
|
||||||
self._dbcx.commit()
|
self._dbcx.commit()
|
||||||
|
@ -15,6 +15,7 @@
|
|||||||
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
||||||
|
|
||||||
import dbus, dbus.glib, gobject
|
import dbus, dbus.glib, gobject
|
||||||
|
from sugar import util
|
||||||
|
|
||||||
class ObjectCache(object):
|
class ObjectCache(object):
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
@ -159,11 +160,27 @@ class DataStore(gobject.GObject):
|
|||||||
# FIXME
|
# FIXME
|
||||||
pass
|
pass
|
||||||
|
|
||||||
def get(self, uid):
|
def get(self, uid=None, activity_id=None):
|
||||||
return self._new_object(self._ds.get(int(uid)))
|
if not activity_id and not uid:
|
||||||
|
raise ValueError("At least one of activity_id or uid must be specified")
|
||||||
|
if activity_id and uid:
|
||||||
|
raise ValueError("Only one of activity_id or uid can be specified")
|
||||||
|
if activity_id:
|
||||||
|
if not util.validate_activity_id(activity_id):
|
||||||
|
raise ValueError("activity_id must be valid")
|
||||||
|
return self._new_object(self._ds.getActivityObject(activity_id))
|
||||||
|
elif uid:
|
||||||
|
if not len(uid):
|
||||||
|
raise ValueError("uid must be valid")
|
||||||
|
return self._new_object(self._ds.get(int(uid)))
|
||||||
|
raise RuntimeError("At least one of activity_id or uid must be specified")
|
||||||
|
|
||||||
def create(self, data, prop_dict={}):
|
def create(self, data, prop_dict={}, activity_id=None):
|
||||||
op = self._ds.create(dbus.ByteArray(data), dbus.Dictionary(prop_dict))
|
if activity_id and not util.validate_activity_id(activity_id):
|
||||||
|
raise ValueError("activity_id must be valid")
|
||||||
|
if not activity_id:
|
||||||
|
activity_id = ""
|
||||||
|
op = self._ds.create(dbus.ByteArray(data), dbus.Dictionary(prop_dict), activity_id)
|
||||||
return self._new_object(op)
|
return self._new_object(op)
|
||||||
|
|
||||||
def delete(self, obj):
|
def delete(self, obj):
|
||||||
|
@ -17,6 +17,7 @@
|
|||||||
|
|
||||||
import unittest
|
import unittest
|
||||||
from sugar.datastore import datastore
|
from sugar.datastore import datastore
|
||||||
|
from sugar import util
|
||||||
import dbus
|
import dbus
|
||||||
|
|
||||||
class NotFoundError(dbus.DBusException): pass
|
class NotFoundError(dbus.DBusException): pass
|
||||||
@ -26,8 +27,8 @@ _ds = datastore.get_instance()
|
|||||||
class DataStoreTestCase(unittest.TestCase):
|
class DataStoreTestCase(unittest.TestCase):
|
||||||
_TEST_DATA = "adsfkjadsfadskjasdkjf"
|
_TEST_DATA = "adsfkjadsfadskjasdkjf"
|
||||||
_TEST_PROPS = {'foo': 1, 'bar': 'baz'}
|
_TEST_PROPS = {'foo': 1, 'bar': 'baz'}
|
||||||
def _create_test_object(self):
|
def _create_test_object(self, activity_id=None):
|
||||||
obj = _ds.create(self._TEST_DATA, self._TEST_PROPS)
|
obj = _ds.create(self._TEST_DATA, self._TEST_PROPS, activity_id=activity_id)
|
||||||
self.assert_(obj)
|
self.assert_(obj)
|
||||||
return obj
|
return obj
|
||||||
|
|
||||||
@ -36,6 +37,31 @@ class DataStoreTestCase(unittest.TestCase):
|
|||||||
self.assert_(obj.uid())
|
self.assert_(obj.uid())
|
||||||
_ds.delete(obj)
|
_ds.delete(obj)
|
||||||
|
|
||||||
|
def testObjectCreateWithActivityId(self):
|
||||||
|
# Try known good create
|
||||||
|
act_id = util.unique_id('afdkjakjddasf')
|
||||||
|
obj = self._create_test_object(act_id)
|
||||||
|
self.assert_(obj.uid())
|
||||||
|
_ds.delete(obj)
|
||||||
|
|
||||||
|
def testObjectCreateWithBadActivityId(self):
|
||||||
|
# try malformed activity id
|
||||||
|
try:
|
||||||
|
uid = self._create_test_object("adfadf")
|
||||||
|
except ValueError:
|
||||||
|
pass
|
||||||
|
else:
|
||||||
|
self.fail("Expected ValueError")
|
||||||
|
|
||||||
|
def testObjectGetActivityObject(self):
|
||||||
|
# create a new object
|
||||||
|
act_id = util.unique_id('afdkjakjddasf')
|
||||||
|
obj = self._create_test_object(act_id)
|
||||||
|
self.assert_(obj.uid())
|
||||||
|
obj2 = _ds.get(activity_id=act_id)
|
||||||
|
self.assert_(obj2)
|
||||||
|
_ds.delete(obj)
|
||||||
|
|
||||||
def testObjectGet(self):
|
def testObjectGet(self):
|
||||||
# create a new object
|
# create a new object
|
||||||
obj = self._create_test_object()
|
obj = self._create_test_object()
|
||||||
@ -94,7 +120,10 @@ class DataStoreTestCase(unittest.TestCase):
|
|||||||
def main():
|
def main():
|
||||||
dsTestSuite = unittest.TestSuite()
|
dsTestSuite = unittest.TestSuite()
|
||||||
dsTestSuite.addTest(DataStoreTestCase('testObjectCreate'))
|
dsTestSuite.addTest(DataStoreTestCase('testObjectCreate'))
|
||||||
|
dsTestSuite.addTest(DataStoreTestCase('testObjectCreateWithActivityId'))
|
||||||
|
dsTestSuite.addTest(DataStoreTestCase('testObjectCreateWithBadActivityId'))
|
||||||
dsTestSuite.addTest(DataStoreTestCase('testObjectGet'))
|
dsTestSuite.addTest(DataStoreTestCase('testObjectGet'))
|
||||||
|
dsTestSuite.addTest(DataStoreTestCase('testObjectGetActivityObject'))
|
||||||
dsTestSuite.addTest(DataStoreTestCase('testObjectDelete'))
|
dsTestSuite.addTest(DataStoreTestCase('testObjectDelete'))
|
||||||
dsTestSuite.addTest(DataStoreTestCase('testObjectFind'))
|
dsTestSuite.addTest(DataStoreTestCase('testObjectFind'))
|
||||||
dsTestSuite.addTest(DataStoreTestCase('testObjectGetData'))
|
dsTestSuite.addTest(DataStoreTestCase('testObjectGetData'))
|
||||||
|
Loading…
Reference in New Issue
Block a user