Commits

Jason R. Coombs committed 2702662

Extracted dbus object initialization.

Comments (0)

Files changed (1)

keyring/backends/SecretService.py

             else:
                 return unicode(s, errors='replace')
 
+    @property
+    def secret_service(self):
+        import dbus
+        bus = dbus.SessionBus()
+        service_obj = bus.get_object('org.freedesktop.secrets',
+            '/org/freedesktop/secrets')
+        return dbus.Interface(service_obj, 'org.freedesktop.Secret.Service')
+
     def get_password(self, service, username):
         """Get password of the username for the service
         """
-        import dbus
         service = self._str_to_dbus_str(service)
         username = self._str_to_dbus_str(username)
-        bus = dbus.SessionBus()
-        service_obj = bus.get_object('org.freedesktop.secrets',
-            '/org/freedesktop/secrets')
-        service_iface = dbus.Interface(service_obj,
-            'org.freedesktop.Secret.Service')
-        unlocked, locked = service_iface.SearchItems(
+        secret_service = self.secret_service
+        unlocked, locked = secret_service.SearchItems(
             {"username": username, "service": service})
-        _, session = service_iface.OpenSession("plain", "")
-        no_longer_locked, prompt = service_iface.Unlock(locked)
+        _, session = secret_service.OpenSession("plain", "")
+        no_longer_locked, prompt = secret_service.Unlock(locked)
         assert prompt == "/"
-        secrets = service_iface.GetSecrets(unlocked + locked, session,
+        secrets = secret_service.GetSecrets(unlocked + locked, session,
             byte_arrays=True)
         for item_path, secret in secrets.iteritems():
             return unicode(secret[2])
         return None
 
+    @property
+    def collection(self):
+        import dbus
+        bus = dbus.SessionBus()
+        collection_obj = bus.get_object(
+            'org.freedesktop.secrets',
+            '/org/freedesktop/secrets/aliases/default')
+        return dbus.Interface(collection_obj,
+            'org.freedesktop.Secret.Collection')
+
     def set_password(self, service, username, password):
         """Set password for the username of the service
         """
         service = self._str_to_dbus_str(service)
         username = self._str_to_dbus_str(username)
         password = self._str_to_dbus_str(password)
-        bus = dbus.SessionBus()
-        service_obj = bus.get_object('org.freedesktop.secrets',
-            '/org/freedesktop/secrets')
-        service_iface = dbus.Interface(service_obj,
-            'org.freedesktop.Secret.Service')
-        collection_obj = bus.get_object(
-            'org.freedesktop.secrets',
-            '/org/freedesktop/secrets/aliases/default')
-        collection = dbus.Interface(collection_obj,
-            'org.freedesktop.Secret.Collection')
         attributes = {
             "service": service,
             "username": username
             }
-        _, session = service_iface.OpenSession("plain", "")
+        _, session = self.secret_service.OpenSession("plain", "")
 
         secret = dbus.Struct(
             (session, "", dbus.ByteArray(password), "application/octet-stream"))
             "org.freedesktop.Secret.Item.Label": "%s @ %s" % (
                 username, service),
             "org.freedesktop.Secret.Item.Attributes": attributes}
-        (item, prompt) = collection.CreateItem(properties, secret,
+        (item, prompt) = self.collection.CreateItem(properties, secret,
             True)
         assert prompt == "/"