Several fixes for the object type registry and the activity registry.
This commit is contained in:
		
							parent
							
								
									cc604e0815
								
							
						
					
					
						commit
						22689ed1da
					
				| @ -52,7 +52,7 @@ activity_info = None | |||||||
| 
 | 
 | ||||||
| if len(sys.argv) > 1: | if len(sys.argv) > 1: | ||||||
|     registry = ActivityRegistry() |     registry = ActivityRegistry() | ||||||
|     activities = registry.get_activities_for_name(sys.argv[1]) |     activities = registry.find_activity(sys.argv[1]) | ||||||
|     if len(activities) > 0: |     if len(activities) > 0: | ||||||
|         activity_info = activities[0] |         activity_info = activities[0] | ||||||
| 
 | 
 | ||||||
|  | |||||||
| @ -4,18 +4,23 @@ service_in_files =                              \ | |||||||
|     org.laptop.Clipboard.service.in             \
 |     org.laptop.Clipboard.service.in             \
 | ||||||
|     org.laptop.ObjectTypeRegistry.service.in |     org.laptop.ObjectTypeRegistry.service.in | ||||||
| 
 | 
 | ||||||
| service_DATA = $(service_in_files:.service.in=.service) | service_DATA =					\
 | ||||||
|  |     org.laptop.Clipboard.service		\
 | ||||||
|  |     org.laptop.ObjectTypeRegistry.service | ||||||
| 
 | 
 | ||||||
| $(service_DATA): $(service_in_files) Makefile | org.laptop.Clipboard.service: org.laptop.Clipboard.service.in Makefile | ||||||
|  | 	@sed -e "s|\@bindir\@|$(bindir)|" $< > $@ | ||||||
|  | 
 | ||||||
|  | org.laptop.ObjectTypeRegistry.service: org.laptop.ObjectTypeRegistry.service.in Makefile | ||||||
| 	@sed -e "s|\@bindir\@|$(bindir)|" $< > $@ | 	@sed -e "s|\@bindir\@|$(bindir)|" $< > $@ | ||||||
| 
 | 
 | ||||||
| sugardir = $(pkgdatadir)/services/clipboard | sugardir = $(pkgdatadir)/services/clipboard | ||||||
| 
 | 
 | ||||||
| sugar_PYTHON =			    \
 | sugar_PYTHON =			\
 | ||||||
| 	__init__.py			    \
 | 	__init__.py		\
 | ||||||
| 	clipboardobject.py      \
 | 	clipboardobject.py      \
 | ||||||
| 	clipboardservice.py     \
 | 	clipboardservice.py     \
 | ||||||
|     objecttypeservice.py    \
 | 	objecttypeservice.py    \
 | ||||||
| 	typeregistry.py | 	typeregistry.py | ||||||
| 
 | 
 | ||||||
| bin_SCRIPTS = sugar-clipboard | bin_SCRIPTS = sugar-clipboard | ||||||
|  | |||||||
| @ -15,7 +15,6 @@ | |||||||
| # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA | # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA | ||||||
| 
 | 
 | ||||||
| import logging | import logging | ||||||
| import gobject |  | ||||||
| import os | import os | ||||||
| import shutil | import shutil | ||||||
| import dbus | import dbus | ||||||
| @ -32,14 +31,13 @@ PREVIEW_KEY = 'PREVIEW' | |||||||
| ACTIVITY_KEY = 'ACTIVITY' | ACTIVITY_KEY = 'ACTIVITY' | ||||||
| FORMATS_KEY = 'FORMATS' | FORMATS_KEY = 'FORMATS' | ||||||
| 
 | 
 | ||||||
| class ClipboardDBusServiceHelper(dbus.service.Object): | class ClipboardService(dbus.service.Object): | ||||||
| 
 | 
 | ||||||
|     _CLIPBOARD_DBUS_INTERFACE = "org.laptop.Clipboard" |     _CLIPBOARD_DBUS_INTERFACE = "org.laptop.Clipboard" | ||||||
|     _CLIPBOARD_OBJECT_PATH = "/org/laptop/Clipboard" |     _CLIPBOARD_OBJECT_PATH = "/org/laptop/Clipboard" | ||||||
|     _CLIPBOARD_OBJECTS_PATH = _CLIPBOARD_OBJECT_PATH + "/Objects/" |     _CLIPBOARD_OBJECTS_PATH = _CLIPBOARD_OBJECT_PATH + "/Objects/" | ||||||
| 
 | 
 | ||||||
