import os from ConfigParser import ConfigParser import gobject from sugar.activity.bundle import Bundle 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/' ] class _ServiceManager(object): def __init__(self): service_dir = os.path.expanduser('~/.local/share/dbus-1/services') if not os.path.isdir(service_dir): os.makedirs(service_dir) self._path = service_dir def add(self, bundle): util.write_service(bundle.get_service_name(), bundle.get_exec(), self._path) class BundleRegistry(gobject.GObject): """Service that tracks the available activity bundles""" __gsignals__ = { 'bundle-added': (gobject.SIGNAL_RUN_FIRST, gobject.TYPE_NONE, ([gobject.TYPE_PYOBJECT])) } def __init__(self): gobject.GObject.__init__(self) self._bundles = {} self._search_path = [] self._service_manager = _ServiceManager() def get_bundle(self, service_name): """Returns an bundle given his service name""" if self._bundles.has_key(service_name): return self._bundles[service_name] else: return None def find_by_default_type(self, default_type): """Find a bundle by the network service default type""" for bundle in self._bundles.values(): if bundle.get_default_type() == default_type: 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.values().__iter__() def _scan_directory(self, path): if os.path.isdir(path): for f in os.listdir(path): bundle_dir = os.path.join(path, f) if os.path.isdir(bundle_dir) and \ bundle_dir.endswith('.activity'): self.add_bundle(bundle_dir) def add_bundle(self, bundle_path): bundle = Bundle(bundle_path) if bundle.is_valid(): self._bundles[bundle.get_service_name()] = bundle self._service_manager.add(bundle) self.emit('bundle-added', bundle) return True else: return False 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())