Split the factory to his own module

This commit is contained in:
Marco Pesenti Gritti 2006-08-09 02:02:34 +02:00
parent 7ab6da7278
commit 70485218c0
6 changed files with 90 additions and 81 deletions

View File

@ -3,7 +3,7 @@ from gettext import gettext as _
import gtk import gtk
import wnck import wnck
from sugar.activity import Activity from sugar.activity import ActivityFactory
from ActivitiesModel import ActivitiesModel from ActivitiesModel import ActivitiesModel
from sugar.presence.PresenceService import PresenceService from sugar.presence.PresenceService import PresenceService
@ -79,7 +79,7 @@ class ActivitiesGrid(gtk.VBox):
activity_ps = pservice.get_activity(activity_id) activity_ps = pservice.get_activity(activity_id)
if activity_ps: if activity_ps:
Activity.create(activity.get_id(), activity_ps) ActivityFactory.create(activity.get_id(), activity_ps)
else: else:
print 'Cannot start activity.' print 'Cannot start activity.'
@ -168,7 +168,7 @@ class HomeWindow(gtk.Window):
return self._shell.get_registry().list_activities() return self._shell.get_registry().list_activities()
def create(self, activity_name): def create(self, activity_name):
Activity.create(activity_name) ActivityFactory.create(activity_name)
def activate(self, activity_window): def activate(self, activity_window):
activity_window.activate(gtk.get_current_event_time()) activity_window.activate(gtk.get_current_event_time())

View File

@ -2,6 +2,6 @@
import sys import sys
from sugar.activity import Activity from sugar.activity import ActivityFactory
Activity.create(sys.argv[1]) ActivityFactory.create(sys.argv[1])

View File

@ -6,8 +6,9 @@ import logging
import pygtk import pygtk
pygtk.require('2.0') pygtk.require('2.0')
import gobject import gobject
import gtk
from sugar.activity import Activity from sugar.activity import ActivityFactory
from sugar.LogWriter import LogWriter from sugar.LogWriter import LogWriter
from sugar import theme from sugar import theme
@ -19,6 +20,8 @@ theme.setup()
#lw.start() #lw.start()
if len(sys.argv) == 4: if len(sys.argv) == 4:
Activity.register_factory(sys.argv[1], sys.argv[2], sys.argv[3]) ActivityFactory.register_factory(sys.argv[1], sys.argv[2], sys.argv[3])
else: else:
Activity.register_factory(sys.argv[1], sys.argv[2]) ActivityFactory.register_factory(sys.argv[1], sys.argv[2])
gtk.main()

View File