|     def __init__(self, parent): |     def __init__(self): | ||||||
|         self._parent = parent |  | ||||||
|         self._objects = {} |         self._objects = {} | ||||||
|         self._next_id = 0 |         self._next_id = 0 | ||||||
| 
 | 
 | ||||||
| @ -175,13 +173,3 @@ class ClipboardDBusServiceHelper(dbus.service.Object): | |||||||
|     def object_state_changed(self, object_path, values): |     def object_state_changed(self, object_path, values): | ||||||
|         pass |         pass | ||||||
| 
 | 
 | ||||||
| class ClipboardService(object): |  | ||||||
|     def __init__(self): |  | ||||||
|         self._dbus_helper = ClipboardDBusServiceHelper(self) |  | ||||||
| 
 |  | ||||||
|     def run(self): |  | ||||||
|         loop = gobject.MainLoop() |  | ||||||
|         try: |  | ||||||
|             loop.run() |  | ||||||
|         except KeyboardInterrupt: |  | ||||||
|             print 'Ctrl+C pressed, exiting...' |  | ||||||
|  | |||||||
| @ -17,46 +17,49 @@ | |||||||
| import dbus | import dbus | ||||||
| import dbus.service | import dbus.service | ||||||
| 
 | 
 | ||||||
| from sugar.objects.objecttype import ObjectType |  | ||||||
| 
 |  | ||||||
| _REGISTRY_IFACE = "org.laptop.ObjectTypeRegistry" | _REGISTRY_IFACE = "org.laptop.ObjectTypeRegistry" | ||||||
| _REGISTRY_PATH = "/org/laptop/ObjectTypeRegistry" | _REGISTRY_PATH = "/org/laptop/ObjectTypeRegistry" | ||||||
| 
 | 
 | ||||||
| class ObjectTypeRegistry(dbus.service.Object): | class ObjectTypeRegistry(dbus.service.Object): | ||||||
|     def __init__(self): |     def __init__(self): | ||||||
|         bus = dbus.SessionBus() |         bus = dbus.SessionBus() | ||||||
|         bus_name = dbus.service.BusName(self._REGISTRY_IFACE, bus=bus) |         bus_name = dbus.service.BusName(_REGISTRY_IFACE, bus=bus) | ||||||
|         dbus.service.Object.__init__(self, bus_name, self._REGISTRY_PATH) |         dbus.service.Object.__init__(self, bus_name, _REGISTRY_PATH) | ||||||
| 
 | 
 | ||||||
|         self._types = {} |         self._types = {} | ||||||
| 
 | 
 | ||||||
|         self._add_primitive('Text', _('Text'), 'object-text', |         from gettext import gettext as _ | ||||||
|                             [ 'text/rtf' ]) |         self._add_primitive('Text', _('Text'), 'theme:object-text', | ||||||
|         self._add_primitive('Image', _('Image'), 'object-image', |                             [ 'text/plain', 'text/rtf', 'application/pdf', | ||||||
|  |                               'application/x-pdf' ]) | ||||||
|  |         self._add_primitive('Image', _('Image'), 'theme:object-image', | ||||||
|                             [ 'image/png' ]) |                             [ 'image/png' ]) | ||||||
| 
 | 
 | ||||||
|     def _add_primitive(self, type_id, name, icon, mime_types): |     def _add_primitive(self, type_id, name, icon, mime_types): | ||||||
|         object_type = ObjectType(type_id, name, icon, mime_types) |         object_type = {'type_id': type_id,  | ||||||
|         self._types.add(object_type) |                        'name': name, | ||||||
|  |                        'icon': icon, | ||||||
|  |                        'mime_types': mime_types} | ||||||
|  |         self._types[type_id] = object_type | ||||||
| 
 | 
 | ||||||
|     def _get_type_for_mime(self, mime_type): |     def _get_type_for_mime(self, mime_type): | ||||||
|         for object_type in self._types.values(): |         for object_type in self._types.values(): | ||||||
|             if mime_type in object_type.mime_types: |             if mime_type in object_type['mime_types']: | ||||||
|                 return object_type |                 return object_type | ||||||
| 
 | 
 | ||||||
|     @dbus.service.method(_CLIPBOARD_DBUS_INTERFACE, |     @dbus.service.method(_REGISTRY_IFACE, | ||||||
|                          in_signature="s", out_signature="a{sv}") |                          in_signature="s", out_signature="a{sv}") | ||||||
|     def GetType(self, type_id): |     def GetType(self, type_id): | ||||||
|         if self._types.has_key(type_id): |         if self._types.has_key(type_id): | ||||||
|             return self._types[type_id].to_dict() |             return self._types[type_id] | ||||||
|         else: |         else: | ||||||
|             return [] |             return {} | ||||||
| 
 | 
 | ||||||
