From 1ef5c576c2d66ee7674ceccc6efea5c52777b443 Mon Sep 17 00:00:00 2001 From: Marco Pesenti Gritti Date: Tue, 25 Jul 2006 22:52:45 +0200 Subject: [PATCH 1/2] Do not use dbus_bindings for exceptions. Use dbus.exceptions. Thanks J5 to point it out. --- shell/PresenceService/PresenceService.py | 4 +- sugar/p2p/presence.py | 106 ----------------------- sugar/presence/Activity.py | 4 +- sugar/presence/Buddy.py | 6 +- sugar/presence/PresenceService.py | 10 +-- sugar/presence/Service.py | 2 +- 6 files changed, 13 insertions(+), 119 deletions(-) delete mode 100644 sugar/p2p/presence.py diff --git a/shell/PresenceService/PresenceService.py b/shell/PresenceService/PresenceService.py index 423997e3..a3af8246 100644 --- a/shell/PresenceService/PresenceService.py +++ b/shell/PresenceService/PresenceService.py @@ -1,4 +1,4 @@ -import avahi, dbus, dbus.glib, dbus.dbus_bindings, gobject +import avahi, dbus, dbus.glib, gobject import Buddy import Service import random @@ -603,7 +603,7 @@ class PresenceService(object): domain, "", # let Avahi figure the 'host' out dbus.UInt16(port), info,) group.Commit() - except dbus.dbus_bindings.DBusException, exc: + except dbus.exceptions.DBusException, exc: # FIXME: ignore local name collisions, since that means # the zeroconf service is already registered. Ideally we # should un-register it an re-register with the correct info diff --git a/sugar/p2p/presence.py b/sugar/p2p/presence.py deleted file mode 100644 index 9cb297e9..00000000 --- a/sugar/p2p/presence.py +++ /dev/null @@ -1,106 +0,0 @@ -# -*- tab-width: 4; indent-tabs-mode: t -*- - -import avahi, dbus, dbus.glib, dbus.dbus_bindings - -ACTION_SERVICE_NEW = 'new' -ACTION_SERVICE_REMOVED = 'removed' - -class PresenceDiscovery(object): - def __init__(self): - self.bus = dbus.SystemBus() - self.server = dbus.Interface(self.bus.get_object(avahi.DBUS_NAME, avahi.DBUS_PATH_SERVER), avahi.DBUS_INTERFACE_SERVER) - self._service_browsers = {} - self._service_type_browsers = {} - self._service_listeners = [] - - def add_service_listener(self, listener): - self._service_listeners.append(listener) - - def start(self): - # Always browse .local - self.browse_domain(avahi.IF_UNSPEC, avahi.PROTO_UNSPEC, "local") - db = dbus.Interface(self.bus.get_object(avahi.DBUS_NAME, self.server.DomainBrowserNew(avahi.IF_UNSPEC, avahi.PROTO_UNSPEC, "", avahi.DOMAIN_BROWSER_BROWSE, dbus.UInt32(0))), avahi.DBUS_INTERFACE_DOMAIN_BROWSER) - db.connect_to_signal('ItemNew', self.new_domain) - - def _error_handler(self, err): - print "Error resolving: %s" % err - - def resolve_service(self, interface, protocol, name, stype, domain, reply_handler, error_handler=None): - if not error_handler: - error_handler = self._error_handler - self.server.ResolveService(int(interface), int(protocol), name, stype, domain, avahi.PROTO_UNSPEC, dbus.UInt32(0), reply_handler=reply_handler, error_handler=error_handler) - - def new_service(self, interface, protocol, name, stype, domain, flags): -# print "Found service '%s' (%d) of type '%s' in domain '%s' on %i.%i." % (name, flags, stype, domain, interface, protocol) - for listener in self._service_listeners: - listener(ACTION_SERVICE_NEW, interface, protocol, name, stype, domain, flags) - - def remove_service(self, interface, protocol, name, stype, domain, flags): -# print "Service '%s' of type '%s' in domain '%s' on %i.%i disappeared." % (name, stype, domain, interface, protocol) - for listener in self._service_listeners: - listener(ACTION_SERVICE_REMOVED, interface, protocol, name, stype, domain, flags) - - def new_service_type(self, interface, protocol, stype, domain, flags): - # Are we already browsing this domain for this type? - if self._service_browsers.has_key((interface, protocol, stype, domain)): - return - -# print "Browsing for services of type '%s' in domain '%s' on %i.%i ..." % (stype, domain, interface, protocol) - - b = dbus.Interface(self.bus.get_object(avahi.DBUS_NAME, self.server.ServiceBrowserNew(interface, protocol, stype, domain, dbus.UInt32(0))), avahi.DBUS_INTERFACE_SERVICE_BROWSER) - b.connect_to_signal('ItemNew', self.new_service) - b.connect_to_signal('ItemRemove', self.remove_service) - - self._service_browsers[(interface, protocol, stype, domain)] = b - - def browse_domain(self, interface, protocol, domain): - # Are we already browsing this domain? - if self._service_type_browsers.has_key((interface, protocol, domain)): - return - -# print "Browsing domain '%s' on %i.%i ..." % (domain, interface, protocol) - - try: - b = dbus.Interface(self.bus.get_object(avahi.DBUS_NAME, self.server.ServiceTypeBrowserNew(interface, protocol, domain, dbus.UInt32(0))), avahi.DBUS_INTERFACE_SERVICE_TYPE_BROWSER) - except dbus.DBusException, exc: - str_exc = str(exc) - if str_exc.find("The name org.freedesktop.Avahi was not provided by any .service files") >= 0: - raise Exception("Avahi does not appear to be running. '%s'" % str_exc) - else: - raise exc - b.connect_to_signal('ItemNew', self.new_service_type) - - self._service_type_browsers[(interface, protocol, domain)] = b - - def new_domain(self, interface, protocol, domain, flags): - if domain != "local": - return - self.browse_domain(interface, protocol, domain) - - -class PresenceAnnounce(object): - def __init__(self): - self.bus = dbus.SystemBus() - self.server = dbus.Interface(self.bus.get_object(avahi.DBUS_NAME, avahi.DBUS_PATH_SERVER), avahi.DBUS_INTERFACE_SERVER) - self._hostname = None - - def register_service(self, rs_name, rs_port, rs_service, **kwargs): - g = dbus.Interface(self.bus.get_object(avahi.DBUS_NAME, self.server.EntryGroupNew()), avahi.DBUS_INTERFACE_ENTRY_GROUP) - if rs_name is None: - if self._hostname is None: - self._hostname = "%s:%s" % (self.server.GetHostName(), rs_port) - rs_name = self._hostname - - info = ["%s=%s" % (k, v) for k, v in kwargs.items()] - try: - g.AddService(avahi.IF_UNSPEC, avahi.PROTO_UNSPEC, 0, rs_name, rs_service, - "", "", # domain, host (let the system figure it out) - dbus.UInt16(rs_port), info,) - g.Commit() - except dbus.dbus_bindings.DBusException, exc: - # FIXME: ignore local name collisions, since that means - # the zeroconf service is already registered. Ideally we - # should un-register it an re-register with the correct info - if str(exc) == "Local name collision": - pass - return g diff --git a/sugar/presence/Activity.py b/sugar/presence/Activity.py index a7c2f7a1..43e67556 100644 --- a/sugar/presence/Activity.py +++ b/sugar/presence/Activity.py @@ -1,5 +1,5 @@ import gobject -import dbus, dbus_bindings +import dbus class Activity(gobject.GObject): @@ -69,7 +69,7 @@ class Activity(gobject.GObject): def get_service_of_type(self, stype): try: object_path = self._buddy.getServiceOfType(stype) - except dbus_bindings.DBusException: + except dbus.exceptions.DBusException: return None return self._ps_new_object(object_path) diff --git a/sugar/presence/Buddy.py b/sugar/presence/Buddy.py index d32e9987..17327908 100644 --- a/sugar/presence/Buddy.py +++ b/sugar/presence/Buddy.py @@ -1,6 +1,6 @@ import gobject import gtk -import dbus, dbus_bindings +import dbus class Buddy(gobject.GObject): @@ -110,14 +110,14 @@ class Buddy(gobject.GObject): def get_service_of_type(self, stype): try: object_path = self._buddy.getServiceOfType(stype) - except dbus_bindings.DBusException: + except dbus.exceptions.DBusException: return None return self._ps_new_object(object_path) def get_joined_activities(self): try: resp = self._buddy.getJoinedActivities() - except dbus_bindings.DBusException: + except dbus.exceptions.DBusException: return [] acts = [] for item in resp: diff --git a/sugar/presence/PresenceService.py b/sugar/presence/PresenceService.py index 8cbbb28f..b64934d5 100644 --- a/sugar/presence/PresenceService.py +++ b/sugar/presence/PresenceService.py @@ -1,4 +1,4 @@ -import dbus, dbus.glib, dbus.dbus_bindings, gobject +import dbus, dbus.glib, gobject import Buddy, Service, Activity @@ -149,7 +149,7 @@ class PresenceService(gobject.GObject): def get_activity(self, activity_id): try: act_op = self._ps.getActivity(activity_id) - except dbus.dbus_bindings.DBusException: + except dbus.exceptions.DBusException: return None return self._new_object(act_op) @@ -163,21 +163,21 @@ class PresenceService(gobject.GObject): def get_buddy_by_name(self, name): try: buddy_op = self._ps.getBuddyByName(name) - except dbus.dbus_bindings.DBusException: + except dbus.exceptions.DBusException: return None return self._new_object(buddy_op) def get_buddy_by_address(self, addr): try: buddy_op = self._ps.getBuddyByAddress(addr) - except dbus.dbus_bindings.DBusException: + except dbus.exceptions.DBusException: return None return self._new_object(buddy_op) def get_owner(self): try: owner_op = self._ps.getOwner() - except dbus.dbus_bindings.DBusException: + except dbus.exceptions.DBusException: return None return self._new_object(owner_op) diff --git a/sugar/presence/Service.py b/sugar/presence/Service.py index 1b09161f..0a01da41 100644 --- a/sugar/presence/Service.py +++ b/sugar/presence/Service.py @@ -1,5 +1,5 @@ import gobject -import dbus, dbus_bindings +import dbus class Service(gobject.GObject): From 9e32bbba7addaff7985329af0a4fdbecc65e1223 Mon Sep 17 00:00:00 2001 From: Marco Pesenti Gritti Date: Tue, 25 Jul 2006 23:00:45 +0200 Subject: [PATCH 2/2] Import some missing dbus.glib imports --- shell/Shell.py | 1 + shell/sugar-console | 1 + shell/sugar-people | 1 + 3 files changed, 3 insertions(+) diff --git a/shell/Shell.py b/shell/Shell.py index cd8f4f49..a7ecea3a 100755 --- a/shell/Shell.py +++ b/shell/Shell.py @@ -1,6 +1,7 @@ import os import dbus +import dbus.glib import pygtk pygtk.require('2.0') import gtk diff --git a/shell/sugar-console b/shell/sugar-console index ed9c1621..538715d6 100755 --- a/shell/sugar-console +++ b/shell/sugar-console @@ -1,6 +1,7 @@ #!/usr/bin/python import dbus +import dbus.glib bus = dbus.SessionBus() proxy_obj = bus.get_object('com.redhat.Sugar.Shell', '/com/redhat/Sugar/Shell') diff --git a/shell/sugar-people b/shell/sugar-people index 60d82db4..f3f5a48c 100755 --- a/shell/sugar-people +++ b/shell/sugar-people @@ -1,6 +1,7 @@ #!/usr/bin/python import dbus +import dbus.glib bus = dbus.SessionBus() proxy_obj = bus.get_object('com.redhat.Sugar.Shell', '/com/redhat/Sugar/Shell')