[Telepathy-commits] [telepathy-doc/master] Make the example work better

Davyd Madeley davyd at madeley.id.au
Sun Mar 1 18:53:56 PST 2009


---
 docs/examples/python_simple_presence/example.py |  155 +++++++++++++----------
 1 files changed, 86 insertions(+), 69 deletions(-)

diff --git a/docs/examples/python_simple_presence/example.py b/docs/examples/python_simple_presence/example.py
index 2cc56c0..57cfc69 100755
--- a/docs/examples/python_simple_presence/example.py
+++ b/docs/examples/python_simple_presence/example.py
@@ -8,77 +8,16 @@ dbus.mainloop.glib.DBusGMainLoop(set_as_default = True)
 
 import telepathy
 import telepathy.client
-from telepathy.interfaces import CONN_MGR_INTERFACE, \
-                                 CONN_INTERFACE
+from telepathy.interfaces import CONNECTION_MANAGER, \
+                                 CONNECTION, \
+                                 CONNECTION_INTERFACE_SIMPLE_PRESENCE, \
+                                 CONNECTION_INTERFACE_CONTACTS
 from telepathy.constants import CONNECTION_STATUS_CONNECTED, \
                                 CONNECTION_STATUS_DISCONNECTED
 
 DBUS_PROPERTIES = 'org.freedesktop.DBus.Properties'
-CONN_INTERFACE_SIMPLE_PRESENCE = 'org.freedesktop.Telepathy.Connection.Interface.SimplePresence'
 
 class Example (object):
-    def generic_reply (self, *args): pass
-    
-    def error_cb (self, error):
-        print "Error:", error
-        self.disconnect()
-
-    def disconnect (self):
-        self.conn[CONN_INTERFACE].Disconnect(
-                                        reply_handler = self.generic_reply,
-                                        error_handler = self.error_cb)
-
-    def get_statuses_cb (self, value):
-        print "Statuses:"
-        
-        for (key, value) in value.iteritems():
-            print " - %s" % key
-
-    def get_interfaces_cb (self, interfaces):
-        print "Interfaces:"
-
-        for interface in interfaces:
-            print " - %s" % interface
-
-    def status_changed_cb (self, status, reason):
-        conn = self.conn
-    
-        if status == CONNECTION_STATUS_DISCONNECTED:
-            print "Disconnected!"
-            self.loop.quit()
-
-        if status != CONNECTION_STATUS_CONNECTED: return
-
-        print 'Carrier Detected' # remember dialup modems?
-    
-        # request the statuses
-        conn[DBUS_PROPERTIES].Get(CONN_INTERFACE_SIMPLE_PRESENCE, 'Statuses',
-                                reply_handler = self.get_statuses_cb,
-                                error_handler = self.error_cb)
-
-        # get a list of interfaces on this connection
-        conn[CONN_INTERFACE].GetInterfaces(
-                                reply_handler = self.get_interfaces_cb,
-                                error_handler = self.error_cb)
-
-        # set our presence
-        # FIXME: doesn't like this interface
-        conn[CONN_INTERFACE_SIMPLE_PRESENCE].SetPresence ('away',
-                                                          'At the Movies',
-                                            reply_handler = self.generic_reply,
-                                            error_handler = self.error_cb)
-    
-    def request_connection_cb (self, bus_name, object_path):
-        print bus_name, object_path
-        self.conn = conn = telepathy.client.Connection(bus_name, object_path)
-        
-        conn[CONN_INTERFACE].connect_to_signal('StatusChanged',
-            self.status_changed_cb)
-    
-        print "Establishing connection..."
-        conn[CONN_INTERFACE].Connect(reply_handler = self.generic_reply,
-                                     error_handler = self.error_cb)
-    
     def __init__ (self, account, password):
         """e.g. account  = 'bob at example.com/test'
                 password = 'bigbob'
@@ -86,19 +25,19 @@ class Example (object):
 
         reg = telepathy.client.ManagerRegistry()
         reg.LoadManagers()
-        
+
         # get the gabble Connection Manager
         self.cm = cm = reg.GetManager('gabble')
-        
+
         # get the parameters required to make a Jabber connection
-        cm[CONN_MGR_INTERFACE].RequestConnection('jabber',
+        cm[CONNECTION_MANAGER].RequestConnection('jabber',
             {
                 'account':  account,
                 'password': password,
             },
             reply_handler = self.request_connection_cb,
             error_handler = self.error_cb)
-        
+
         self.loop = gobject.MainLoop()
         try:
             self.loop.run()
@@ -108,6 +47,84 @@ class Example (object):
             # reengage the mainloop so that we can disconnect cleanly
             self.loop.run()
 
+    def generic_reply (self, *args): pass
+
+    def error_cb (self, error):
+        print "Error:", error
+        self.disconnect()
+
+    def disconnect (self):
+        self.conn[CONNECTION].Disconnect(reply_handler = self.generic_reply,
+                                         error_handler = self.error_cb)
+
+    def request_connection_cb (self, bus_name, object_path):
+        print bus_name, object_path
+        self.conn = conn = telepathy.client.Connection(bus_name, object_path)
+
+        conn[CONNECTION].connect_to_signal('StatusChanged',
+            self.status_changed_cb)
+
+        print "Establishing connection..."
+        conn[CONNECTION].Connect(reply_handler = self.generic_reply,
+                                 error_handler = self.error_cb)
+
+    def status_changed_cb (self, status, reason):
+        conn = self.conn
+
+        if status == CONNECTION_STATUS_DISCONNECTED:
+            print "Disconnected!"
+            self.loop.quit()
+
+        if status != CONNECTION_STATUS_CONNECTED: return
+
+        print 'Carrier Detected' # remember dialup modems?
+
+        # get a list of interfaces on this connection
+        conn[CONNECTION].GetInterfaces(reply_handler = self.get_interfaces_cb,
+                                       error_handler = self.error_cb)
+
+    def get_interfaces_cb (self, interfaces):
+        conn = self.conn
+
+        print "Interfaces:"
+        for interface in interfaces:
+            print " - %s" % interface
+
+        if CONNECTION_INTERFACE_SIMPLE_PRESENCE in interfaces:
+            # request the statuses
+            print 'Requesting statuses...'
+            conn[DBUS_PROPERTIES].Get(CONNECTION_INTERFACE_SIMPLE_PRESENCE,
+                                    'Statuses',
+                                    reply_handler = self.get_statuses_cb,
+                                    error_handler = self.error_cb)
+
+            # set our presence
+            # FIXME: doesn't like this interface
+            print 'Setting presence...'
+            conn[CONNECTION_INTERFACE_SIMPLE_PRESENCE].SetPresence(
+                                    'away',
+                                    'At the Movies',
+                                    reply_handler = self.generic_reply,
+                                    error_handler = self.error_cb)
+
+        if CONNECTION_INTERFACE_CONTACTS in interfaces:
+            print 'Requesting contact attribute interfaces...'
+            conn[DBUS_PROPERTIES].Get(CONNECTION_INTERFACE_CONTACTS,
+                                    'ContactAttributeInterfaces',
+                                    reply_handler = self.get_contact_ifaces_cb,
+                                    error_handler = self.error_cb)
+
+    def get_statuses_cb (self, value):
+        print "Statuses:"
+
+        for (key, value) in value.iteritems():
+            print " - %s" % key
+
+    def get_contact_ifaces_cb (self, interfaces):
+        print "Contact Attribute Interfaces:"
+        for interface in interfaces:
+            print " - %s" % interface
+
 if __name__ == '__main__':
     import getpass
     password = getpass.getpass()
-- 
1.5.6.5




More information about the telepathy-commits mailing list