From 70485218c07c56c8ce5c77ce435663b1c920b347 Mon Sep 17 00:00:00 2001 From: Marco Pesenti Gritti Date: Wed, 9 Aug 2006 02:02:34 +0200 Subject: [PATCH] Split the factory to his own module --- shell/HomeWindow.py | 6 +-- shell/sugar-activity | 4 +- shell/sugar-activity-factory | 9 ++-- sugar/activity/Activity.py | 73 +---------------------------- sugar/activity/ActivityFactory.py | 76 +++++++++++++++++++++++++++++++ sugar/activity/Makefile.am | 3 +- 6 files changed, 90 insertions(+), 81 deletions(-) create mode 100644 sugar/activity/ActivityFactory.py diff --git a/shell/HomeWindow.py b/shell/HomeWindow.py index a0c9f828..ef44f615 100644 --- a/shell/HomeWindow.py +++ b/shell/HomeWindow.py @@ -3,7 +3,7 @@ from gettext import gettext as _ import gtk import wnck -from sugar.activity import Activity +from sugar.activity import ActivityFactory from ActivitiesModel import ActivitiesModel from sugar.presence.PresenceService import PresenceService @@ -79,7 +79,7 @@ class ActivitiesGrid(gtk.VBox): activity_ps = pservice.get_activity(activity_id) if activity_ps: - Activity.create(activity.get_id(), activity_ps) + ActivityFactory.create(activity.get_id(), activity_ps) else: print 'Cannot start activity.' @@ -168,7 +168,7 @@ class HomeWindow(gtk.Window): return self._shell.get_registry().list_activities() def create(self, activity_name): - Activity.create(activity_name) + ActivityFactory.create(activity_name) def activate(self, activity_window): activity_window.activate(gtk.get_current_event_time()) diff --git a/shell/sugar-activity b/shell/sugar-activity index ca46f093..9bf9dc84 100755 --- a/shell/sugar-activity +++ b/shell/sugar-activity @@ -2,6 +2,6 @@ import sys -from sugar.activity import Activity +from sugar.activity import ActivityFactory -Activity.create(sys.argv[1]) +ActivityFactory.create(sys.argv[1]) diff --git a/shell/sugar-activity-factory b/shell/sugar-activity-factory index 99133c5d..b39d229f 100755 --- a/shell/sugar-activity-factory +++ b/shell/sugar-activity-factory @@ -6,8 +6,9 @@ import logging import pygtk pygtk.require('2.0') import gobject +import gtk -from sugar.activity import Activity +from sugar.activity import ActivityFactory from sugar.LogWriter import LogWriter from sugar import theme @@ -19,6 +20,8 @@ theme.setup() #lw.start() 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: - Activity.register_factory(sys.argv[1], sys.argv[2]) + ActivityFactory.register_factory(sys.argv[1], sys.argv[2]) + +gtk.main() diff --git a/sugar/activity/Activity.py b/sugar/activity/Activity.py index c1e9297a..e99da5a6 100644 --- a/sugar/activity/Activity.py +++ b/sugar/activity/Activity.py @@ -1,6 +1,3 @@ -import sys -import logging - import dbus import dbus.service import dbus.glib @@ -13,78 +10,11 @@ from sugar.presence.PresenceService import PresenceService gtk.gdk.threads_init() dbus.glib.threads_init() -from sugar.LogWriter import LogWriter import sugar.util 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) - gtk.main() - class ActivityDbusService(dbus.service.Object): """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.""" properties = { 'title' : self.get_title() } self._service = self._pservice.share_activity(self, - self._default_type, - properties) + self._default_type, properties) self._shared = True diff --git a/sugar/activity/ActivityFactory.py b/sugar/activity/ActivityFactory.py new file mode 100644 index 00000000..ca3dfb8b --- /dev/null +++ b/sugar/activity/ActivityFactory.py @@ -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) diff --git a/sugar/activity/Makefile.am b/sugar/activity/Makefile.am index dacd474d..1e90df11 100644 --- a/sugar/activity/Makefile.am +++ b/sugar/activity/Makefile.am @@ -1,4 +1,5 @@ sugardir = $(pythondir)/sugar/activity sugar_PYTHON = \ __init__.py \ - Activity.py + Activity.py \ + ActivityFactory.py