@ -1,6 +1,3 @@
import sys
import logging
import dbus import dbus
import dbus.service import dbus.service
import dbus.glib import dbus.glib
@ -13,78 +10,11 @@ from sugar.presence.PresenceService import PresenceService
gtk.gdk.threads_init() gtk.gdk.threads_init()
dbus.glib.threads_init() dbus.glib.threads_init()
from sugar.LogWriter import LogWriter
import sugar.util import sugar.util
ACTIVITY_SERVICE_NAME = "com.redhat.Sugar.Activity" ACTIVITY_SERVICE_NAME = "com.redhat.Sugar.Activity"
ACTIVITY_SERVICE_PATH = "/com/redhat/Sugar/Activity" ACTIVITY_SERVICE_PATH = "/com/redhat/Sugar/Activity"
def get_path(activity_name):
"""Returns the activity path"""
return '/' + activity_name.replace('.', '/')
def get_factory(activity_name):
"""Returns the activity factory"""
return activity_name + '.Factory'
class ActivityFactory(dbus.service.Object):
"""Dbus service that takes care of creating new instances of an activity"""
def __init__(self, name, activity_class, default_type):
self._default_type = default_type
splitted_module = activity_class.rsplit('.', 1)
module_name = splitted_module[0]
class_name = splitted_module[1]
module = __import__(module_name)
for comp in module_name.split('.')[1:]:
module = getattr(module, comp)
self._class = getattr(module, class_name)
bus = dbus.SessionBus()
factory = get_factory(name)
bus_name = dbus.service.BusName(factory, bus = bus)
dbus.service.Object.__init__(self, bus_name, get_path(factory))
@dbus.service.method("com.redhat.Sugar.ActivityFactory",
in_signature="o", out_signature="")
def create_with_service(self, service_path):
pservice = PresenceService()
service = pservice.get(service_path)
activity = self._class()
activity.set_default_type(self._default_type)
activity.join(service)
@dbus.service.method("com.redhat.Sugar.ActivityFactory")
def create(self):
activity = self._class()
activity.set_default_type(self._default_type)
def create(activity_name, service = None):
"""Create a new activity from his name."""
bus = dbus.SessionBus()
factory_name = get_factory(activity_name)
factory_path = get_path(factory_name)
proxy_obj = bus.get_object(factory_name, factory_path)
factory = dbus.Interface(proxy_obj, "com.redhat.Sugar.ActivityFactory")
if service:
print service.object_path()
factory.create_with_service(service.object_path())
else:
factory.create()
def register_factory(name, activity_class, default_type=None):
"""Register the activity factory."""
factory = ActivityFactory(name, activity_class, default_type)
gtk.main()
class ActivityDbusService(dbus.service.Object): class ActivityDbusService(dbus.service.Object):
"""Base dbus service object that each Activity uses to export dbus methods. """Base dbus service object that each Activity uses to export dbus methods.
@ -187,6 +117,5 @@ class Activity(gtk.Window):
"""Share the activity on the network.""" """Share the activity on the network."""
properties = { 'title' : self.get_title() } properties = { 'title' : self.get_title() }
self._service = self._pservice.share_activity(self, self._service = self._pservice.share_activity(self,
self._default_type, self._default_type, properties)
properties)
self._shared = True self._shared = True

View File

@ -0,0 +1,76 @@
import sys
import logging
import dbus
import dbus.service
import gobject
from sugar.presence.PresenceService import PresenceService
ACTIVITY_SERVICE_NAME = "com.redhat.Sugar.Activity"
ACTIVITY_SERVICE_PATH = "/com/redhat/Sugar/Activity"
def get_path(activity_name):
"""Returns the activity path"""
return '/' + activity_name.replace('.', '/')
def _get_factory(activity_name):
"""Returns the activity factory"""
return activity_name + '.Factory'
class ActivityFactory(dbus.service.Object):
"""Dbus service that takes care of creating new instances of an activity"""
def __init__(self, name, activity_class, default_type):
self._default_type = default_type
splitted_module = activity_class.rsplit('.', 1)
module_name = splitted_module[0]
class_name = splitted_module[1]
module = __import__(module_name)
for comp in module_name.split('.')[1:]:
module = getattr(module, comp)
self._class = getattr(module, class_name)
bus = dbus.SessionBus()
factory = _get_factory(name)
bus_name = dbus.service.BusName(factory, bus = bus)
dbus.service.Object.__init__(self, bus_name, get_path(factory))
@dbus.service.method("com.redhat.Sugar.ActivityFactory",
in_signature="o", out_signature="")
def create_with_service(self, service_path):
pservice = PresenceService()
service = pservice.get(service_path)
activity = self._class()
activity.set_default_type(self._default_type)
activity.join(service)
@dbus.service.method("com.redhat.Sugar.ActivityFactory")
def create(self):
activity = self._class()
activity.set_default_type(self._default_type)
def create(activity_name, service = None):
"""Create a new activity from his name."""
bus = dbus.SessionBus()
factory_name = _get_factory(activity_name)
factory_path = get_path(factory_name)
proxy_obj = bus.get_object(factory_name, factory_path)
factory = dbus.Interface(proxy_obj, "com.redhat.Sugar.ActivityFactory")
if service:
print service.object_path()
factory.create_with_service(service.object_path())
else:
factory.create()
def register_factory(name, activity_class, default_type=None):
"""Register the activity factory."""
factory = ActivityFactory(name, activity_class, default_type)

View File

@ -1,4 +1,5 @@
sugardir = $(pythondir)/sugar/activity sugardir = $(pythondir)/sugar/activity
sugar_PYTHON = \ sugar_PYTHON = \
__init__.py \ __init__.py \
Activity.py Activity.py \
ActivityFactory.py