2007-05-10 11:01:32 +02:00
|
|
|
# Copyright (C) 2007, One Laptop Per Child
|
2006-12-11 13:55:01 +01:00
|
|
|
#
|
|
|
|
# This program is free software; you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation; either version 2 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with this program; if not, write to the Free Software
|
|
|
|
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
2007-03-02 21:17:03 +01:00
|
|
|
import logging
|
|
|
|
|
|
|
|
import gobject
|
2006-12-11 13:55:01 +01:00
|
|
|
|
2007-05-10 11:01:32 +02:00
|
|
|
from sugar.datastore import dbus_helpers
|
2006-12-11 13:55:01 +01:00
|
|
|
|
2007-05-29 15:53:58 +02:00
|
|
|
class DSMetadata(gobject.GObject):
|
2006-12-11 13:55:01 +01:00
|
|
|
__gsignals__ = {
|
|
|
|
'updated': (gobject.SIGNAL_RUN_FIRST, gobject.TYPE_NONE,
|
2007-05-13 18:21:35 +02:00
|
|
|
([]))
|
2006-12-11 13:55:01 +01:00
|
|
|
}
|
|
|
|
|
2007-05-29 15:53:58 +02:00
|
|
|
def __init__(self, props={}):
|
2006-12-11 13:55:01 +01:00
|
|
|
gobject.GObject.__init__(self)
|
2007-05-29 15:53:58 +02:00
|
|
|
self._props = props
|
2007-05-10 11:01:32 +02:00
|
|
|
|
|
|
|
def __getitem__(self, key):
|
2007-05-29 15:53:58 +02:00
|
|
|
return self._props[key]
|
2007-05-10 11:01:32 +02:00
|
|
|
|
|
|
|
def __setitem__(self, key, value):
|
2007-05-29 15:53:58 +02:00
|
|
|
if not self._props.has_key(key) or self._props[key] != value:
|
|
|
|
self._props[key] = value
|
2007-05-13 18:21:35 +02:00
|
|
|
self.emit('updated')
|
|
|
|
|
2007-05-20 12:38:08 +02:00
|
|
|
def __delitem__(self, key):
|
2007-05-29 15:53:58 +02:00
|
|
|
del self._props[key]
|
|
|
|
|
|
|
|
def has_key(self, key):
|
|
|
|
return self._props.has_key(key)
|
|
|
|
|
|
|
|
def get_dictionary(self):
|
|
|
|
return self._props
|
|
|
|
|
|
|
|
class DSObject:
|
|
|
|
def __init__(self, object_id, metadata=None, file_path=None):
|
|
|
|
self.object_id = object_id
|
|
|
|
self._metadata = metadata
|
|
|
|
self._file_path = file_path
|
2007-05-20 12:38:08 +02:00
|
|
|
|
2007-05-13 18:21:35 +02:00
|
|
|
def get_metadata(self):
|
2007-05-22 14:03:31 +02:00
|
|
|
if self._metadata is None and not self.object_id is None:
|
2007-05-29 15:53:58 +02:00
|
|
|
metadata = DSMetadata(dbus_helpers.get_properties(self.object_id))
|
|
|
|
self._metadata = metadata
|
2007-05-13 18:21:35 +02:00
|
|
|
return self._metadata
|
|
|
|
|
|
|
|
def set_metadata(self, metadata):
|
|
|
|
if self._metadata != metadata:
|
|
|
|
self._metadata = metadata
|
|
|
|
|
|
|
|
metadata = property(get_metadata, set_metadata)
|
|
|
|
|
|
|
|
def get_file_path(self):
|
2007-05-22 14:03:31 +02:00
|
|
|
if self._file_path is None and not self.object_id is None:
|
|
|
|
self.set_file_path(dbus_helpers.get_filename(self.object_id))
|
2007-05-13 18:21:35 +02:00
|
|
|
return self._file_path
|
|
|
|
|
|
|
|
def set_file_path(self, file_path):
|
|
|
|
if self._file_path != file_path:
|
|
|
|
self._file_path = file_path
|
|
|
|
|
|
|
|
file_path = property(get_file_path, set_file_path)
|
2007-05-10 11:01:32 +02:00
|
|
|
|
|
|
|
def get(object_id):
|
|
|
|
logging.debug('datastore.get')
|
|
|
|
metadata = dbus_helpers.get_properties(object_id)
|
|
|
|
file_path = dbus_helpers.get_filename(object_id)
|
2007-05-13 18:21:35 +02:00
|
|
|
|
2007-05-29 15:53:58 +02:00
|
|
|
ds_object = DSObject(object_id, DSMetadata(metadata), file_path)
|
2007-05-10 11:01:32 +02:00
|
|
|
# TODO: register the object for updates
|
|
|
|
return ds_object
|
|
|
|
|
|
|
|
def create():
|
2007-05-29 15:53:58 +02:00
|
|
|
return DSObject(object_id=None, metadata=DSMetadata(), file_path=None)
|
2007-05-10 11:01:32 +02:00
|
|
|
|
2007-05-16 06:41:45 +02:00
|
|
|
def write(ds_object, reply_handler=None, error_handler=None):
|
2007-05-10 11:01:32 +02:00
|
|
|
logging.debug('datastore.write')
|
|
|
|
if ds_object.object_id:
|
|
|
|
dbus_helpers.update(ds_object.object_id,
|
2007-05-29 15:53:58 +02:00
|
|
|
ds_object.metadata.get_dictionary(),
|
2007-05-16 06:41:45 +02:00
|
|
|
ds_object.file_path,
|
|
|
|
reply_handler=reply_handler,
|
|
|
|
error_handler=error_handler)
|
2007-05-10 11:01:32 +02:00
|
|
|
else:
|
2007-05-29 15:53:58 +02:00
|
|
|
ds_object.object_id = dbus_helpers.create(ds_object.metadata.get_dictionary(),
|
2007-05-10 11:01:32 +02:00
|
|
|
ds_object.file_path)
|
|
|
|
# TODO: register the object for updates
|
|
|
|
logging.debug('Written object %s to the datastore.' % ds_object.object_id)
|
2006-12-11 13:55:01 +01:00
|
|
|
|
2007-05-22 14:03:31 +02:00
|
|
|
def find(query, sorting=None, limit=None, offset=None, reply_handler=None,
|
|
|
|
error_handler=None):
|
|
|
|
if sorting:
|
|
|
|
query['order_by'] = sorting
|
|
|
|
if limit:
|
|
|
|
query['limit'] = limit
|
|
|
|
if offset:
|
|
|
|
query['offset'] = offset
|
|
|
|
|
|
|
|
props_list, total_count = dbus_helpers.find(query, reply_handler, error_handler)
|
|
|
|
|
2007-05-10 11:01:32 +02:00
|
|
|
objects = []
|
2007-05-22 14:03:31 +02:00
|
|
|
for props in props_list:
|
|
|
|
if props.has_key('filename') and props['filename']:
|
|
|
|
file_path = props['filename']
|
|
|
|
del props['filename']
|
|
|
|
else:
|
|
|
|
file_path = None
|
|
|
|
|
|
|
|
object_id = props['uid']
|
|
|
|
del props['uid']
|
|
|
|
|
2007-05-29 15:53:58 +02:00
|
|
|
ds_object = DSObject(object_id, DSMetadata(props), file_path)
|
2007-05-22 14:03:31 +02:00
|
|
|
objects.append(ds_object)
|
|
|
|
|
|
|
|
return objects, total_count
|
|
|
|
|