2006-07-12 13:20:41 +02:00
|
|
|
import logging
|
2006-07-12 17:21:22 +02:00
|
|
|
import os
|
|
|
|
from ConfigParser import ConfigParser
|
|
|
|
from ConfigParser import NoOptionError
|
|
|
|
|
|
|
|
from sugar import env
|
2006-07-08 11:56:13 +02:00
|
|
|
|
2006-07-12 13:20:41 +02:00
|
|
|
class ActivityModule:
|
|
|
|
"""Info about an activity module. Wraps a .activity file."""
|
|
|
|
|
|
|
|
def __init__(self, name, activity_id, activity_exec, directory):
|
2006-07-08 11:56:13 +02:00
|
|
|
self._name = name
|
2006-07-12 13:20:41 +02:00
|
|
|
self._id = activity_id
|
|
|
|
self._directory = directory
|
|
|
|
self._exec = activity_exec
|
2006-07-08 11:56:13 +02:00
|
|
|
|
|
|
|
def get_name(self):
|
2006-07-12 13:20:41 +02:00
|
|
|
"""Get the activity user visible name."""
|
2006-07-08 11:56:13 +02:00
|
|
|
return self._name
|
|
|
|
|
2006-07-12 13:20:41 +02:00
|
|
|
def get_id(self):
|
|
|
|
"""Get the activity identifier"""
|
|
|
|
return self._id
|
|
|
|
|
2006-07-12 17:21:22 +02:00
|
|
|
def get_exec(self):
|
2006-07-12 13:20:41 +02:00
|
|
|
"""Get the activity executable"""
|
2006-07-12 17:21:22 +02:00
|
|
|
return self._exec
|
2006-07-08 11:56:13 +02:00
|
|
|
|
2006-07-12 13:20:41 +02:00
|
|
|
def get_directory(self):
|
|
|
|
"""Get the path to activity directory."""
|
|
|
|
return self._directory
|
2006-07-14 16:40:45 +02:00
|
|
|
|
|
|
|
def get_default_type(self):
|
|
|
|
"""Get the the type of the default activity service."""
|
|
|
|
return self._default_type
|
|
|
|
|
|
|
|
def set_default_type(self, default_type):
|
|
|
|
"""Set the the type of the default activity service."""
|
|
|
|
self._default_type = default_type
|
2006-07-12 13:20:41 +02:00
|
|
|
|
|
|
|
class ActivityRegistry:
|
|
|
|
"""Service that tracks the available activities"""
|
2006-07-08 11:56:13 +02:00
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
self._activities = []
|
|
|
|
|
2006-07-15 12:31:06 +02:00
|
|
|
def get_activity(self, default_type):
|
|
|
|
"""Returns an activity given his default type"""
|
|
|
|
for activity in self._activities:
|
|
|
|
if activity.get_default_type() == default_type:
|
|
|
|
return activity
|
|
|
|
return None
|
|
|
|
|
2006-07-12 13:20:41 +02:00
|
|
|
def scan_directory(self, path):
|
|
|
|
"""Scan a directory for activities and add them to the registry."""
|
2006-07-12 17:21:22 +02:00
|
|
|
if os.path.isdir(path):
|
|
|
|
for filename in os.listdir(path):
|
|
|
|
activity_dir = os.path.join(path, filename)
|
2006-07-12 13:20:41 +02:00
|
|
|
if os.path.isdir(activity_dir):
|
|
|
|
for filename in os.listdir(activity_dir):
|
|
|
|
if filename.endswith(".activity"):
|
|
|
|
self.add(os.path.join(activity_dir, filename))
|
|
|
|
|
|
|
|
def add(self, path):
|
|
|
|
"""Add an activity to the registry. The path points to a .activity file."""
|
|
|
|
cp = ConfigParser()
|
|
|
|
cp.read([path])
|
|
|
|
|
|
|
|
directory = os.path.dirname(path)
|
|
|
|
|
|
|
|
try:
|
|
|
|
activity_id = cp.get('Activity', 'id')
|
2006-07-12 17:21:22 +02:00
|
|
|
except NoOptionError:
|
|
|
|
logging.error('%s miss the required id option' % (path))
|
|
|
|
return False
|
|
|
|
|
|
|
|
try:
|
2006-07-12 13:20:41 +02:00
|
|
|
name = cp.get('Activity', 'name')
|
|
|
|
except NoOptionError:
|
2006-07-12 17:21:22 +02:00
|
|
|
logging.error('%s miss the required name option' % (path))
|
|
|
|
return False
|
2006-07-12 13:20:41 +02:00
|
|
|
|
2006-07-14 16:40:45 +02:00
|
|
|
if cp.has_option('Activity', 'default_type'):
|
|
|
|
default_type = cp.get('Activity', 'default_type')
|
|
|
|
else:
|
|
|
|
default_type = None
|
|
|
|
|
2006-07-12 13:20:41 +02:00
|
|
|
if cp.has_option('Activity', 'exec'):
|
|
|
|
activity_exec = cp.get('Activity', 'exec')
|
|
|
|
elif cp.has_option('Activity', 'python_module'):
|
2006-07-14 16:40:45 +02:00
|
|
|
python_module = cp.get('Activity', 'python_module')
|
2006-07-12 13:20:41 +02:00
|
|
|
python_module = cp.get('Activity', 'python_module')
|
2006-07-12 22:17:57 +02:00
|
|
|
activity_exec = '%s %s %s' % (env.get_activity_runner(),
|
|
|
|
activity_id, python_module)
|
2006-07-14 16:40:45 +02:00
|
|
|
if default_type:
|
|
|
|
activity_exec += ' ' + default_type
|
2006-07-12 17:21:22 +02:00
|
|
|
env.add_to_python_path(directory)
|
2006-07-12 13:20:41 +02:00
|
|
|
else:
|
|
|
|
logging.error('%s must specifiy exec or python_module' % (path))
|
2006-07-12 17:21:22 +02:00
|
|
|
return False
|
2006-07-12 13:20:41 +02:00
|
|
|
|
|
|
|
module = ActivityModule(name, activity_id, activity_exec, directory)
|
|
|
|
self._activities.append(module)
|
2006-07-08 11:56:13 +02:00
|
|
|
|
2006-07-14 16:40:45 +02:00
|
|
|
module.set_default_type(default_type)
|
|
|
|
|
2006-07-12 13:20:41 +02:00
|
|
|
return True
|
2006-07-08 11:56:13 +02:00
|
|
|
|
|
|
|
def list_activities(self):
|
2006-07-12 13:20:41 +02:00
|
|
|
"""Enumerate the registered activities as an ActivityModule list."""
|
2006-07-08 11:56:13 +02:00
|
|
|
return self._activities
|