|     @dbus.service.method(_CLIPBOARD_DBUS_INTERFACE, |     @dbus.service.method(_REGISTRY_IFACE, | ||||||
|                          in_signature="s", out_signature="a{sv}") |                          in_signature="s", out_signature="a{sv}") | ||||||
|     def GetTypeForMIME(self, mime_type): |     def GetTypeForMIME(self, mime_type): | ||||||
|         object_type = self._get_type_for_mime(mime_type) |         object_type = self._get_type_for_mime(mime_type) | ||||||
|         if object_type: |         if object_type: | ||||||
|             return object_type.to_dict() |             return object_type | ||||||
|         else: |         else: | ||||||
|             return [] |             return {} | ||||||
|  | |||||||
| @ -35,11 +35,18 @@ from sugar import env | |||||||
| sys.path.append(env.get_service_path('clipboard')) | sys.path.append(env.get_service_path('clipboard')) | ||||||
| 
 | 
 | ||||||
| from clipboardservice import ClipboardService | from clipboardservice import ClipboardService | ||||||
|  | from objecttypeservice import ObjectTypeRegistry | ||||||
| 
 | 
 | ||||||
| logging.info('Starting clipboard service.') | logging.info('Starting clipboard service.') | ||||||
| 
 | 
 | ||||||
| gobject.threads_init() | gobject.threads_init() | ||||||
| dbus.glib.threads_init() | dbus.glib.threads_init() | ||||||
| 
 | 
 | ||||||
| app = ClipboardService() | clipboard_service = ClipboardService() | ||||||
| app.run() | object_type_registry = ObjectTypeRegistry() | ||||||
|  | 
 | ||||||
|  | loop = gobject.MainLoop() | ||||||
|  | try: | ||||||
|  |     loop.run() | ||||||
|  | except KeyboardInterrupt: | ||||||
|  |     print 'Ctrl+C pressed, exiting...' | ||||||
|  | |||||||
| @ -17,9 +17,6 @@ | |||||||
| """D-bus service providing access to the shell's functionality""" | """D-bus service providing access to the shell's functionality""" | ||||||
| import dbus | import dbus | ||||||
| 
 | 
 | ||||||
| from sugar.activity import ActivityRegistry |  | ||||||
| from sugar.activity import ActivityInfo |  | ||||||
| 
 |  | ||||||
| from model import bundleregistry | from model import bundleregistry | ||||||
| 
 | 
 | ||||||
| _DBUS_SERVICE = "org.laptop.Shell" | _DBUS_SERVICE = "org.laptop.Shell" | ||||||
| @ -76,9 +73,19 @@ class ShellService(dbus.service.Object): | |||||||
|         registry = bundleregistry.get_registry() |         registry = bundleregistry.get_registry() | ||||||
|         return registry.add_bundle(bundle_path) |         return registry.add_bundle(bundle_path) | ||||||
| 
 | 
 | ||||||
|  |     @dbus.service.method(_DBUS_ACTIVITY_REGISTRY_IFACE, | ||||||
|  |                          in_signature="s", out_signature="a{sv}") | ||||||
|  |     def GetActivity(self, service_name): | ||||||
|  |         registry = bundleregistry.get_registry() | ||||||
|  |         bundle = registry.get_bundle(service_name) | ||||||
|  |         if not bundle: | ||||||
|  |             return {} | ||||||
|  |          | ||||||
|  |         return self._bundle_to_dict(bundle) | ||||||
|  | 
 | ||||||
|     @dbus.service.method(_DBUS_ACTIVITY_REGISTRY_IFACE, |     @dbus.service.method(_DBUS_ACTIVITY_REGISTRY_IFACE, | ||||||
|                          in_signature="s", out_signature="aa{sv}") |                          in_signature="s", out_signature="aa{sv}") | ||||||
|     def GetActivitiesForName(self, name): |     def FindActivity(self, name): | ||||||
|         result = [] |         result = [] | ||||||
|         key = name.lower() |         key = name.lower() | ||||||
| 
 | 
 | ||||||
| @ -86,8 +93,7 @@ class ShellService(dbus.service.Object): | |||||||
|             name = bundle.get_name().lower() |             name = bundle.get_name().lower() | ||||||
|             service_name = bundle.get_service_name().lower() |             service_name = bundle.get_service_name().lower() | ||||||
|             if name.find(key) != -1 or service_name.find(key) != -1: |             if name.find(key) != -1 or service_name.find(key) != -1: | ||||||
|                 info = self._bundle_to_activity_info(bundle) |                 result.append(self._bundle_to_dict(bundle)) | ||||||
|                 result.append(info.to_dict()) |  | ||||||
| 
 | 
 | ||||||
