Commits

Jason R. Coombs  committed 5f1c923

Re-implemented get_all_keyring to get keyring classes from the KeyringBackend base class.

  • Participants
  • Parent commits c6d0183

Comments (0)

Files changed (2)

File keyring/backend.py

         super(EnvironEncryptedPyfilesystemKeyring, self).__init__(
             EnvironKeyczarCrypter())
 
-_all_keyring = None
-
 class MultipartKeyringWrapper(KeyringBackend):
 
     """A wrapper around an existing keyring that breaks the password into
                 curr_username += '{{part_%d}}' %i
             self._keyring.set_password(service, curr_username, password_part)
 
+@keyring.util.once
 def get_all_keyring():
-    """Return the list of all keyrings in the lib
     """
-    global _all_keyring
-    if _all_keyring is None:
-        _all_keyring = [OSXKeychain(), GnomeKeyring(), KDEKWallet(),
-                        CryptedFileKeyring(), UncryptedFileKeyring(),
-                        Win32CryptoKeyring(), Win32CryptoRegistry(),
-                        WinVaultKeyring(), SecretServiceKeyring(),
-                        EnvironGoogleDocsKeyring(),
-                        UnencryptedPyfilesystemKeyring(),
-                        EnvironEncryptedPyfilesystemKeyring()]
-    return _all_keyring
+    Return a list of all implemented keyrings.
+    """
+    return [
+        keyring_class()
+        for keyring_class in KeyringBackend._classes
+    ]

File keyring/util/__init__.py

+try:
+    import functools
+except ImportError:
+    # functools not available until Python 2.5
+    pass
+
+def once(func):
+    """
+    Decorate func so it's only ever called the first time.
+
+    This decorator can ensure that an expensive or non-idempotent function
+    will not be expensive on subsequent calls and is idempotent.
+
+    >>> func = once(lambda a: a+3)
+    >>> func(3)
+    6
+    >>> func(9)
+    6
+    >>> func('12')
+    6
+    """
+    def wrapper(*args, **kwargs):
+        if not hasattr(func, 'always_returns'):
+            func.always_returns = func(*args, **kwargs)
+        return func.always_returns
+    if 'functools' in globals():
+        wrapper = functools.wraps(func)(wrapper)
+    return wrapper