2007-05-10 11:01:32 +02:00
|
|
|
# Copyright (C) 2007, One Laptop Per Child
|
2006-12-11 13:55:01 +01:00
|
|
|
#
|
2007-06-24 13:10:53 +02:00
|
|
|
# This library is free software; you can redistribute it and/or
|
|
|
|
# modify it under the terms of the GNU Lesser General Public
|
|
|
|
# License as published by the Free Software Foundation; either
|
|
|
|
# version 2 of the License, or (at your option) any later version.
|
2006-12-11 13:55:01 +01:00
|
|
|
#
|
2007-06-24 13:10:53 +02:00
|
|
|
# This library is distributed in the hope that it will be useful,
|
2006-12-11 13:55:01 +01:00
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2007-06-24 13:10:53 +02:00
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
# Lesser General Public License for more details.
|
2006-12-11 13:55:01 +01:00
|
|
|
#
|
2007-06-24 13:10:53 +02:00
|
|
|
# You should have received a copy of the GNU Lesser General Public
|
|
|
|
# License along with this library; if not, write to the
|
|
|
|
# Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
|
|
# Boston, MA 02111-1307, USA.
|
|
|
|
|
2007-03-02 21:17:03 +01:00
|
|
|
import logging
|
2007-09-10 14:04:43 +02:00
|
|
|
import time
|
2007-07-16 13:01:35 +02:00
|
|
|
from datetime import datetime
|
2007-07-20 19:50:49 +02:00
|
|
|
import os
|
2007-03-02 21:17:03 +01:00
|
|
|
|
|
|
|
import gobject
|
2006-12-11 13:55:01 +01:00
|
|
|
|
2007-05-10 11:01:32 +02:00
|
|
|
from sugar.datastore import dbus_helpers
|
2007-07-11 11:39:40 +02:00
|
|
|
from sugar import activity
|
2007-07-20 13:15:11 +02:00
|
|
|
from sugar.activity.activityhandle import ActivityHandle
|
2007-09-06 22:24:44 +02:00
|
|
|
from sugar.bundle.contentbundle import ContentBundle
|
2007-09-20 18:20:21 +02:00
|
|
|
from sugar.bundle.activitybundle import ActivityBundle
|
|
|
|
from sugar.bundle.contentbundle import ContentBundle
|
2007-09-20 12:25:12 +02:00
|
|
|
from sugar.objects import mime
|
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-06-12 21:57:49 +02:00
|
|
|
def __init__(self, props=None):
|
2006-12-11 13:55:01 +01:00
|
|
|
gobject.GObject.__init__(self)
|
2007-06-12 21:57:49 +02:00
|
|
|
if not props:
|
|
|
|
self._props = {}
|
|
|
|
else:
|
|
|
|
self._props = props
|
|
|
|
|
2007-07-20 13:15:11 +02:00
|
|
|
default_keys = ['activity', 'activity_id',
|
|
|
|
'mime_type', 'title_set_by_user']
|
2007-06-12 21:57:49 +02:00
|
|
|
for key in default_keys:
|
|
|
|
if not self._props.has_key(key):
|
|
|
|
self._props[key] = ''
|
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
|
|
|
|
|
2007-08-27 19:44:49 +02:00
|
|
|
def copy(self):
|
|
|
|
return DSMetadata(self._props.copy())
|
|
|
|
|
2007-07-20 19:50:49 +02:00
|
|
|
class DSObject(object):
|
2007-05-29 15:53:58 +02:00
|
|
|
def __init__(self, object_id, metadata=None, file_path=None):
|
|
|
|
self.object_id = object_id
|
|
|
|
self._metadata = metadata
|
|
|
|
self._file_path = file_path
|
2007-07-20 19:50:49 +02:00
|
|
|
self._destroyed = False
|
|
|
|
self._owns_file = False
|
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-07-20 19:50:49 +02:00
|
|
|
self._owns_file = True
|
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:
|
2007-07-20 19:50:49 +02:00
|
|
|
if self._file_path and self._owns_file:
|
|
|
|
if os.path.isfile(self._file_path):
|
|
|
|
os.remove(self._file_path)
|
|
|
|
self._owns_file = False
|
2007-05-13 18:21:35 +02:00
|
|
|
self._file_path = file_path
|
|
|
|
|
|
|
|
file_path = property(get_file_path, set_file_path)
|
2007-05-10 11:01:32 +02:00
|
|
|
|
2007-07-11 11:39:40 +02:00
|
|
|
def get_activities(self):
|
|
|
|
activities = []
|
|
|
|
|
|
|
|
if self.metadata['activity']:
|
|
|
|
activity_info = activity.get_registry().get_activity(self.metadata['activity'])
|
2007-09-18 23:34:04 +02:00
|
|
|
if activity_info:
|
|
|
|
activities.append(activity_info)
|
2007-07-11 11:39:40 +02:00
|
|
|
|
|
|
|
mime_type = self.metadata['mime_type']
|
|
|
|
if mime_type:
|
|
|
|
activities_info = activity.get_registry().get_activities_for_type(mime_type)
|
|
|
|
for activity_info in activities_info:
|
|
|
|
if activity_info.service_name != self.metadata['activity']:
|
|
|
|
activities.append(activity_info)
|
|
|
|
|
|
|
|
return activities
|
|
|
|
|
2007-09-20 18:20:21 +02:00
|
|
|
def is_activity_bundle(self):
|
|
|
|
return self.metadata['mime_type'] in \
|
|
|
|
[ActivityBundle.MIME_TYPE, ActivityBundle.DEPRECATED_MIME_TYPE]
|
|
|
|
|
2007-09-06 22:24:44 +02:00
|
|
|
def is_content_bundle(self):
|
|
|
|
return self.metadata['mime_type'] == ContentBundle.MIME_TYPE
|
|
|
|
|
2007-07-11 11:39:40 +02:00
|
|
|
def is_bundle(self):
|
2007-09-20 18:20:21 +02:00
|
|
|
return self.is_activity_bundle() or self.is_content_bundle()
|
2007-07-11 11:39:40 +02:00
|
|
|
|
2007-07-28 20:57:47 +02:00
|
|
|
def resume(self, service_name=None):
|
2007-09-14 16:16:54 +02:00
|
|
|
from sugar.activity import activityfactory
|
|
|
|
|
2007-09-20 18:20:21 +02:00
|
|
|
if self.is_activity_bundle():
|
2007-08-15 12:19:29 +02:00
|
|
|
if service_name is not None:
|
|
|
|
raise ValueError('Object is a bundle, cannot be resumed as an activity.')
|
|
|
|
|
2007-09-20 18:20:21 +02:00
|
|
|
bundle = ActivityBundle(self.file_path)
|
2007-07-11 11:39:40 +02:00
|
|
|
if not bundle.is_installed():
|
|
|
|
bundle.install()
|
|
|
|
|
|
|
|
activityfactory.create(bundle.get_service_name())
|
|
|
|
else:
|
2007-09-09 15:04:26 +02:00
|
|
|
if not self.get_activities() and service_name is None:
|
|
|
|
logging.warning('No activity can open this object.')
|
2007-09-08 18:41:59 +02:00
|
|
|
return
|
2007-07-28 20:57:47 +02:00
|
|
|
if service_name is None:
|
|
|
|
service_name = self.get_activities()[0].service_name
|
2007-07-20 13:15:11 +02:00
|
|
|
|
2007-07-20 21:25:39 +02:00
|
|
|
activity_id = self.metadata['activity_id']
|
2007-07-20 21:40:41 +02:00
|
|
|
object_id = self.object_id
|
|
|
|
|
2007-07-20 21:25:39 +02:00
|
|
|
if activity_id:
|
2007-07-20 21:40:41 +02:00
|
|
|
handle = ActivityHandle(object_id=object_id,
|
2007-07-20 21:25:39 +02:00
|
|
|
activity_id=activity_id)
|
2007-07-20 21:40:41 +02:00
|
|
|
activityfactory.create(service_name, handle)
|
2007-07-20 21:25:39 +02:00
|
|
|
else:
|
2007-07-20 21:40:41 +02:00
|
|
|
activityfactory.create_with_object_id(service_name, object_id)
|
2007-07-11 11:39:40 +02:00
|
|
|
|
2007-07-20 19:50:49 +02:00
|
|
|
def destroy(self):
|
|
|
|
if self._destroyed:
|
|
|
|
logging.warning('This DSObject has already been destroyed!.')
|
2007-09-10 18:03:40 +02:00
|
|
|
import traceback;traceback.print_stack()
|
2007-07-20 19:50:49 +02:00
|
|
|
return
|
|
|
|
self._destroyed = True
|
|
|
|
if self._file_path and self._owns_file:
|
|
|
|
if os.path.isfile(self._file_path):
|
|
|
|
os.remove(self._file_path)
|
|
|
|
self._owns_file = False
|
|
|
|
self._file_path = None
|
|
|
|
|
|
|
|
def __del__(self):
|
|
|
|
if not self._destroyed:
|
|
|
|
logging.warning('DSObject was deleted without cleaning up first. ' \
|
|
|
|
'Please call DSObject.destroy() before disposing it.')
|
|
|
|
self.destroy()
|
|
|
|
|
2007-08-27 19:44:49 +02:00
|
|
|
def copy(self):
|
|
|
|
return DSObject(None, self._metadata.copy(), self._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)
|
2007-05-13 18:21:35 +02:00
|
|
|
|
2007-07-23 13:20:24 +02:00
|
|
|
ds_object = DSObject(object_id, DSMetadata(metadata), None)
|
2007-05-10 11:01:32 +02:00
|
|
|
# TODO: register the object for updates
|
|
|
|
return ds_object
|
|
|
|
|
|
|
|
def create():
|
2007-07-13 13:20:44 +02:00
|
|
|
metadata = DSMetadata()
|
2007-09-10 14:04:43 +02:00
|
|
|
metadata['mtime'] = datetime.now().isoformat()
|
|
|
|
metadata['timestamp'] = int(time.time())
|
2007-07-13 13:20:44 +02:00
|
|
|
return DSObject(object_id=None, metadata=metadata, file_path=None)
|
2007-05-10 11:01:32 +02:00
|
|
|
|
2007-09-08 03:53:32 +02:00
|
|
|
def write(ds_object, update_mtime=True, transfer_ownership=False, reply_handler=None, error_handler=None, timeout=-1):
|
2007-06-15 18:03:17 +02:00
|
|
|
logging.debug('datastore.write')
|
2007-07-09 14:26:41 +02:00
|
|
|
|
|
|
|
properties = ds_object.metadata.get_dictionary().copy()
|
|
|
|
|
2007-07-16 14:57:07 +02:00
|
|
|
if update_mtime:
|
|
|
|
properties['mtime'] = datetime.now().isoformat()
|
2007-09-10 14:04:43 +02:00
|
|
|
properties['timestamp'] = int(time.time())
|
2007-07-16 13:01:35 +02:00
|
|
|
|
2007-09-10 18:03:40 +02:00
|
|
|
if ds_object._file_path is None:
|
|
|
|
file_path = ''
|
|
|
|
else:
|
|
|
|
file_path = ds_object._file_path
|
|
|
|
|
|
|
|
# FIXME: this func will be sync for creates regardless of the handlers
|
|
|
|
# supplied. This is very bad API, need to decide what to do here.
|
2007-05-10 11:01:32 +02:00
|
|
|
if ds_object.object_id:
|
|
|
|
dbus_helpers.update(ds_object.object_id,
|
2007-07-09 14:26:41 +02:00
|
|
|
properties,
|
2007-09-10 18:03:40 +02:00
|
|
|
file_path,
|
2007-09-08 03:53:32 +02:00
|
|
|
transfer_ownership,
|
2007-05-16 06:41:45 +02:00
|
|
|
reply_handler=reply_handler,
|
2007-07-26 09:20:38 +02:00
|
|
|
error_handler=error_handler,
|
|
|
|
timeout=timeout)
|
2007-05-10 11:01:32 +02:00
|
|
|
else:
|
2007-09-10 18:03:40 +02:00
|
|
|
if reply_handler or error_handler:
|
|
|
|
logging.warning('datastore.write() cannot currently be called async' \
|
|
|
|
' for creates, see https://dev.laptop.org/ticket/3071')
|
2007-07-09 14:26:41 +02:00
|
|
|
ds_object.object_id = dbus_helpers.create(properties,
|
2007-09-10 18:03:40 +02:00
|
|
|
file_path,
|
2007-09-08 03:53:32 +02:00
|
|
|
transfer_ownership)
|
2007-05-10 11:01:32 +02:00
|
|
|
# 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-06-22 17:01:13 +02:00
|
|
|
def delete(object_id):
|
|
|
|
logging.debug('datastore.delete')
|
|
|
|
dbus_helpers.delete(object_id)
|
|
|
|
|
2007-09-10 18:03:40 +02:00
|
|
|
def find(query, sorting=None, limit=None, offset=None, properties=[],
|
|
|
|
reply_handler=None, error_handler=None):
|
2007-08-01 16:31:33 +02:00
|
|
|
|
|
|
|
query = query.copy()
|
|
|
|
|
2007-05-22 14:03:31 +02:00
|
|
|
if sorting:
|
|
|
|
query['order_by'] = sorting
|
|
|
|
if limit:
|
|
|
|
query['limit'] = limit
|
|
|
|
if offset:
|
|
|
|
query['offset'] = offset
|
|
|
|
|
2007-09-10 18:03:40 +02:00
|
|
|
props_list, total_count = dbus_helpers.find(query, properties, reply_handler, error_handler)
|
2007-05-22 14:03:31 +02:00
|
|
|
|
2007-05-10 11:01:32 +02:00
|
|
|
objects = []
|
2007-05-22 14:03:31 +02:00
|
|
|
for props in props_list:
|
|
|
|
object_id = props['uid']
|
|
|
|
del props['uid']
|
|
|
|
|
2007-07-30 16:21:42 +02:00
|
|
|
ds_object = DSObject(object_id, DSMetadata(props), None)
|
2007-05-22 14:03:31 +02:00
|
|
|
objects.append(ds_object)
|
|
|
|
|
|
|
|
return objects, total_count
|
|
|
|
|
2007-08-27 19:44:49 +02:00
|
|
|
def copy(jobject, mount_point):
|
|
|
|
|
|
|
|
new_jobject = jobject.copy()
|
|
|
|
new_jobject.metadata['mountpoint'] = mount_point
|
|
|
|
|
2007-09-20 12:25:12 +02:00
|
|
|
if jobject.metadata.has_key('title'):
|
|
|
|
filename = jobject.metadata['title']
|
|
|
|
|
|
|
|
if jobject.metadata.has_key('mime_type'):
|
|
|
|
mime_type = jobject.metadata['mime_type']
|
|
|
|
extension = mime.get_primary_extension(mime_type)
|
|
|
|
if extension:
|
|
|
|
filename += '.' + extension
|
|
|
|
|
|
|
|
new_jobject.metadata['suggested_filename'] = filename
|
|
|
|
|
2007-08-27 19:44:49 +02:00
|
|
|
# this will cause the file be retrieved from the DS
|
|
|
|
new_jobject.file_path = jobject.file_path
|
|
|
|
|
|
|
|
write(new_jobject)
|
|
|
|
|
2007-06-27 11:36:05 +02:00
|
|
|
def mount(uri, options):
|
|
|
|
return dbus_helpers.mount(uri, options)
|
|
|
|
|
2007-06-28 10:43:38 +02:00
|
|
|
def unmount(mount_point_id):
|
|
|
|
dbus_helpers.unmount(mount_point_id)
|
|
|
|
|
2007-06-27 11:36:05 +02:00
|
|
|
def mounts():
|
|
|
|
return dbus_helpers.mounts()
|
2007-07-03 17:07:48 +02:00
|
|
|
|
2007-08-29 19:56:43 +02:00
|
|
|
def complete_indexing():
|
|
|
|
return dbus_helpers.complete_indexing()
|
|
|
|
|
2007-07-03 17:07:48 +02:00
|
|
|
def get_unique_values(key):
|
|
|
|
return dbus_helpers.get_unique_values(key)
|