Commits

Maciej Sitarz committed 217ecc0

Allow passing application name to backend.

Ex. KWallet shows application's name on prompt for opening wallet.

Comments (0)

Files changed (3)

keyring/backend.py

         return -1
 
     @abc.abstractmethod
-    def get_password(self, service, username):
-        """Get password of the username for the service
+    def get_password(self, service, username, app_name=None):
+        """Get password of the username for the service, introducing as app_name
         """
         return None
 
     @abc.abstractmethod
-    def set_password(self, service, username, password):
-        """Set password for the username of the service
+    def set_password(self, service, username, password, app_name=None):
+        """Set password for the username of the service, introducing as app_name
         """
         raise errors.PasswordSetError("reason")
 
     # for backward-compatibility, don't require a backend to implement
     #  delete_password
     #@abc.abstractmethod
-    def delete_password(self, service, username):
-        """Delete the password for the username of the service.
+    def delete_password(self, service, username, app_name=None):
+        """Delete the password for the username of the service, introducing as app_name
         """
         raise errors.PasswordDeleteError("reason")
 

keyring/backends/kwallet.py

 kwallet = None
 
 
-def open_kwallet(kwallet_module=None, qt_module=None, folder_name='Python'):
+def open_kwallet(kwallet_module=None, qt_module=None, app_name=None, folder_name='Python'):
 
     # If we specified the kwallet_module and/or qt_module, surely we won't need
     # the cached kwallet object...
     app = None
     if qt_module.qApp.instance() is None:
         app = qt_module.QApplication([])
+    if not app_name is None:
+        if not app is None:
+            app.setApplicationName(app_name)
+        else:
+            qt_module.qApp.instance().setApplicationName(app_name)
     try:
         window = qt_module.QWidget()
         kwallet = kwallet_module.openWallet(
         else:
             return -1
 
-    def get_password(self, service, username):
+    def get_password(self, service, username, app_name=None):
         """Get password of the username for the service
         """
         network = KWallet.Wallet.NetworkWallet()
-        wallet = open_kwallet(folder_name=service)
+        wallet = open_kwallet(app_name=app_name, folder_name=service)
         if wallet.keyDoesNotExist(network, service, username):
             return None
 
         # object.
         return unicode(result)
 
-    def set_password(self, service, username, password):
+    def set_password(self, service, username, password, app_name=None):
         """Set password for the username of the service
         """
-        wallet = open_kwallet(folder_name=service)
+        wallet = open_kwallet(app_name=app_name, folder_name=service)
         wallet.writePassword(username, password)
 
-    def delete_password(self, service, username):
+    def delete_password(self, service, username, app_name=None):
         """Delete the password for the username of the service.
         """
-        wallet = open_kwallet(folder_name=service)
+        wallet = open_kwallet(app_name=app_name, folder_name=service)
         if wallet.keyDoesNotExist(wallet.walletName(), service, username):
             raise PasswordDeleteError("Password not found")
         wallet.removeEntry(username)

keyring/tests/backends/test_kwallet.py

 from keyring.backends import kwallet
 from ..test_backend import BackendBasicTests
 
+app_name="python-keyring-lib Unit Test"
+
 
 def is_kwallet_supported():
     supported = kwallet.Keyring().supported()
         def __init__(self, *args):
             pass
 
+        def setApplicationName(self, app_name):
+            return
+
         def exit(self):
             pass
 
         # open_wallet() function should handle that eventuality by returning
         # None to signify that the KWallet backend is not available.
         self.assertEqual(
-            kwallet.open_kwallet(UnOpenableKWallet(), FauxQtGui()),
+            kwallet.open_kwallet(UnOpenableKWallet(), FauxQtGui(),
+                app_name=app_name),
             None)
 
 
             return
 
         app = QApplication([])
-        wallet = kwallet.open_kwallet()
+        wallet = kwallet.open_kwallet(app_name=app_name)
         self.assertTrue(isinstance(wallet, KWallet.Wallet),
                         msg="The object wallet should be type "
                         "<KWallet.Wallet> but it is: %s" % repr(wallet))