From 760b18f934dff28418a1cb516cf56299700e94dd 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 | 47 ++++++++++++++++++++++++++++++--------------- src/client/conn.py | 46 ++++++++++++++++++++++++++++++-------------- src/client/connmgr.py | 3 ++ 5 files changed, 75 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..75b15b0 100644 --- a/src/client/channel.py +++ b/src/client/channel.py @@ -28,31 +28,46 @@ class Channel(InterfaceFactory): error_handler=default_error_handler): if not bus: bus = dbus.Bus() - + 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) + + self[dbus.PROPERTIES_IFACE].GetAll( + reply_handler=self._get_properties_cb, + error_handler=self._error_handler) - 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 __repr__(self): + return self.object_path - def get_channel_type_reply_cb(self, interface): + 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_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_properties_cb(self, properties): + if properties['ChannelType']: + _get_channel_type_cb(properties['ChannelType']) + else: + self[CHANNEL_INTERFACE].GetChannelType( + reply_handler=self._get_channel_type_cb, + error_handler=self._error_handler) + + if properties['Interfaces']: + _get_interfaces_cb(properties['Interfaces']) + else: + 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..87357f7 100644 --- a/src/client/conn.py +++ b/src/client/conn.py @@ -40,8 +40,7 @@ class Connection(InterfaceFactory): self.service_name = service_name self.object_path = object_path - self._ready_handlers = [] - if ready_handler is not None: + if ready_handler: self._ready_handlers.append(ready_handler) self._error_handler = error_handler self._ready = False @@ -49,29 +48,46 @@ class Connection(InterfaceFactory): 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) + lambda status, reason: self._get_status_cb(status)) - def _status_cb(self, status): + self[dbus.PROPERTIES_IFACE].GetAll( + reply_handler=self._get_properties_cb, + error_handler=self.error_handler) + + def __repr__(self): + return self.object_path + + def _get_properties_cb(self, properties): + if properties['Status']: + _get_status_cb(properties['Status']) + else: + self[CONN_INTERFACE].GetStatus( + reply_handler=self._get_status_cb, + error_handler=self._error_handler) + + if properties['Interfaces']: + _get_interfaces_cb(properties['Interfaces']) + else: + self[CONN_INTERFACE].GetInterfaces( + reply_handler=self._get_interfaces_cb, + error_handler=self._error_handler) + + 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, - error_handler=self._error_handler) - - def _get_interfaces_reply_cb(self, interfaces): + def _get_interfaces_cb(self, interfaces): if self._ready: return 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