Commits

Benji York committed 914d9ae

replace C/C++ extension modules for Gnome and KDE integration with Python
modules

Comments (0)

Files changed (3)

 Created by Kang Zhang on 2009-08-07
 """
 
-import os
 import sys
-import commands
 
 from distutils.core import Extension
 
-def pkg_check(packages):
-    """Return false if not all packages has been installed properly.
-    """
-    status, output = commands.getstatusoutput("pkg-config --exists %s" %
-                                                    ' '.join(packages))
-    return len(output) == 0 and status == 0
-
-def pkg_config(packages):
-    """Return the config parameters for all packages
-    """
-    keywords = {}
-    flag_map = {'-I':'include_dirs', '-L':'library_dirs', '-l':'libraries'}
-
-    for token in commands.getoutput("pkg-config --libs --cflags %s" %
-                                                ' '.join(packages)).split():
-        try:
-            key = flag_map[token[:2]]
-            keywords.setdefault(key, []).append(token[2:])
-        except KeyError:
-            keywords.setdefault('extra_link_args', []).append(token)
-
-    return keywords
-
-def kde_exec(cmd, option):
-    """Execute the kde-config command and get the output dirs
-    """
-    return commands.getoutput("%s %s --expandvars" % (cmd, option)).split(':')
-
-def kde_check(cmd, headfiles):
-    includes = kde_exec(cmd, '--install include')
-    for filename in headfiles:
-        # generate all possible paths for the headfile
-        paths = [os.path.join(dir, filename) for dir in includes]
-        # check if file exists on any path
-        exists = any([ os.path.exists(path) for path in paths])
-        if not exists:
-            return False
-    return True
-
-def kde_config(cmd, keywords):
-    """Add the compile parameters for kdelibs
-    """
-
-    # KDE guys hate pkg-config, so we need due with it seperately. :-(
-    # See following link for more details
-    #       http://lists.kde.org/?t=109647896600005&r=1&w=2
-
-    keywords.setdefault('libraries', []).append('kdeui')
-
-    libs = kde_exec(cmd, '--path lib')
-    includes = kde_exec(cmd, '--install include')
-
-    keywords.setdefault('library_dirs', []).extend(libs)
-    keywords.setdefault('include_dirs', []).extend(includes)
-    return keywords
 
 def get_extensions():
     """Collect the extensions that can be installed.
                             'CoreServices'])
         exts.append(osx_keychain_module)
 
