Some refactoring of the DataStore API.

This commit is contained in:
Tomeu Vizoso 2007-05-29 15:53:58 +02:00
parent 53d64a0536
commit c35db7acf8
2 changed files with 76 additions and 50 deletions

View File

@ -42,14 +42,14 @@ class ActivityToolbar(gtk.Toolbar):
activity.connect('shared', self._activity_shared_cb) activity.connect('shared', self._activity_shared_cb)
activity.connect('joined', self._activity_shared_cb) activity.connect('joined', self._activity_shared_cb)
if activity.jobject: if activity.metadata:
self.title = gtk.Entry() self.title = gtk.Entry()
self.title.set_size_request(int(gtk.gdk.screen_width() / 6), -1) self.title.set_size_request(int(gtk.gdk.screen_width() / 6), -1)
self.title.set_text(activity.jobject['title']) self.title.set_text(activity.metadata['title'])
self.title.connect('focus-out-event', self._title_focus_out_event_cb) self.title.connect('focus-out-event', self._title_focus_out_event_cb)
self._add_widget(self.title) self._add_widget(self.title)
activity.jobject.connect('updated', self._jobject_updated_cb) activity.metadata.connect('updated', self._jobject_updated_cb)
separator = gtk.SeparatorToolItem() separator = gtk.SeparatorToolItem()
separator.props.draw = False separator.props.draw = False
@ -84,8 +84,8 @@ class ActivityToolbar(gtk.Toolbar):
self.title.set_text(jobject['title']) self.title.set_text(jobject['title'])
def _title_focus_out_event_cb(self, entry, event): def _title_focus_out_event_cb(self, entry, event):
if self._activity.jobject['title'] != self.title.get_text(): if self._activity.metadata['title'] != self.title.get_text():
self._activity.jobject['title'] = self.title.get_text() self._activity.metadata['title'] = self.title.get_text()
self._activity.save() self._activity.save()
def _add_widget(self, widget, expand=False): def _add_widget(self, widget, expand=False):
@ -199,54 +199,62 @@ class Activity(Window, gtk.Container):
self._bus = ActivityService(self) self._bus = ActivityService(self)
if handle.object_id: if handle.object_id:
self.jobject = datastore.get(handle.object_id) self._jobject = datastore.get(handle.object_id)
self.jobject.object_id = '' self._jobject.object_id = ''
del self.jobject['ctime'] del self._jobject.metadata['ctime']
del self.jobject['mtime'] del self._jobject.metadata['mtime']
elif create_jobject: elif create_jobject:
logging.debug('Creating a jobject.') logging.debug('Creating a jobject.')
self.jobject = datastore.create() self._jobject = datastore.create()
self.jobject['title'] = '%s %s' % (get_bundle_name(), 'Activity') self._jobject.metadata['title'] = '%s %s' % (get_bundle_name(), 'Activity')
self.jobject['activity'] = self.get_service_name() self._jobject.metadata['activity'] = self.get_service_name()
self.jobject['keep'] = '0' self._jobject.metadata['keep'] = '0'
self.jobject['buddies'] = '' self._jobject.metadata['buddies'] = ''
self.jobject['preview'] = '' self._jobject.metadata['preview'] = ''
self.jobject['icon-color'] = profile.get_color().to_string() self._jobject.metadata['icon-color'] = profile.get_color().to_string()
self.jobject.file_path = '' self._jobject.file_path = ''
datastore.write(self.jobject, datastore.write(self._jobject,
reply_handler=self._internal_jobject_create_cb, reply_handler=self._internal_jobject_create_cb,
error_handler=self._internal_jobject_error_cb) error_handler=self._internal_jobject_error_cb)
else: else:
self.jobject = None self._jobject = None
def do_set_property(self, pspec, value): def do_set_property(self, pspec, value):
if pspec.name == 'active': if pspec.name == 'active':
if self._active != value: if self._active != value:
self._active = value self._active = value
if not self._active and self.jobject: if not self._active and self._jobject:
self.save() self.save()
def do_get_property(self, pspec): def do_get_property(self, pspec):
if pspec.name == 'active': if pspec.name == 'active':
return self._active return self._active
def set_canvas(self, canvas):
Window.set_canvas(self, canvas)
canvas.connect('map', self._canvas_map_cb)
def _canvas_map_cb(self, canvas):
if self._jobject and self._jobject.file_path:
self.read_file(self._jobject.file_path)
def _internal_jobject_create_cb(self): def _internal_jobject_create_cb(self):
pass pass
def _internal_jobject_error_cb(self, err): def _internal_jobject_error_cb(self, err):
logging.debug("Error creating activity datastore object: %s" % err) logging.debug("Error creating activity datastore object: %s" % err)
def read_file(self): def read_file(self, file_path):
""" """
Subclasses implement this method if they support resuming objects from Subclasses implement this method if they support resuming objects from
the journal. Can access the object through the jobject attribute. the journal. 'file_path' is the file to read from.
""" """
raise NotImplementedError raise NotImplementedError
def write_file(self): def write_file(self, file_path):
""" """
Subclasses implement this method if they support saving data to objects Subclasses implement this method if they support saving data to objects
in the journal. Can access the object through the jobject attribute. in the journal. 'file_path' is the file to write to.
""" """
raise NotImplementedError raise NotImplementedError
@ -259,11 +267,12 @@ class Activity(Window, gtk.Container):
def save(self): def save(self):
"""Request that the activity is saved to the Journal.""" """Request that the activity is saved to the Journal."""
try: try:
self.jobject.file_path = os.path.join('/tmp', '%i.txt' % time.time()) file_path = os.path.join('/tmp', '%i' % time.time())
self.write_file() self.write_file(file_path)
self._jobject.file_path = file_path
except NotImplementedError: except NotImplementedError:
self.jobject.file_path = '' pass
datastore.write(self.jobject, datastore.write(self._jobject,
reply_handler=self._internal_save_cb, reply_handler=self._internal_save_cb,
error_handler=self._internal_save_error_cb) error_handler=self._internal_save_error_cb)
@ -322,7 +331,7 @@ class Activity(Window, gtk.Container):
self._shared_activity.leave() self._shared_activity.leave()
def close(self): def close(self):
if self.jobject: if self._jobject:
try: try:
self.save() self.save()
except: except:
@ -330,6 +339,14 @@ class Activity(Window, gtk.Container):
raise raise
self.destroy() self.destroy()
def get_metadata(self):
if self._jobject:
return self._jobject.metadata
else:
return None
metadata = property(get_metadata, None)
def get_bundle_name(): def get_bundle_name():
"""Return the bundle name for the current process' bundle """Return the bundle name for the current process' bundle
""" """