|         return result |         return result | ||||||
| 
 | 
 | ||||||
| @ -97,9 +103,8 @@ class ShellService(dbus.service.Object): | |||||||
|         result = [] |         result = [] | ||||||
| 
 | 
 | ||||||
|         for bundle in bundleregistry.get_registry(): |         for bundle in bundleregistry.get_registry(): | ||||||
|             if mime_type in bundle.get_mime_types(): |             if bundle.get_mime_types() and mime_type in bundle.get_mime_types(): | ||||||
|                 info = self._bundle_to_activity_info(bundle) |                 result.append(self._bundle_to_dict(bundle)) | ||||||
|                 result.append(info.to_dict()) |  | ||||||
| 
 | 
 | ||||||
|         return result |         return result | ||||||
| 
 | 
 | ||||||
| @ -135,6 +140,8 @@ class ShellService(dbus.service.Object): | |||||||
|         if new_id: |         if new_id: | ||||||
|             self.CurrentActivityChanged(new_id) |             self.CurrentActivityChanged(new_id) | ||||||
| 
 | 
 | ||||||
|     def _bundle_to_activity_info(self, bundle): |     def _bundle_to_dict(self, bundle): | ||||||
|         return ActivityInfo(bundle.get_name(), bundle.get_icon(), |         return {'name': bundle.get_name(), | ||||||
|                             bundle.get_service_name(), bundle.get_path()) |                 'icon': bundle.get_icon(), | ||||||
|  |                 'service_name': bundle.get_service_name(), | ||||||
|  |                 'path': bundle.get_path()} | ||||||
|  | |||||||
| @ -37,5 +37,3 @@ window, requesting sharing across the network, and basic | |||||||
| "what type of application are you" queries. | "what type of application are you" queries. | ||||||
| """ | """ | ||||||
| 
 | 
 | ||||||
| from sugar.activity.registry import ActivityRegistry |  | ||||||
| from sugar.activity.registry import ActivityInfo |  | ||||||
|  | |||||||
| @ -93,7 +93,7 @@ class Bundle: | |||||||
|             logging.error('%s must specify exec or class' % self._path) |             logging.error('%s must specify exec or class' % self._path) | ||||||
| 
 | 
 | ||||||
|         if cp.has_option(section, 'mime_types'): |         if cp.has_option(section, 'mime_types'): | ||||||
|             mime_list = cp.get(section, 'show_launcher') |             mime_list = cp.get(section, 'mime_types') | ||||||
|             self._mime_types = mime_list.strip(';') |             self._mime_types = mime_list.strip(';') | ||||||
| 
 | 
 | ||||||
|         if cp.has_option(section, 'show_launcher'): |         if cp.has_option(section, 'show_launcher'): | ||||||
|  | |||||||
| @ -32,13 +32,6 @@ class ActivityInfo(object): | |||||||
|         self.service_name = service_name |         self.service_name = service_name | ||||||
|         self.path = path |         self.path = path | ||||||
| 
 | 
 | ||||||
|     def to_dict(self): |  | ||||||
|         return { 'name'         : self.name, |  | ||||||
|                  'icon'         : self.icon, |  | ||||||
|                  'service_name' : self.service_name, |  | ||||||
|                  'path'         : self.path |  | ||||||
|                 } |  | ||||||
| 
 |  | ||||||
| class ActivityRegistry(object): | class ActivityRegistry(object): | ||||||
|     def __init__(self): |     def __init__(self): | ||||||
|         bus = dbus.SessionBus() |         bus = dbus.SessionBus() | ||||||
| @ -53,10 +46,19 @@ class ActivityRegistry(object): | |||||||
| 
 | 
 | ||||||
|         return result |         return result | ||||||
| 
 | 
 | ||||||
|     def get_activities_for_name(self, name): |     def get_activity(self, service_name): | ||||||
|         info_list = self._registry.GetActivitiesForName(name) |         info_dict = self._registry.GetActivity(service_name) | ||||||
|  |         return _activity_info_from_dict(info_dict) | ||||||
|  | 
 | ||||||
|  |     def find_activity(self, 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): | ||||||
|         info_list = self._registry.GetActivitiesForType(mime_type) |         info_list = self._registry.GetActivitiesForType(mime_type) | ||||||
|         return self._convert_info_list(info_list) |         return self._convert_info_list(info_list) | ||||||
|  | 
 | ||||||
|  | _registry = ActivityRegistry() | ||||||
|  | 
 | ||||||