-    gnome_keyring_libs = ['dbus-1', 'glib-2.0', 'gnome-keyring-1']
-    if pkg_check(gnome_keyring_libs):
-        # gnome-keyring installed
-        gnome_keychain_module = Extension('gnome_keyring',
-                        sources = ['keyring/backends/gnome_keyring.c'],
-                        **pkg_config(gnome_keyring_libs)
-            )
-        exts.append(gnome_keychain_module)
-
-    # check for KWallet heardfiles
-    kde_kwallet_headfiles = ['kwallet.h']
-    kde_cmd = None
-    for cmd in ('kde-config','kde4-config'):
-        if kde_check(cmd, kde_kwallet_headfiles):
-            kde_cmd = cmd
-
-    # check for Kwallet related libs
-    kde_kwallet_libs = ['dbus-1', 'glib-2.0', 'QtGui']
-    if pkg_check(kde_kwallet_libs) and kde_cmd is not None:
-        # KDE Kwallet is installed.
-        kde_kwallet_module = Extension('kde_kwallet',
-                        sources = ['keyring/backends/kde_kwallet.cpp'],
-                        **kde_config(kde_cmd, pkg_config(kde_kwallet_libs))
-            )
-        exts.append(kde_kwallet_module)
-
     if platform in ['win32'] and sys.getwindowsversion()[-2] == 2:
         # windows 2k+
         win32_crypto_module = Extension('win32_crypto',
     def abstractmethod(funcobj):
         return funcobj
 
+try:
+    import gnomekeyring
+except ImportError:
+    pass
 
 _KEYRING_SETTING = 'keyring-setting'
 _CRYPTED_PASSWORD = 'crypted-password'
         """
         return sys.platform == 'darwin'
 
-class GnomeKeyring(_ExtensionKeyring):
+class GnomeKeyring(KeyringBackend):
     """Gnome Keyring"""
-    def _init_backend(self):
-        """Return the gnome_keyring handler.
+
+    def supported(self):
+        try:
+            import gnomekeyring
+        except ImportError:
+            return -1
+        else:
+            return 1
+
+    def get_password(self, service, username):
+        """Get password of the username for the service
         """
-        import gnome_keyring
-        return gnome_keyring
+        try:
+            items = gnomekeyring.find_network_password_sync(username, service)
+        except gnomekeyring.NoMatchError:
+            return None
+        except gnomekeyring.CancelledError:
+            # The user pressed "Cancel" when prompted to unlock their keyring.
+            return None
 
-    def _recommend(self):
-        """Recommend this keyring when Gnome is running.
+        assert len(items) == 1, 'no more than one entry should ever match'
+        return items[0]['password']
+
+    def set_password(self, service, username, password):
+        """Set password for the username of the service
         """
-        # Gnome is running
-        return os.getenv("GNOME_DESKTOP_SESSION_ID") is not None
+        try:
+            gnomekeyring.set_network_password_sync(None, username, service,
+                None, None, None, None, 0, password)
+        except gnomekeyring.CancelledError:
+            # The user pressed "Cancel" when prompted to unlock their keyring.
+            raise PasswordSetError()
 
 
-class KDEKWallet(_ExtensionKeyring):
+kwallet = None
+try:
+    from PyKDE4.kdeui import KWallet
+    from PyQt4 import QtCore, QtGui
+except ImportError:
+    kwallet = None
+else:
+    # KDE wants us to instantiate an application object.
+    app = QtGui.QApplication([])
+    kwallet = KWallet.Wallet.openWallet(
+        KWallet.Wallet.NetworkWallet(), KWallet.Wallet.Synchronous)
+    if not kwallet.hasFolder('Python'):
+        kwallet.createFolder('Python')
+    kwallet.setFolder('Python')
+    app.exit()
+
+class KDEKWallet(KeyringBackend):
     """KDE KWallet"""
-    def _init_backend(self):
-        """Return the kde_kwallet handler.
+
+    def supported(self):
+        if kwallet is None:
+            return -1
+        else:
+            return 1
+
+    def get_password(self, service, username):
+        """Get password of the username for the service
         """
-        import kde_kwallet
-        return kde_kwallet
+        key = username + '@' + service
+        network = KWallet.Wallet.NetworkWallet()
+        if kwallet.keyDoesNotExist(network, 'Python', key):
+            return None
 
-    def _recommend(self):
-        """Recommend this keyring backend when KDE is running.
+        result = kwallet.readPassword(key)[1]
+        # The string will be a PyQt4.QtCore.QString, so turn it into a unicode
+        # object.
+        return unicode(result)
+
+    def set_password(self, service, username, password):
+        """Set password for the username of the service
         """
-        # KDE is running
-        return os.getenv("KDE_FULL_SESSION") == "true"
+        kwallet.writePassword(username+'@'+service, password)
 
 class BasicFileKeyring(KeyringBackend):
     """BasicFileKeyring is a filebased implementation of keyring.

keyring/tests/test_backend.py

         return keyring.backend.GnomeKeyring()
 
     def supported(self):
-        try:
-            import gnome_keyring
-        except ImportError:
-            return -1
-        if os.getenv("GNOME_DESKTOP_SESSION_ID") is not None:
-            return 1
-        return 0
+        return self.keyring.supported()
 
 class KDEKWalletTestCase(BackendBasicTestCase):
     __test__ = True
         return keyring.backend.KDEKWallet()
 
     def supported(self):
-        try:
-            import kde_kwallet
-        except ImportError:
-            return -1
-        if os.getenv("KDE_FULL_SESSION") == "true":
-            return 1
-        return 0
+        return self.keyring.supported()
 
 class FileKeyringTestCase(BackendBasicTestCase):
     __test__ = False