From 02678b89554d5d758762b53479edf51e0e2ff56e Mon Sep 17 00:00:00 2001 From: Morten Mjelva Date: Tue, 20 Apr 2010 23:29:55 +0200 Subject: [PATCH] Refactored interfaces to check properties first, and request them only if that fails --- src/client/account.py | 7 +++++-- src/client/accountmgr.py | 7 +++++-- src/client/channel.py | 44 ++++++++++++++++++++++++++++---------------- src/client/conn.py | 42 +++++++++++++++++++++++++++--------------- src/client/connmgr.py | 3 +++ 5 files changed, 68 insertions(+), 35 deletions(-) diff --git a/src/client/account.py b/src/client/account.py index 66bf260..d1822a1 100644 --- a/src/client/account.py +++ b/src/client/account.py @@ -32,7 +32,7 @@ class Account(InterfaceFactory): self.service_name = ACCOUNT_MANAGER self.object_path = object_path self._ready_handler = ready_handler - self._error_cb = error_handler + self._error_handler = error_handler object = bus.get_object(self.service_name, self.object_path) InterfaceFactory.__init__(self, object, ACCOUNT) @@ -40,7 +40,10 @@ class Account(InterfaceFactory): ACCOUNT, 'Interfaces', reply_handler=self._get_interfaces_cb, - error_handler=self._error_cb) + error_handler=self._error_handler) + + def __repr__(self): + return self.object_path def _get_interfaces_cb(self, interfaces): self.get_valid_interfaces().update(interfaces) diff --git a/src/client/accountmgr.py b/src/client/accountmgr.py index 472099e..e9e22f5 100644 --- a/src/client/accountmgr.py +++ b/src/client/accountmgr.py @@ -32,7 +32,7 @@ class AccountManager(InterfaceFactory): self.service_name = ACCOUNT_MANAGER self.object_path = '/org/freedesktop/Telepathy/AccountManager' self._ready_handler = ready_handler - self._error_cb = error_handler + self._error_handler = error_handler object = bus.get_object(self.service_name, self.object_path) InterfaceFactory.__init__(self, object, ACCOUNT_MANAGER) @@ -40,7 +40,10 @@ class AccountManager(InterfaceFactory): ACCOUNT_MANAGER, 'Interfaces', reply_handler=self._get_interfaces_cb, - error_handler=self._error_cb) + error_handler=self._error_handler) + + def __repr__(self): + return self.object_path def _get_interfaces_cb(self, interfaces): self.get_valid_interfaces().update(interfaces) diff --git a/src/client/channel.py b/src/client/channel.py index 84edc1f..532d488 100644 --- a/src/client/channel.py +++ b/src/client/channel.py @@ -32,27 +32,39 @@ class Channel(InterfaceFactory): self.service_name = service_name self.object_path = object_path self._ready_handler = ready_handler - self.error_cb = error_handler + self._error_handler = error_handler + object = bus.get_object(service_name, object_path) InterfaceFactory.__init__(self, object, CHANNEL_INTERFACE) - if ready_handler: - self[CHANNEL_INTERFACE].GetChannelType( - reply_handler=self.get_channel_type_reply_cb, - error_handler=self.error_cb) - else: - type = self.GetChannelType() - interfaces = self.GetInterfaces() - self.get_valid_interfaces().add(type) - self.get_valid_interfaces().update(interfaces) - - def get_channel_type_reply_cb(self, interface): + self[dbus.PROPERTIES_IFACE].Get( + CHANNEL_INTERFACE, + 'ChannelType', + reply_handler=self._get_channel_type_cb, + error_handler=self._error_handler) + + def __repr__(self): + return self.object_path + + def _get_channel_type_cb(self, interface): self.get_valid_interfaces().add(interface) + self[CHANNEL_INTERFACE].GetInterfaces( - reply_handler=self.get_interfaces_reply_cb, - error_handler=self.error_cb) + reply_handler=self._get_interfaces_cb, + error_handler=self._error_handler) - def get_interfaces_reply_cb(self, interfaces): + def _get_channel_type_error(self, error): + self[CHANNEL_INTERFACE].GetChannelType( + reply_handler=self._get_channel_type_cb, + error_handler=self._error_handler) + + def _get_interfaces_cb(self, interfaces): self.get_valid_interfaces().update(interfaces) - if self._ready_handler is not None: + + if self._ready_handler: self._ready_handler(self) + + def _get_interfaces_error(self, error): + self[CHANNEL_INTERFACE].GetInterfaces( + reply_handler=self._get_interfaces_cb, + error_handler=self._error_handler) diff --git a/src/client/conn.py b/src/client/conn.py index e46c3ea..1468d53 100644 --- a/src/client/conn.py +++ b/src/client/conn.py @@ -40,38 +40,45 @@ class Connection(InterfaceFactory): self.service_name = service_name self.object_path = object_path - self._ready_handlers = [] - if ready_handler is not None: - self._ready_handlers.append(ready_handler) + self._ready_handler = ready_handler self._error_handler = error_handler self._ready = False object = self.bus.get_object(service_name, object_path) InterfaceFactory.__init__(self, object, CONN_INTERFACE) - # note: old dbus-python returns None from connect_to_signal self._status_changed_connection = \ self[CONN_INTERFACE].connect_to_signal('StatusChanged', lambda status, reason: self._status_cb(status)) - self[CONN_INTERFACE].GetStatus( - reply_handler=self._status_cb, - error_handler=error_handler) - def _status_cb(self, status): + self[dbus.PROPERTIES_IFACE].Get( + CONN_INTERFACE, + 'Status', + reply_handler=self._get_status_cb, + error_handler=self._get_status_error) + + def __repr__(self): + return self.object_path + + def _get_status_cb(self, status): if status == CONNECTION_STATUS_CONNECTED: - self._get_interfaces() + self[dbus.PROPERTIES_IFACE].Get( + CONN_INTERFACE, + 'Interfaces', + reply_handler=self._get_interfaces_cb, + error_handler=self._get_interfaces_error) if self._status_changed_connection: # disconnect signal handler self._status_changed_connection.remove() self._status_changed_connection = None - def _get_interfaces(self): - self[CONN_INTERFACE].GetInterfaces( - reply_handler=self._get_interfaces_reply_cb, + def _get_status_error(self, error): + self[CONN_INTERFACE].GetStatus( + reply_handler=self._get_status_cb, error_handler=self._error_handler) - def _get_interfaces_reply_cb(self, interfaces): + def _get_interfaces_cb(self, interfaces): if self._ready: return @@ -79,8 +86,13 @@ class Connection(InterfaceFactory): self.get_valid_interfaces().update(interfaces) - for ready_handler in self._ready_handlers: - ready_handler(self) + if self._ready_handler: + self._ready_handler(self) + + def _get_interfaces_error(self, error): + self[CONN_INTERFACE].GetInterfaces( + reply_handler=self._get_interfaces_cb, + error_handler=self._error_handler) @staticmethod def get_connections(bus=None): diff --git a/src/client/connmgr.py b/src/client/connmgr.py index 4b9fc56..2fa1fbd 100644 --- a/src/client/connmgr.py +++ b/src/client/connmgr.py @@ -33,6 +33,9 @@ class ConnectionManager(InterfaceFactory): object = bus.get_object(service_name, object_path) InterfaceFactory.__init__(self, object, CONN_MGR_INTERFACE) + def __repr__(self): + return self.object_path + def request_connection(self, proto, parameters): name, path = self.RequestConnection(proto, parameters) return Connection(name, path) -- 1.7.0.4