Add caching to the activity and object type registries.
This commit is contained in:
parent
1ae7a908ea
commit
ecca1dca00
@ -3,6 +3,9 @@ import sys
|
|||||||
|
|
||||||
from sugar.activity.bundle import Bundle
|
from sugar.activity.bundle import Bundle
|
||||||
|
|
||||||
|
from dbus.mainloop.glib import DBusGMainLoop
|
||||||
|
DBusGMainLoop(set_as_default=True)
|
||||||
|
|
||||||
bundle = Bundle(sys.argv[1])
|
bundle = Bundle(sys.argv[1])
|
||||||
bundle.install()
|
bundle.install()
|
||||||
|
|
||||||
|
@ -56,6 +56,9 @@ class ShellService(dbus.service.Object):
|
|||||||
self._home_model.connect('active-activity-changed',
|
self._home_model.connect('active-activity-changed',
|
||||||
self._cur_activity_changed_cb)
|
self._cur_activity_changed_cb)
|
||||||
|
|
||||||
|
bundle_registry = bundleregistry.get_registry()
|
||||||
|
bundle_registry.connect('bundle-added', self._bundle_added_cb)
|
||||||
|
|
||||||
bus = dbus.SessionBus()
|
bus = dbus.SessionBus()
|
||||||
bus_name = dbus.service.BusName(_DBUS_SERVICE, bus=bus)
|
bus_name = dbus.service.BusName(_DBUS_SERVICE, bus=bus)
|
||||||
dbus.service.Object.__init__(self, bus_name, _DBUS_PATH)
|
dbus.service.Object.__init__(self, bus_name, _DBUS_PATH)
|
||||||
@ -121,6 +124,10 @@ class ShellService(dbus.service.Object):
|
|||||||
|
|
||||||
return result
|
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")
|
@dbus.service.signal(_DBUS_OWNER_IFACE, signature="s")
|
||||||
def ColorChanged(self, color):
|
def ColorChanged(self, color):
|
||||||
pass
|
pass
|
||||||
@ -158,3 +165,7 @@ class ShellService(dbus.service.Object):
|
|||||||
'icon': bundle.get_icon(),
|
'icon': bundle.get_icon(),
|
||||||
'service_name': bundle.get_service_name(),
|
'service_name': bundle.get_service_name(),
|
||||||
'path': bundle.get_path()}
|
'path': bundle.get_path()}
|
||||||
|
|
||||||
|
def _bundle_added_cb(self, bundle_registry, bundle):
|
||||||
|
self.ActivityAdded(self._bundle_to_dict(bundle))
|
||||||
|
|
||||||
|
@ -15,6 +15,8 @@
|
|||||||
# Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
# Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
||||||
# Boston, MA 02111-1307, USA.
|
# Boston, MA 02111-1307, USA.
|
||||||
|
|
||||||
|
import logging
|
||||||
|
|
||||||
import dbus
|
import dbus
|
||||||
|
|
||||||
_SHELL_SERVICE = "org.laptop.Shell"
|
_SHELL_SERVICE = "org.laptop.Shell"
|
||||||
@ -39,6 +41,11 @@ class ActivityRegistry(object):
|
|||||||
bus = dbus.SessionBus()
|
bus = dbus.SessionBus()
|
||||||
bus_object = bus.get_object(_SHELL_SERVICE, _SHELL_PATH)
|
bus_object = bus.get_object(_SHELL_SERVICE, _SHELL_PATH)
|
||||||
self._registry = dbus.Interface(bus_object, _REGISTRY_IFACE)
|
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):
|
def _convert_info_list(self, info_list):
|
||||||
result = []
|
result = []
|
||||||
@ -49,16 +56,33 @@ class ActivityRegistry(object):
|
|||||||
return result
|
return result
|
||||||
|
|
||||||
def get_activity(self, service_name):
|
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)
|
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):
|
def find_activity(self, name):
|
||||||
info_list = self._registry.FindActivity(name)
|
info_list = self._registry.FindActivity(name)
|
||||||
return self._convert_info_list(info_list)
|
return self._convert_info_list(info_list)
|
||||||
|
|
||||||
def get_activities_for_type(self, mime_type):
|
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)
|
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
|
_registry = None
|
||||||
|
|
||||||
|
@ -36,20 +36,39 @@ class ObjectType(object):
|
|||||||
self.name = name
|
self.name = name
|
||||||
self.icon = icon
|
self.icon = icon
|
||||||
self.mime_types = mime_types
|
self.mime_types = mime_types
|
||||||
|
|
||||||
|
self._type_id_to_type = {}
|
||||||
|
self._mime_type_to_type = {}
|
||||||
|
|
||||||
class ObjectTypeRegistry(object):
|
class ObjectTypeRegistry(object):
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
bus = dbus.SessionBus()
|
bus = dbus.SessionBus()
|
||||||
bus_object = bus.get_object(_SERVICE, _PATH)
|
bus_object = bus.get_object(_SERVICE, _PATH)
|
||||||
self._registry = dbus.Interface(bus_object, _IFACE)
|
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):
|
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)
|
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):
|
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)
|
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
|
_registry = None
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user