View File

@ -19,38 +19,48 @@ import gobject
from sugar.datastore import dbus_helpers from sugar.datastore import dbus_helpers
class DSObject(gobject.GObject): class DSMetadata(gobject.GObject):
__gsignals__ = { __gsignals__ = {
'updated': (gobject.SIGNAL_RUN_FIRST, gobject.TYPE_NONE, 'updated': (gobject.SIGNAL_RUN_FIRST, gobject.TYPE_NONE,
([])) ([]))
} }
def __init__(self, object_id, metadata=None, file_path=None): def __init__(self, props={}):
gobject.GObject.__init__(self) gobject.GObject.__init__(self)
self._props = props
def __getitem__(self, key):
return self._props[key]
def __setitem__(self, key, value):
if not self._props.has_key(key) or self._props[key] != value:
self._props[key] = value
self.emit('updated')
def __delitem__(self, key):
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.object_id = object_id
self._metadata = metadata self._metadata = metadata
self._file_path = file_path self._file_path = file_path
def __getitem__(self, key):
return self.metadata[key]
def __setitem__(self, key, value):
if not self.metadata.has_key(key) or self.metadata[key] != value:
self.metadata[key] = value
self.emit('updated')
def __delitem__(self, key):
del self.metadata[key]
def get_metadata(self): def get_metadata(self):
if self._metadata is None and not self.object_id is None: if self._metadata is None and not self.object_id is None:
self.set_metadata(dbus_helpers.get_properties(self.object_id)) metadata = DSMetadata(dbus_helpers.get_properties(self.object_id))
self._metadata = metadata
return self._metadata return self._metadata
def set_metadata(self, metadata): def set_metadata(self, metadata):
if self._metadata != metadata: if self._metadata != metadata:
self._metadata = metadata self._metadata = metadata
self.emit('updated')
metadata = property(get_metadata, set_metadata) metadata = property(get_metadata, set_metadata)
@ -62,7 +72,6 @@ class DSObject(gobject.GObject):
def set_file_path(self, file_path): def set_file_path(self, file_path):
if self._file_path != file_path: if self._file_path != file_path:
self._file_path = file_path self._file_path = file_path
self.emit('updated')
file_path = property(get_file_path, set_file_path) file_path = property(get_file_path, set_file_path)
@ -71,23 +80,23 @@ def get(object_id):
metadata = dbus_helpers.get_properties(object_id) metadata = dbus_helpers.get_properties(object_id)
file_path = dbus_helpers.get_filename(object_id) file_path = dbus_helpers.get_filename(object_id)
ds_object = DSObject(object_id, metadata, file_path) ds_object = DSObject(object_id, DSMetadata(metadata), file_path)
# TODO: register the object for updates # TODO: register the object for updates
return ds_object return ds_object
def create(): def create():
return DSObject(object_id=None, metadata={}, file_path=None) return DSObject(object_id=None, metadata=DSMetadata(), file_path=None)
def write(ds_object, reply_handler=None, error_handler=None): def write(ds_object, reply_handler=None, error_handler=None):
logging.debug('datastore.write') logging.debug('datastore.write')
if ds_object.object_id: if ds_object.object_id:
dbus_helpers.update(ds_object.object_id, dbus_helpers.update(ds_object.object_id,
ds_object.metadata, ds_object.metadata.get_dictionary(),
ds_object.file_path, ds_object.file_path,
reply_handler=reply_handler, reply_handler=reply_handler,
error_handler=error_handler) error_handler=error_handler)
else: else:
ds_object.object_id = dbus_helpers.create(ds_object.metadata, ds_object.object_id = dbus_helpers.create(ds_object.metadata.get_dictionary(),
ds_object.file_path) ds_object.file_path)
# TODO: register the object for updates # TODO: register the object for updates
logging.debug('Written object %s to the datastore.' % ds_object.object_id) logging.debug('Written object %s to the datastore.' % ds_object.object_id)
@ -114,7 +123,7 @@ def find(query, sorting=None, limit=None, offset=None, reply_handler=None,
object_id = props['uid'] object_id = props['uid']
del props['uid'] del props['uid']
ds_object = DSObject(object_id, props, file_path) ds_object = DSObject(object_id, DSMetadata(props), file_path)
objects.append(ds_object) objects.append(ds_object)
return objects, total_count return objects, total_count