From c35db7acf803296aec4185e169cd74fc5f4255e5 Mon Sep 17 00:00:00 2001 From: Tomeu Vizoso Date: Tue, 29 May 2007 15:53:58 +0200 Subject: [PATCH] Some refactoring of the DataStore API. --- sugar/activity/activity.py | 75 ++++++++++++++++++++++-------------- sugar/datastore/datastore.py | 51 ++++++++++++++---------- 2 files changed, 76 insertions(+), 50 deletions(-) diff --git a/sugar/activity/activity.py b/sugar/activity/activity.py index dbcbd603..f1bda6c5 100644 --- a/sugar/activity/activity.py +++ b/sugar/activity/activity.py @@ -42,14 +42,14 @@ class ActivityToolbar(gtk.Toolbar): activity.connect('shared', self._activity_shared_cb) activity.connect('joined', self._activity_shared_cb) - if activity.jobject: + if activity.metadata: self.title = gtk.Entry() 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._add_widget(self.title) - activity.jobject.connect('updated', self._jobject_updated_cb) + activity.metadata.connect('updated', self._jobject_updated_cb) separator = gtk.SeparatorToolItem() separator.props.draw = False @@ -84,8 +84,8 @@ class ActivityToolbar(gtk.Toolbar): self.title.set_text(jobject['title']) def _title_focus_out_event_cb(self, entry, event): - if self._activity.jobject['title'] != self.title.get_text(): - self._activity.jobject['title'] = self.title.get_text() + if self._activity.metadata['title'] != self.title.get_text(): + self._activity.metadata['title'] = self.title.get_text() self._activity.save() def _add_widget(self, widget, expand=False): @@ -199,54 +199,62 @@ class Activity(Window, gtk.Container): self._bus = ActivityService(self) if handle.object_id: - self.jobject = datastore.get(handle.object_id) - self.jobject.object_id = '' - del self.jobject['ctime'] - del self.jobject['mtime'] + self._jobject = datastore.get(handle.object_id) + self._jobject.object_id = '' + del self._jobject.metadata['ctime'] + del self._jobject.metadata['mtime'] elif create_jobject: logging.debug('Creating a jobject.') - self.jobject = datastore.create() - self.jobject['title'] = '%s %s' % (get_bundle_name(), 'Activity') - self.jobject['activity'] = self.get_service_name() - self.jobject['keep'] = '0' - self.jobject['buddies'] = '' - self.jobject['preview'] = '' - self.jobject['icon-color'] = profile.get_color().to_string() - self.jobject.file_path = '' - datastore.write(self.jobject, + self._jobject = datastore.create() + self._jobject.metadata['title'] = '%s %s' % (get_bundle_name(), 'Activity') + self._jobject.metadata['activity'] = self.get_service_name() + self._jobject.metadata['keep'] = '0' + self._jobject.metadata['buddies'] = '' + self._jobject.metadata['preview'] = '' + self._jobject.metadata['icon-color'] = profile.get_color().to_string() + self._jobject.file_path = '' + datastore.write(self._jobject, reply_handler=self._internal_jobject_create_cb, error_handler=self._internal_jobject_error_cb) else: - self.jobject = None + self._jobject = None def do_set_property(self, pspec, value): if pspec.name == 'active': if self._active != value: self._active = value - if not self._active and self.jobject: + if not self._active and self._jobject: self.save() def do_get_property(self, pspec): if pspec.name == '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): pass def _internal_jobject_error_cb(self, 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 - the journal. Can access the object through the jobject attribute. + the journal. 'file_path' is the file to read from. """ raise NotImplementedError - def write_file(self): + def write_file(self, file_path): """ 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 @@ -259,11 +267,12 @@ class Activity(Window, gtk.Container): def save(self): """Request that the activity is saved to the Journal.""" try: - self.jobject.file_path = os.path.join('/tmp', '%i.txt' % time.time()) - self.write_file() + file_path = os.path.join('/tmp', '%i' % time.time()) + self.write_file(file_path) + self._jobject.file_path = file_path except NotImplementedError: - self.jobject.file_path = '' - datastore.write(self.jobject, + pass + datastore.write(self._jobject, reply_handler=self._internal_save_cb, error_handler=self._internal_save_error_cb) @@ -322,7 +331,7 @@ class Activity(Window, gtk.Container): self._shared_activity.leave() def close(self): - if self.jobject: + if self._jobject: try: self.save() except: @@ -330,6 +339,14 @@ class Activity(Window, gtk.Container): raise 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(): """Return the bundle name for the current process' bundle """ diff --git a/sugar/datastore/datastore.py b/sugar/datastore/datastore.py index 82f50266..52d735f9 100644 --- a/sugar/datastore/datastore.py +++ b/sugar/datastore/datastore.py @@ -19,38 +19,48 @@ import gobject from sugar.datastore import dbus_helpers -class DSObject(gobject.GObject): +class DSMetadata(gobject.GObject): __gsignals__ = { '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) + 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._metadata = metadata 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): 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 def set_metadata(self, metadata): if self._metadata != metadata: self._metadata = metadata - self.emit('updated') metadata = property(get_metadata, set_metadata) @@ -62,7 +72,6 @@ class DSObject(gobject.GObject): def set_file_path(self, file_path): if self._file_path != file_path: self._file_path = file_path - self.emit('updated') file_path = property(get_file_path, set_file_path) @@ -71,23 +80,23 @@ def get(object_id): metadata = dbus_helpers.get_properties(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 return ds_object 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): logging.debug('datastore.write') if ds_object.object_id: dbus_helpers.update(ds_object.object_id, - ds_object.metadata, + ds_object.metadata.get_dictionary(), ds_object.file_path, reply_handler=reply_handler, error_handler=error_handler) 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) # TODO: register the object for updates 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'] del props['uid'] - ds_object = DSObject(object_id, props, file_path) + ds_object = DSObject(object_id, DSMetadata(props), file_path) objects.append(ds_object) return objects, total_count