Add caching to the activity and object type registries.

This commit is contained in:
Tomeu Vizoso 2007-07-09 20:14:24 +02:00
parent 1ae7a908ea
commit ecca1dca00
4 changed files with 61 additions and 4 deletions

View File

@ -3,6 +3,9 @@ import sys
from sugar.activity.bundle import Bundle
from dbus.mainloop.glib import DBusGMainLoop
DBusGMainLoop(set_as_default=True)
bundle = Bundle(sys.argv[1])
bundle.install()

View File

@ -56,6 +56,9 @@ class ShellService(dbus.service.Object):
self._home_model.connect('active-activity-changed',
self._cur_activity_changed_cb)
bundle_registry = bundleregistry.get_registry()
bundle_registry.connect('bundle-added', self._bundle_added_cb)
bus = dbus.SessionBus()
bus_name = dbus.service.BusName(_DBUS_SERVICE, bus=bus)
dbus.service.Object.__init__(self, bus_name, _DBUS_PATH)
@ -121,6 +124,10 @@ class ShellService(dbus.service.Object):
return result
@dbus.service.signal(_DBUS_ACTIVITY_REGISTRY_IFACE, signature="a{sv}")
def ActivityAdded(self, activity_info):
pass
@dbus.service.signal(_DBUS_OWNER_IFACE, signature="s")
def ColorChanged(self, color):
pass
@ -158,3 +165,7 @@ class ShellService(dbus.service.Object):
'icon': bundle.get_icon(),
'service_name': bundle.get_service_name(),
'path': bundle.get_path()}
def _bundle_added_cb(self, bundle_registry, bundle):
self.ActivityAdded(self._bundle_to_dict(bundle))

View File

@ -15,6 +15,8 @@
# Free Software Foundation, Inc., 59 Temple Place - Suite 330,
# Boston, MA 02111-1307, USA.
import logging
import dbus
_SHELL_SERVICE = "org.laptop.Shell"
@ -39,6 +41,11 @@ class ActivityRegistry(object):
bus = dbus.SessionBus()
bus_object = bus.get_object(_SHELL_SERVICE, _SHELL_PATH)
self._registry = dbus.Interface(bus_object, _REGISTRY_IFACE)
self._registry.connect_to_signal('ActivityAdded', self._activity_added_cb)
# Two caches fo saving some travel across dbus.
self._service_name_to_activity_info = {}
self._mime_type_to_activities = {}
def _convert_info_list(self, info_list):
result = []
@ -49,16 +56,33 @@ class ActivityRegistry(object):
return result
def get_activity(self, service_name):
if self._service_name_to_activity_info.has_key(service_name):
return self._service_name_to_activity_info[service_name]
info_dict = self._registry.GetActivity(service_name)
return _activity_info_from_dict(info_dict)
activity_info = _activity_info_from_dict(info_dict)
self._service_name_to_activity_info[service_name] = activity_info
return activity_info
def find_activity(self, name):
info_list = self._registry.FindActivity(name)
return self._convert_info_list(info_list)
def get_activities_for_type(self, mime_type):
if self._mime_type_to_activities.has_key(mime_type):
return self._mime_type_to_activities[mime_type]
info_list = self._registry.GetActivitiesForType(mime_type)
return self._convert_info_list(info_list)
activities = self._convert_info_list(info_list)
self._mime_type_to_activities[mime_type] = activities
return activities
def _activity_added_cb(self, bundle):
logging.debug('ActivityRegistry._activity_added_cb: flushing caches')
self._service_name_to_activity_info.clear()
self._mime_type_to_activities.clear()
_registry = None

View File

@ -36,20 +36,39 @@ class ObjectType(object):
self.name = name
self.icon = icon
self.mime_types = mime_types
self._type_id_to_type = {}
self._mime_type_to_type = {}
class ObjectTypeRegistry(object):
def __init__(self):
bus = dbus.SessionBus()
bus_object = bus.get_object(_SERVICE, _PATH)
self._registry = dbus.Interface(bus_object, _IFACE)
# Two caches fo saving some travel across dbus.
self._type_id_to_type = {}
self._mime_type_to_type = {}
def get_type(self, type_id):
if self._type_id_to_type.has_key(type_id):
return self._type_id_to_type[type_id]
type_dict = self._registry.GetType(type_id)
return _object_type_from_dict(type_dict)
object_type = _object_type_from_dict(type_dict)
self._type_id_to_type[type_id] = object_type
return object_type
def get_type_for_mime(self, mime_type):
if self._mime_type_to_type.has_key(mime_type):
return self._mime_type_to_type[mime_type]
type_dict = self._registry.GetTypeForMIME(mime_type)
return _object_type_from_dict(type_dict)
object_type = _object_type_from_dict(type_dict)
self._mime_type_to_type[mime_type] = object_type
return object_type
_registry = None