2006-10-29 19:05:09 +01:00
|
|
|
import os
|
2006-10-31 10:48:45 +01:00
|
|
|
from ConfigParser import ConfigParser
|
2007-01-27 12:54:56 +01:00
|
|
|
import gobject
|
2006-10-29 19:05:09 +01:00
|
|
|
|
2006-10-29 18:28:48 +01:00
|
|
|
from sugar.activity.bundle import Bundle
|
2006-10-31 12:06:28 +01:00
|
|
|
from sugar import env
|
2006-12-01 23:23:58 +01:00
|
|
|
from sugar import util
|
2006-10-31 10:48:45 +01:00
|
|
|
|
|
|
|
class _ServiceManager(object):
|
2006-12-04 20:12:24 +01:00
|
|
|
def __init__(self):
|
|
|
|
self._path = env.get_user_service_dir()
|
2006-10-31 10:48:45 +01:00
|
|
|
|
2006-12-04 20:12:24 +01:00
|
|
|
def add(self, bundle):
|
|
|
|
name = bundle.get_service_name()
|
2006-10-31 10:48:45 +01:00
|
|
|
|
2006-12-04 20:12:24 +01:00
|
|
|
# FIXME evil hack. Probably need to fix Exec spec
|
|
|
|
full_exec = env.get_shell_bin_dir() + '/' + bundle.get_exec()
|
|
|
|
full_exec += ' ' + bundle.get_path()
|
2006-10-31 10:48:45 +01:00
|
|
|
|
2006-12-04 20:12:24 +01:00
|
|
|
util.write_service(name, full_exec, self._path)
|
2006-10-31 10:48:45 +01:00
|
|
|
|
2007-01-27 12:54:56 +01:00
|
|
|
class BundleRegistry(gobject.GObject):
|
2006-12-04 20:12:24 +01:00
|
|
|
"""Service that tracks the available activity bundles"""
|
|
|
|
|
2007-01-27 12:54:56 +01:00
|
|
|
__gsignals__ = {
|
|
|
|
'bundle-added': (gobject.SIGNAL_RUN_FIRST, gobject.TYPE_NONE,
|
|
|
|
([gobject.TYPE_PYOBJECT]))
|
|
|
|
}
|
|
|
|
|
2006-12-04 20:12:24 +01:00
|
|
|
def __init__(self):
|
2007-01-27 12:54:56 +01:00
|
|
|
gobject.GObject.__init__(self)
|
|
|
|
|
2006-12-04 20:12:24 +01:00
|
|
|
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
|
|
|
|
|
2006-12-20 13:58:29 +01:00
|
|
|
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
|
|
|
|
|
2006-12-04 20:12:24 +01:00
|
|
|
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'):
|
2007-01-27 12:54:56 +01:00
|
|
|
self.add_bundle(bundle_dir)
|
2006-12-04 20:12:24 +01:00
|
|
|
|
2007-01-27 12:54:56 +01:00
|
|
|
def add_bundle(self, bundle_path):
|
2006-12-04 20:12:24 +01:00
|
|
|
bundle = Bundle(bundle_path)
|
|
|
|
if bundle.is_valid():
|
|
|
|
self._bundles[bundle.get_service_name()] = bundle
|
|
|
|
self._service_manager.add(bundle)
|
2007-01-27 12:54:56 +01:00
|
|
|
self.emit('bundle-added', bundle)
|
|
|
|
return True
|
|
|
|
else:
|
|
|
|
return False
|
2007-02-21 17:53:44 +01:00
|
|
|
|
|
|
|
def get_registry():
|
|
|
|
return _bundle_registry
|
|
|
|
|
|
|
|
_bundle_registry = BundleRegistry()
|
|
|
|
|
|
|
|
for path in env.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_dir())
|