2006-10-29 19:05:09 +01:00
|
|
|
import os
|
2006-10-31 10:48:45 +01:00
|
|
|
from ConfigParser import ConfigParser
|
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-10-29 18:28:48 +01:00
|
|
|
|
2006-10-31 10:48:45 +01:00
|
|
|
class _ServiceParser(ConfigParser):
|
|
|
|
def optionxform(self, option):
|
|
|
|
return option
|
|
|
|
|
|
|
|
class _ServiceManager(object):
|
|
|
|
def __init__(self):
|
2006-12-01 21:24:24 +01:00
|
|
|
self._path = env.get_user_service_dir()
|
2006-10-31 10:48:45 +01:00
|
|
|
|
|
|
|
def add(self, bundle):
|
|
|
|
name = bundle.get_service_name()
|
|
|
|
|
|
|
|
service_cp = _ServiceParser()
|
|
|
|
|
|
|
|
section = 'D-BUS Service'
|
|
|
|
service_cp.add_section(section)
|
2006-10-31 12:06:28 +01:00
|
|
|
|
|
|
|
# Compatibility with the old activity registry, remove after BTest-1
|
|
|
|
# service_cp.set(section, 'Name', name)
|
|
|
|
service_cp.set(section, 'Name', name + '.Factory')
|
|
|
|
|
|
|
|
# FIXME total hack
|
|
|
|
full_exec = env.get_shell_bin_dir() + '/' + bundle.get_exec()
|
|
|
|
full_exec += ' ' + bundle.get_path()
|
|
|
|
service_cp.set(section, 'Exec', full_exec)
|
2006-10-31 10:48:45 +01:00
|
|
|
|
|
|
|
dest = os.path.join(self._path, name + '.service')
|
|
|
|
fileobject = open(dest, 'w')
|
|
|
|
service_cp.write(fileobject)
|
|
|
|
fileobject.close()
|
|
|
|
|
2006-10-29 18:28:48 +01:00
|
|
|
class BundleRegistry:
|
|
|
|
"""Service that tracks the available activity bundles"""
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
self._bundles = {}
|
|
|
|
self._search_path = []
|
2006-10-31 10:48:45 +01:00
|
|
|
self._service_manager = _ServiceManager()
|
2006-10-29 18:28:48 +01:00
|
|
|
|
|
|
|
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-10-29 19:05:09 +01:00
|
|
|
def add_search_path(self, path):
|
|
|
|
"""Add a directory to the bundles search path"""
|
2006-10-29 18:28:48 +01:00
|
|
|
self._search_path.append(path)
|
|
|
|
self._scan_directory(path)
|
|
|
|
|
|
|
|
def __iter__(self):
|
2006-10-29 19:05:09 +01:00
|
|
|
return self._bundles.values().__iter__()
|
2006-10-29 18:28:48 +01:00
|
|
|
|
|
|
|
def _scan_directory(self, path):
|
2006-10-29 21:03:28 +01:00
|
|
|
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)
|
2006-10-29 19:05:09 +01:00
|
|
|
|
2006-10-31 12:06:28 +01:00
|
|
|
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)
|