Commits

Jason R. Coombs  committed c40d550

More backends support priorities

  • Participants
  • Parent commits 51fdcfc
  • Branches prioritized backends

Comments (0)

Files changed (2)

File keyring/backends/Gnome.py

 import os
 
+try:
+    import gnomekeyring
+except ImportError:
+    pass
+
 from keyring.backend import KeyringBackend
 from keyring.errors import PasswordSetError, PasswordDeleteError
+from keyring.util import properties
 
 class Keyring(KeyringBackend):
     """Gnome Keyring"""
     # Use None for the default keyring.
     KEYRING_NAME = None
 
-    def supported(self):
-        try:
-            __import__('gnomekeyring')
-        except ImportError:
-            return -1
-        else:
-            if ("GNOME_KEYRING_CONTROL" in os.environ and
-                "DISPLAY" in os.environ and
-                "DBUS_SESSION_BUS_ADDRESS" in os.environ):
-                return 1
-            else:
-                return 0
+    requisite_vars = [
+        'GNOME_KEYRING_CONTROL',
+        'DISPLAY',
+        'DBUS_SESSION_BUS_ADDRESS',
+    ]
+
+    @properties.ClassProperty
+    @classmethod
+    def priority(cls):
+        if 'gnomekeyring' not in globals():
+            raise RuntimeError("gnomekeyring module required")
+        return int(cls.has_requisite_vars())
+
+    @classmethod
+    def has_requisite_vars(cls):
+        """
+        Return True if the requisite environment vars are present in the
+        environment.
+        """
+        return set(cls.requisite_vars).issubset(os.environ)
 
     def get_password(self, service, username):
         """Get password of the username for the service
         """
-        import gnomekeyring
-
         service = self._safe_string(service)
         username = self._safe_string(username)
         try:
     def set_password(self, service, username, password):
         """Set password for the username of the service
         """
-        import gnomekeyring
-
         service = self._safe_string(service)
         username = self._safe_string(username)
         password = self._safe_string(password)
     def delete_password(self, service, username):
         """Delete the password for the username of the service.
         """
-        import gnomekeyring
         try:
             items = gnomekeyring.find_network_password_sync(username, service)
             for current in items:

File keyring/backends/Google.py

 import cPickle
 import base64
 
+try:
+    import gdata.docs.service
+except ImportError:
+    pass
+
 from . import keyczar
 from keyring import errors
 from keyring import credentials
 import keyring.py25compat
 import keyring.py27compat
 from keyring.backend import KeyringBackend
+from keyring.util import properties
 
 class EnvironCredential(credentials.EnvironCredential):
     """Retrieve credentials from specifically named environment variables
                  collection=None, client=None,
                  can_create=True, input_getter=keyring.py27compat.input
                 ):
-        try:
-            DocsService = (__import__('gdata.docs.service')
-                .docs.service.DocsService)
-        except ImportError:
-            return
-
         self.credential = credential
         self.crypter = crypter
         self.source = source
         self._keyring_dict = None
 
         if not client:
-            self._client = DocsService()
+            self._client = gdata.docs.service.DocsService()
         else:
             self._client = client
 
         self._client.ssl = True
         self._login_reqd = True
 
-    def supported(self):
-        """Return if this keyring supports current environment:
-        -1: not applicable
-         0: suitable
-         1: recommended
-        """
-        try:
-            __import__('gdata.docs.service')
-        except ImportError:
-            return -1
-        return 0
+    @properties.ClassProperty
+    @classmethod
+    def priority(cls):
+        if 'gdata' not in globals():
+            raise RuntimeError("Requires gdata")
+        if not hasattr(keyczar, 'keyczar'):
+            raise RuntimeError("Requires keyczar")
+        return 3
 
     def get_password(self, service, username):
         """Get password of the username for the service
     @property
     def client(self):
         if not self._client.GetClientLoginToken():
-            import gdata
             try:
                 self._client.ClientLogin(self.credential.username,
                                          self.credential.password,