Refactor directory structure a bit, preliminary to the

library split-out.
This commit is contained in:
Marco Pesenti Gritti
2008-02-04 23:36:12 +01:00
parent 652df4bf51
commit ae5ce06ccb
105 changed files with 15 additions and 17 deletions
+34
View File
@@ -0,0 +1,34 @@
servicedir = $(datadir)/dbus-1/services
service_in_files = \
org.laptop.ActivityRegistry.service.in \
org.laptop.Clipboard.service.in
service_DATA = \
org.laptop.ActivityRegistry.service \
org.laptop.Clipboard.service
org.laptop.ActivityRegistry.service: org.laptop.ActivityRegistry.service.in Makefile
@sed -e "s|\@bindir\@|$(bindir)|" $< > $@
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)|" $< > $@
sugardir = $(pkgdatadir)/services/shell
sugar_PYTHON = \
__init__.py \
activityregistryservice.py \
bundleregistry.py \
clipboardobject.py \
clipboardservice.py
bin_SCRIPTS = sugar-shell-service
DISTCLEANFILES = $(service_DATA)
EXTRA_DIST = $(service_in_files) $(bin_SCRIPTS)
+16
View File
@@ -0,0 +1,16 @@
# Copyright (C) 2007, One Laptop Per Child
#
# 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
+134
View File
@@ -0,0 +1,134 @@
# Copyright (C) 2006-2007 Red Hat, Inc.
# Copyright (C) 2007 One Laptop Per Child
#
# 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
import dbus.service
import bundleregistry
_ACTIVITY_REGISTRY_SERVICE_NAME = 'org.laptop.ActivityRegistry'
_ACTIVITY_REGISTRY_IFACE = 'org.laptop.ActivityRegistry'
_ACTIVITY_REGISTRY_PATH = '/org/laptop/ActivityRegistry'
class ActivityRegistry(dbus.service.Object):
def __init__(self):
bus = dbus.SessionBus()
bus_name = dbus.service.BusName(_ACTIVITY_REGISTRY_SERVICE_NAME, bus=bus)
dbus.service.Object.__init__(self, bus_name, _ACTIVITY_REGISTRY_PATH)
bundle_registry = bundleregistry.get_registry()
bundle_registry.connect('bundle-added', self._bundle_added_cb)
bundle_registry.connect('bundle-removed', self._bundle_removed_cb)
@dbus.service.method(_ACTIVITY_REGISTRY_IFACE,
in_signature='s', out_signature='b')
def AddBundle(self, bundle_path):
'''Register the activity bundle with the global registry
bundle_path -- path to the root directory of the activity bundle,
that is, the directory with activity/activity.info as a
child of the directory.
The bundleregistry.BundleRegistry is responsible for setting
up a set of d-bus service mappings for each available activity.
'''
registry = bundleregistry.get_registry()
return registry.add_bundle(bundle_path)
@dbus.service.method(_ACTIVITY_REGISTRY_IFACE,
in_signature='s', out_signature='b')
def RemoveBundle(self, bundle_path):
'''Unregister the activity bundle with the global registry
bundle_path -- path to the activity bundle root directory
'''
registry = bundleregistry.get_registry()
return registry.remove_bundle(bundle_path)
@dbus.service.method(_ACTIVITY_REGISTRY_IFACE,
in_signature='', out_signature='aa{sv}')
def GetActivities(self):
result = []
registry = bundleregistry.get_registry()
for bundle in registry:
result.append(self._bundle_to_dict(bundle))
return result
@dbus.service.method(_ACTIVITY_REGISTRY_IFACE,
in_signature='s', out_signature='a{sv}')
def GetActivity(self, bundle_id):
registry = bundleregistry.get_registry()
bundle = registry.get_bundle(bundle_id)
if not bundle:
return {}
return self._bundle_to_dict(bundle)
@dbus.service.method(_ACTIVITY_REGISTRY_IFACE,
in_signature='s', out_signature='aa{sv}')
def FindActivity(self, name):
result = []
key = name.lower()
for bundle in bundleregistry.get_registry():
name = bundle.get_name().lower()
bundle_id = bundle.get_bundle_id().lower()
if name.find(key) != -1 or bundle_id.find(key) != -1:
result.append(self._bundle_to_dict(bundle))
return result
@dbus.service.method(_ACTIVITY_REGISTRY_IFACE,
in_signature='s', out_signature='aa{sv}')
def GetActivitiesForType(self, mime_type):
result = []
registry = bundleregistry.get_registry()
for bundle in registry.get_activities_for_type(mime_type):
result.append(self._bundle_to_dict(bundle))
return result
@dbus.service.signal(_ACTIVITY_REGISTRY_IFACE, signature='a{sv}')
def ActivityAdded(self, activity_info):
pass
@dbus.service.signal(_ACTIVITY_REGISTRY_IFACE, signature='a{sv}')
def ActivityRemoved(self, activity_info):
pass
def _bundle_to_dict(self, bundle):
return {'name': bundle.get_name(),
'icon': bundle.get_icon(),
'bundle_id': bundle.get_bundle_id(),
'version': bundle.get_activity_version(),
'path': bundle.get_path(),
'command': bundle.get_command(),
'show_launcher': bundle.get_show_launcher()}
def _bundle_added_cb(self, bundle_registry, bundle):
self.ActivityAdded(self._bundle_to_dict(bundle))
def _bundle_removed_cb(self, bundle_registry, bundle):
self.ActivityRemoved(self._bundle_to_dict(bundle))
_instance = None
def get_instance():
global _instance
if not _instance:
_instance = ActivityRegistry()
return _instance
+149
View File
@@ -0,0 +1,149 @@
# Copyright (C) 2006-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 os
import logging
import gobject
from sugar.bundle.activitybundle import ActivityBundle
from sugar.bundle.bundle import MalformedBundleException
from sugar import env
from sugar import util
# http://standards.freedesktop.org/basedir-spec/basedir-spec-0.6.html
def _get_data_dirs():
if os.environ.has_key('XDG_DATA_DIRS'):
return os.environ['XDG_DATA_DIRS'].split(':')
else:
return [ '/usr/local/share/', '/usr/share/' ]
def _load_mime_defaults():
defaults = {}
f = open(env.get_data_path('mime.defaults'), 'r')
for line in f.readlines():
line = line.strip()
if line and not line.startswith('#'):
mime = line[:line.find(' ')]
handler = line[line.rfind(' ') + 1:]
defaults[mime] = handler
f.close()
return defaults
class BundleRegistry(gobject.GObject):
"""Service that tracks the available activity bundles"""
__gsignals__ = {
'bundle-added': (gobject.SIGNAL_RUN_FIRST, gobject.TYPE_NONE,
([gobject.TYPE_PYOBJECT])),
'bundle-removed': (gobject.SIGNAL_RUN_FIRST, gobject.TYPE_NONE,
([gobject.TYPE_PYOBJECT]))
}
def __init__(self):
gobject.GObject.__init__(self)
self._bundles = []
self._search_path = []
self._mime_defaults = _load_mime_defaults()
def get_bundle(self, bundle_id):
"""Returns an bundle given his service name"""
for bundle in self._bundles:
if bundle.get_bundle_id() == bundle_id:
return bundle
return None
def add_search_path(self, path):
"""Add a directory to the bundles search path"""
self._search_path.append(path)
self._scan_directory(path)
def __iter__(self):
return self._bundles.__iter__()
def _scan_directory(self, path):
if not os.path.isdir(path):
return
# Sort by mtime to ensure a stable activity order
bundles = {}
for f in os.listdir(path):
if not f.endswith('.activity'):
continue
try:
bundle_dir = os.path.join(path, f)
if os.path.isdir(bundle_dir):
bundles[bundle_dir] = os.stat(bundle_dir).st_mtime
except Exception, e:
logging.error('Error while processing installed activity ' \
'bundle: %s, %s, %s' % (f, e.__class__, e))
bundle_dirs = bundles.keys()
bundle_dirs.sort(lambda d1,d2: cmp(bundles[d1], bundles[d2]))
for dir in bundle_dirs:
try:
self.add_bundle(dir)
except Exception, e:
logging.error('Error while processing installed activity ' \
'bundle: %s, %s, %s' % (dir, e.__class__, e))
def add_bundle(self, bundle_path):
try:
bundle = ActivityBundle(bundle_path)
except MalformedBundleException:
return False
self._bundles.append(bundle)
self.emit('bundle-added', bundle)
return True
def remove_bundle(self, bundle_path):
for bundle in self._bundles:
if bundle.get_path() == bundle_path:
self._bundles.remove(bundle)
self.emit('bundle-removed', bundle)
return True
return False
def get_activities_for_type(self, mime_type):
result = []
for bundle in self._bundles:
if bundle.get_mime_types() and mime_type in bundle.get_mime_types():
if self.get_default_for_type(mime_type) == bundle.get_bundle_id():
result.insert(0, bundle)
else:
result.append(bundle)
return result
def get_default_for_type(self, mime_type):
if self._mime_defaults.has_key(mime_type):
return self._mime_defaults[mime_type]
else:
return None
def get_registry():
return _bundle_registry
_bundle_registry = BundleRegistry()
for path in _get_data_dirs():
bundles_path = os.path.join(path, 'activities')
_bundle_registry.add_search_path(bundles_path)
_bundle_registry.add_search_path(env.get_user_activities_path())
+122
View File
@@ -0,0 +1,122 @@
# Copyright (C) 2007, One Laptop Per Child
#
# 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 os
import logging
import urlparse
from sugar import mime
import bundleregistry
class ClipboardObject:
def __init__(self, object_path, name):
self._id = object_path
self._name = name
self._percent = 0
self._formats = {}
def destroy(self):
for type, format in self._formats.iteritems():
format.destroy()
def get_id(self):
return self._id
def get_name(self):
name = self._name
if not name:
name = mime.get_mime_description(self.get_mime_type())
if not name:
name = ''
return name
def get_icon(self):
return mime.get_mime_icon(self.get_mime_type())
def get_preview(self):
# TODO: should previews really be here?
#return self._get_type_info().get_preview()
return ''
def get_activities(self):
mime = self.get_mime_type()
if not mime:
return ''
registry = bundleregistry.get_registry()
activities = registry.get_activities_for_type(self.get_mime_type())
if activities:
return [activity.get_bundle_id() for activity in activities]
else:
return ''
def get_percent(self):
return self._percent
def set_percent(self, percent):
self._percent = percent
def add_format(self, format):
self._formats[format.get_type()] = format
def get_formats(self):
return self._formats
def get_mime_type(self):
if not self._formats:
return ''
format = mime.choose_most_significant(self._formats.keys())
if format == 'text/uri-list':
data = self._formats['text/uri-list'].get_data()
uri = urlparse.urlparse(mime.split_uri_list(data)[0], 'file')
if uri.scheme == 'file':
if os.path.exists(uri.path):
format = mime.get_for_file(uri.path)
else:
format = mime.get_from_file_name(uri.path)
logging.debug('Choosed %r!' % format)
return format
class Format:
def __init__(self, type, data, on_disk):
self.owns_disk_data = False
self._type = type
self._data = data
self._on_disk = on_disk
def destroy(self):
if self._on_disk:
uri = urlparse.urlparse(self._data)
if os.path.isfile(uri.path):
os.remove(uri.path)
def get_type(self):
return self._type
def get_data(self):
return self._data
def set_data(self, data):
self._data = data
def is_on_disk(self):
return self._on_disk
+212
View File
@@ -0,0 +1,212 @@
# Copyright (C) 2006, 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 logging
import os
import shutil
import urlparse
import tempfile
import dbus
import dbus.service
from sugar import env
from sugar import util
from sugar import mime
from clipboardobject import ClipboardObject, Format
NAME_KEY = 'NAME'
PERCENT_KEY = 'PERCENT'
ICON_KEY = 'ICON'
PREVIEW_KEY = 'PREVIEW'
ACTIVITIES_KEY = 'ACTIVITIES'
FORMATS_KEY = 'FORMATS'
TYPE_KEY = 'TYPE'
DATA_KEY = 'DATA'
ON_DISK_KEY = 'ON_DISK'
class ClipboardService(dbus.service.Object):
_CLIPBOARD_DBUS_INTERFACE = "org.laptop.Clipboard"
_CLIPBOARD_OBJECT_PATH = "/org/laptop/Clipboard"
_CLIPBOARD_OBJECTS_PATH = _CLIPBOARD_OBJECT_PATH + "/Objects/"
def __init__(self):
self._objects = {}
self._next_id = 0
bus = dbus.SessionBus()
bus_name = dbus.service.BusName(self._CLIPBOARD_DBUS_INTERFACE, bus=bus)
dbus.service.Object.__init__(self, bus_name, self._CLIPBOARD_OBJECT_PATH)
def _get_next_object_id(self):
self._next_id += 1
return self._next_id
# dbus methods
@dbus.service.method(_CLIPBOARD_DBUS_INTERFACE,
in_signature="s", out_signature="o")
def add_object(self, name):
logging.debug('ClipboardService.add_object')
op = self._CLIPBOARD_OBJECTS_PATH + "%d" % self._get_next_object_id()
self._objects[op] = ClipboardObject(op, name)
self.object_added(dbus.ObjectPath(op), name)
logging.debug('Added object ' + op + ' with name ' + name)
return dbus.ObjectPath(op)
@dbus.service.method(_CLIPBOARD_DBUS_INTERFACE,
in_signature="ssayb", out_signature="", byte_arrays=True)
def add_object_format(self, object_path, format_type, data, on_disk):
logging.debug('ClipboardService.add_object_format')
cb_object = self._objects[str(object_path)]
if format_type == 'XdndDirectSave0':
format = Format('text/uri-list', data + '\r\n', on_disk)
format.owns_disk_data = True
cb_object.add_format(format)
elif on_disk and cb_object.get_percent() == 100:
new_uri = self._copy_file(data)
cb_object.add_format(Format(format_type, new_uri, on_disk))
logging.debug('Added format of type ' + format_type + ' with path at ' + new_uri)
else:
cb_object.add_format(Format(format_type, data, on_disk))
logging.debug('Added in-memory format of type ' + format_type + '.')
self.object_state_changed(object_path, {NAME_KEY: cb_object.get_name(),
PERCENT_KEY: cb_object.get_percent(),
ICON_KEY: cb_object.get_icon(),
PREVIEW_KEY: cb_object.get_preview(),
ACTIVITIES_KEY: cb_object.get_activities()})
@dbus.service.method(_CLIPBOARD_DBUS_INTERFACE,
in_signature="o", out_signature="")
def delete_object(self, object_path):
cb_object = self._objects.pop(str(object_path))
cb_object.destroy()
self.object_deleted(object_path)
logging.debug('Deleted object with object_id ' + object_path)
@dbus.service.method(_CLIPBOARD_DBUS_INTERFACE,
in_signature="oi", out_signature="")
def set_object_percent(self, object_path, percent):
cb_object = self._objects[str(object_path)]
if percent < 0 or percent > 100:
raise ValueError("invalid percentage")
if cb_object.get_percent() > percent:
raise ValueError("invalid percentage; less than current percent")
if cb_object.get_percent() == percent:
# ignore setting same percentage
return
cb_object.set_percent(percent)
if percent == 100:
formats = cb_object.get_formats()
for format_name, format in formats.iteritems():
if format.is_on_disk() and not format.owns_disk_data:
new_uri = self._copy_file(format.get_data())
format.set_data(new_uri)
# Add a text/plain format to objects that are text but lack it
if 'text/plain' not in formats.keys():
if 'UTF8_STRING' in formats.keys():
self.add_object_format(object_path,
'text/plain',
data=formats['UTF8_STRING'].get_data(),
on_disk=False)
elif 'text/unicode' in formats.keys():
self.add_object_format(object_path,
'text/plain',
data=formats['UTF8_STRING'].get_data(),
on_disk=False)
self.object_state_changed(object_path, {NAME_KEY: cb_object.get_name(),
PERCENT_KEY: percent,
ICON_KEY: cb_object.get_icon(),
PREVIEW_KEY: cb_object.get_preview(),
ACTIVITIES_KEY: cb_object.get_activities()})
@dbus.service.method(_CLIPBOARD_DBUS_INTERFACE,
in_signature="o", out_signature="a{sv}")
def get_object(self, object_path):
logging.debug('ClipboardService.get_object')
if not self._objects.has_key(str(object_path)):
return dbus.Dictionary({}, signature='sv')
cb_object = self._objects[str(object_path)]
formats = cb_object.get_formats()
format_types = dbus.Array([], signature='s')
for type, format in formats.iteritems():
format_types.append(type)
result_dict = {NAME_KEY: cb_object.get_name(),
PERCENT_KEY: cb_object.get_percent(),
ICON_KEY: cb_object.get_icon(),
PREVIEW_KEY: cb_object.get_preview(),
ACTIVITIES_KEY: cb_object.get_activities(),
FORMATS_KEY: format_types}
return dbus.Dictionary(result_dict)
@dbus.service.method(_CLIPBOARD_DBUS_INTERFACE,
in_signature="os", out_signature="a{sv}")
def get_object_data(self, object_path, format_type):
logging.debug('ClipboardService.get_object_data')
cb_object = self._objects[str(object_path)]
format = cb_object.get_formats()[format_type]
result_dict = {TYPE_KEY: format.get_type(),
DATA_KEY: dbus.ByteArray(format.get_data()),
ON_DISK_KEY: format.is_on_disk()}
return dbus.Dictionary(result_dict)
# dbus signals
@dbus.service.signal(_CLIPBOARD_DBUS_INTERFACE, signature="os")
def object_added(self, object_path, name):
pass
@dbus.service.signal(_CLIPBOARD_DBUS_INTERFACE, signature="o")
def object_deleted(self, object_path):
pass
@dbus.service.signal(_CLIPBOARD_DBUS_INTERFACE, signature="oa{sv}")
def object_state_changed(self, object_path, values):
pass
def _copy_file(self, original_uri):
uri = urlparse.urlparse(original_uri)
path, file_name = os.path.split(uri.path)
root, ext = os.path.splitext(file_name)
if not ext or ext == '.':
mime_type = mime.get_for_file(uri.path)
ext = '.' + mime.get_primary_extension(mime_type)
f, new_file_path = tempfile.mkstemp(ext, root)
del f
shutil.copyfile(uri.path, new_file_path)
return 'file://' + new_file_path
_instance = None
def get_instance():
global _instance
if not _instance:
_instance = ClipboardService()
return _instance
@@ -0,0 +1,4 @@
[D-BUS Service]
Name = org.laptop.ActivityRegistry
Exec = @bindir@/sugar-shell-service
+4
View File
@@ -0,0 +1,4 @@
[D-BUS Service]
Name = org.laptop.Clipboard
Exec = @bindir@/sugar-shell-service
@@ -0,0 +1,4 @@
[D-BUS Service]
Name = org.laptop.ObjectTypeRegistry
Exec = @bindir@/sugar-shell-service
+50
View File
@@ -0,0 +1,50 @@
#!/usr/bin/env python
# vi: ts=4 ai noet
#
# Copyright (C) 2006, Red Hat, Inc.
# Copyright (C) 2007, One Laptop Per Child
#
# 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 sys
import os
import logging
import gobject
import dbus.glib
from sugar import logger
from sugar import env
sys.path.append(env.get_service_path('shell'))
import clipboardservice
import activityregistryservice
logger.start('shellservice')
logging.info('Starting shell service.')
gobject.threads_init()
dbus.glib.threads_init()
clipboard_service = clipboardservice.get_instance()
activity_registry = activityregistryservice.get_instance()
loop = gobject.MainLoop()
try:
loop.run()
except KeyboardInterrupt:
print 'Ctrl+C pressed, exiting...'