|  | def get_registry(): | ||||||
|  |     return _registry | ||||||
|  | |||||||
| @ -25,9 +25,17 @@ class DSMetadata(gobject.GObject): | |||||||
|                     ([])) |                     ([])) | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     def __init__(self, props={}): |     def __init__(self, props=None): | ||||||
|         gobject.GObject.__init__(self) |         gobject.GObject.__init__(self) | ||||||
|         self._props = props |         if not props: | ||||||
|  |             self._props = {} | ||||||
|  |         else: | ||||||
|  |             self._props = props | ||||||
|  |          | ||||||
|  |         default_keys = ['activity', 'mime_type'] | ||||||
|  |         for key in default_keys: | ||||||
|  |             if not self._props.has_key(key): | ||||||
|  |                 self._props[key] = '' | ||||||
| 
 | 
 | ||||||
|     def __getitem__(self, key): |     def __getitem__(self, key): | ||||||
|         return self._props[key] |         return self._props[key] | ||||||
| @ -88,7 +96,7 @@ def create(): | |||||||
|     return DSObject(object_id=None, metadata=DSMetadata(), 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: %r' % ds_object.metadata.get_dictionary()) | ||||||
|     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.get_dictionary(), |                             ds_object.metadata.get_dictionary(), | ||||||
|  | |||||||
| @ -1,3 +1,21 @@ | |||||||
|  | # Copyright (C) 2007, Red Hat, Inc. | ||||||
|  | # | ||||||
|  | # This program is free software; you can redistribute it and/or modify | ||||||
|  | # it under the terms of the GNU General Public License as published by | ||||||
|  | # the Free Software Foundation; either version 2 of the License, or | ||||||
|  | # (at your option) any later version. | ||||||
|  | # | ||||||
|  | # This program is distributed in the hope that it will be useful, | ||||||
|  | # but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||||
|  | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | ||||||
|  | # GNU General Public License for more details. | ||||||
|  | # | ||||||
|  | # You should have received a copy of the GNU General Public License | ||||||
|  | # along with this program; if not, write to the Free Software | ||||||
|  | # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA | ||||||
|  | 
 | ||||||
|  | import dbus | ||||||
|  | 
 | ||||||
| _SERVICE = "org.laptop.ObjectTypeRegistry" | _SERVICE = "org.laptop.ObjectTypeRegistry" | ||||||
| _PATH = "/org/laptop/ObjectTypeRegistry" | _PATH = "/org/laptop/ObjectTypeRegistry" | ||||||
| _IFACE = "org.laptop.ObjectTypeRegistry" | _IFACE = "org.laptop.ObjectTypeRegistry" | ||||||
| @ -11,33 +29,27 @@ def _object_type_from_dict(info_dict): | |||||||
|         return None |         return None | ||||||
| 
 | 
 | ||||||
| class ObjectType(object): | class ObjectType(object): | ||||||
|     def __init__(self, type_id, name, icon, mime_types): |     def __init__(self, type_id, name, icon): | ||||||
|         self.type_id = type_id |         self.type_id = type_id | ||||||
|         self.name = name |         self.name = name | ||||||
|         self.icon = icon |         self.icon = icon | ||||||
|         self.mime_types = [] |         self.mime_types = [] | ||||||
| 
 | 
 | ||||||
|     def to_dict(self): |  | ||||||
|         return { 'type_id' : self.type_id, |  | ||||||
|                  'name'    : self.name, |  | ||||||
|                  'icon'    : self.icon |  | ||||||
|                 } |  | ||||||
| 
 |  | ||||||
| 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) | ||||||
| 
 | 
 | ||||||
|     def get_type(type_id): |     def get_type(self, type_id): | ||||||
|         type_dict = self._registry.GetType(type_id) |         type_dict = self._registry.GetType(type_id) | ||||||
|         return _object_type_from_dict(type_dict) |         return _object_type_from_dict(type_dict) | ||||||
| 
 | 
 | ||||||
|     def get_type_for_mime(mime_type): |     def get_type_for_mime(self, mime_type): | ||||||
|         type_dict = self._registry.GetTypeForMime(type_id) |         type_dict = self._registry.GetTypeForMIME(mime_type) | ||||||
|         return _object_type_from_dict(type_dict) |         return _object_type_from_dict(type_dict) | ||||||
| 
 | 
 | ||||||
| _registry = ObjectRegistry() | _registry = ObjectTypeRegistry() | ||||||
| 
 | 
 | ||||||
| def get_registry(): | def get_registry(): | ||||||
|     return _registry |     return _registry | ||||||
|  | |||||||
		Loading…
	
		Reference in New Issue
	
	Block a user