Skip to content

Commit

Permalink
Renamed KeyRingLib to Keyring
Browse files Browse the repository at this point in the history
Signed-off-by: Andreas Maier <andreas.r.maier@gmx.de>
  • Loading branch information
andy-maier committed Apr 4, 2021
1 parent d418bc2 commit 8d6bbee
Show file tree
Hide file tree
Showing 8 changed files with 75 additions and 72 deletions.
14 changes: 7 additions & 7 deletions docs/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,12 @@ EasyVault class
:special-members: __str__


.. _`KeyRingLib class`:
.. _`Keyring class`:

KeyRingLib class
----------------
Keyring class
-------------

.. autoclass:: easy_vault.KeyRingLib
.. autoclass:: easy_vault.Keyring
:members:
:autosummary:
:autosummary-inherited-members:
Expand Down Expand Up @@ -83,15 +83,15 @@ Exception classes
:members:
:special-members: __str__

.. autoclass:: easy_vault.KeyRingException
.. autoclass:: easy_vault.KeyringException
:members:
:special-members: __str__

.. autoclass:: easy_vault.KeyRingNotAvailable
.. autoclass:: easy_vault.KeyringNotAvailable
:members:
:special-members: __str__

.. autoclass:: easy_vault.KeyRingError
.. autoclass:: easy_vault.KeyringError
:members:
:special-members: __str__

Expand Down
3 changes: 3 additions & 0 deletions docs/changes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ Released: not yet

**Incompatible changes:**

* The KeyRingLib class has been renamed to Keyring, and its KeyRing...
exception classes to Keyring... (issue #50).

**Deprecations:**

**Bug fixes:**
Expand Down
2 changes: 1 addition & 1 deletion easy_vault/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
from __future__ import absolute_import, print_function

from ._easy_vault import * # noqa: F403,F401
from ._key_ring_lib import * # noqa: F403,F401
from ._keyring import * # noqa: F403,F401
from ._password import * # noqa: F403,F401
from . import _version

Expand Down
56 changes: 28 additions & 28 deletions easy_vault/_key_ring_lib.py → easy_vault/_keyring.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
# limitations under the License.

"""
The KeyRingLib class.
The Keyring class.
"""

from __future__ import absolute_import, print_function
Expand All @@ -21,8 +21,8 @@
import keyring.backends.null # Required to import separately
import keyring.backends.fail

__all__ = ['KeyRingLib', 'KeyRingException', 'KeyRingNotAvailable',
'KeyRingError']
__all__ = ['Keyring', 'KeyringException', 'KeyringNotAvailable',
'KeyringError']


# Service/app name used for keyring items:
Expand All @@ -38,35 +38,35 @@
NO_KEYRING_EXCEPTION = RuntimeError


class KeyRingException(Exception):
class KeyringException(Exception):
"""
Base exception for all exceptions raised by the
:class:`~easy_vault.KeyRingLib` class.
:class:`~easy_vault.Keyring` class.
Derived from :exc:`~py:Exception`.
"""
pass


class KeyRingNotAvailable(KeyRingException):
class KeyringNotAvailable(KeyringException):
"""
Exception indicating that the keyring service is not available.
Derived from :exc:`KeyRingException`.
Derived from :exc:`KeyringException`.
"""
pass


class KeyRingError(KeyRingException):
class KeyringError(KeyringException):
"""
Exception indicating that an error happend in the keyring service.
Derived from :exc:`KeyRingException`.
Derived from :exc:`KeyringException`.
"""
pass


class KeyRingLib(object):
class Keyring(object):
"""
Access to the keyring service of the local system for storing vault
passwords.
Expand Down Expand Up @@ -97,16 +97,16 @@ def get_password(self, filepath):
:term:`unicode string`: Password for the vault file, or `None`.
Raises:
:exc:`KeyRingNotAvailable`: No keyring service available.
:exc:`KeyRingError`: An error happend in the keyring service.
:exc:`KeyringNotAvailable`: No keyring service available.
:exc:`KeyringError`: An error happend in the keyring service.
"""
try:
return keyring.get_password(
self.keyring_service(), self.keyring_username(filepath))
except NO_KEYRING_EXCEPTION as exc:
raise KeyRingNotAvailable(str(exc))
raise KeyringNotAvailable(str(exc))
except keyring.errors.KeyringError as exc:
raise KeyRingError(str(exc))
raise KeyringError(str(exc))

def set_password(self, filepath, password):
"""
Expand All @@ -122,8 +122,8 @@ def set_password(self, filepath, password):
Password for the vault file.
Raises:
:exc:`KeyRingNotAvailable`: No keyring service available.
:exc:`KeyRingError`: An error happend in the keyring service.
:exc:`KeyringNotAvailable`: No keyring service available.
:exc:`KeyringError`: An error happend in the keyring service.
"""
keyring.set_password(
self.keyring_service(), self.keyring_username(filepath), password)
Expand All @@ -141,7 +141,7 @@ def is_available(self):
"""
try:
self.check_available()
except KeyRingNotAvailable:
except KeyringNotAvailable:
return False
return True

Expand All @@ -156,7 +156,7 @@ def check_available():
some information about the keyring configuration.
Raises:
:exc:`KeyRingNotAvailable`: No keyring service available.
:exc:`KeyringNotAvailable`: No keyring service available.
"""

# Check the cases where the keyring package indicates it has no
Expand All @@ -166,37 +166,37 @@ def check_available():

if isinstance(backend, keyring.backends.chainer.ChainerBackend):
if not backend.backends:
raise KeyRingNotAvailable(
raise KeyringNotAvailable(
"No keyring service found by the configured backends")

if isinstance(backend, keyring.backends.fail.Keyring):
raise KeyRingNotAvailable(
raise KeyringNotAvailable(
"No keyring service found by the configured backends")

if isinstance(backend, keyring.backends.null.Keyring):
raise KeyRingNotAvailable(
raise KeyringNotAvailable(
"Keyring service disabled by a configured null backend")

# In theory, now the keyring service should be available.
# We try it out to really make sure.

keyringlib = KeyRingLib()
service = keyringlib.keyring_service()
username = keyringlib.keyring_username('deleteme:check_available')
kr = Keyring()
service = kr.keyring_service()
username = kr.keyring_username('deleteme:check_available')
try:
keyring.set_password(service, username, 'dummy')
except NO_KEYRING_EXCEPTION as exc:
new_exc = KeyRingNotAvailable(
new_exc = KeyringNotAvailable(
"Keyring test call failed with: {msg}".format(msg=exc))
new_exc.__cause__ = None
raise new_exc # KeyRingNotAvailable
raise new_exc # KeyringNotAvailable
try:
keyring.delete_password(service, username)
except Exception as exc:
new_exc = KeyRingNotAvailable(
new_exc = KeyringNotAvailable(
"Keyring cleanup call failed with: {msg}".format(msg=exc))
new_exc.__cause__ = None
raise new_exc # KeyRingNotAvailable
raise new_exc # KeyringNotAvailable

@staticmethod
def keyring_service():
Expand Down
26 changes: 13 additions & 13 deletions easy_vault/_password.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
from __future__ import absolute_import, print_function

import getpass
from ._key_ring_lib import KeyRingLib
from ._keyring import Keyring

__all__ = ['get_password', 'set_password']

Expand All @@ -38,7 +38,7 @@ def get_password(
stored.
This is a convenience function that uses the password methods of the
:class:`~easy_vault.KeyRingLib` class.
:class:`~easy_vault.Keyring` class.
Parameters:
Expand All @@ -64,17 +64,17 @@ def get_password(
Raises:
ValueError: Use of keyring service and use of password prompting were
both disabled.
:exc:`KeyRingNotAvailable`: No keyring service available.
:exc:`KeyRingError`: An error happend in the keyring service.
:exc:`KeyringNotAvailable`: No keyring service available.
:exc:`KeyringError`: An error happend in the keyring service.
"""
password = None

if not use_keyring and not use_prompting:
raise ValueError("use_keyring and use_prompt were both False")

if use_keyring:
keyringlib = KeyRingLib()
password = keyringlib.get_password(filepath)
kr = Keyring()
password = kr.get_password(filepath)
if password is not None:
if verbose:
echo("Using password from keyring service")
Expand All @@ -97,7 +97,7 @@ def set_password(
can be disabled, in which case the function does nothing.
This is a convenience function that uses the password methods of the
:class:`~easy_vault.KeyRingLib` class.
:class:`~easy_vault.Keyring` class.
Parameters:
Expand All @@ -119,17 +119,17 @@ def set_password(
Print function to be used for the additional messages in verbose mode.
Raises:
:exc:`KeyRingNotAvailable`: No keyring service available.
:exc:`KeyRingError`: An error happend in the keyring service.
:exc:`KeyringNotAvailable`: No keyring service available.
:exc:`KeyringError`: An error happend in the keyring service.
"""
if use_keyring:
keyringlib = KeyRingLib()
current_password = keyringlib.get_password(filepath)
kr = Keyring()
current_password = kr.get_password(filepath)
if current_password is None:
if verbose:
echo("Setting new password in keyring service")
keyringlib.set_password(filepath, password)
kr.set_password(filepath, password)
elif password != current_password:
if verbose:
echo("Updating password in keyring service")
keyringlib.set_password(filepath, password)
kr.set_password(filepath, password)
6 changes: 3 additions & 3 deletions easy_vault/cli/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

from ._common_options import add_options, help_option, quiet_option
from .._version import __version__ as cli_version
from .._key_ring_lib import KeyRingLib, KeyRingNotAvailable
from .._keyring import Keyring, KeyringNotAvailable
from .._easy_vault import EasyVault, EasyVaultException
from .._password import get_password, set_password

Expand Down Expand Up @@ -193,8 +193,8 @@ def cli_check_keyring(**options):
verbose = not options['quiet']

try:
KeyRingLib.check_available()
except KeyRingNotAvailable as exc:
Keyring.check_available()
except KeyringNotAvailable as exc:
if verbose:
click.echo("Error: {}".format(exc))
click.get_current_context().exit(1)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@
# limitations under the License.

"""
Test the _key_ring_lib.py module.
Test the _keyring.py module.
"""

from __future__ import absolute_import, print_function

import pytest
from easy_vault import KeyRingLib, KeyRingNotAvailable
from easy_vault import Keyring, KeyringNotAvailable

# pylint: disable=unused-import
from ..utils.keyring_utils import keyring_filepath # noqa: F401
Expand All @@ -27,42 +27,42 @@
@pytest.mark.skipif(
not is_keyring_available(), reason="No keyring service available")
# pylint: disable=redefined-outer-name
def test_keyringlib_get_set(keyring_filepath):
def test_keyring_get_set(keyring_filepath):
"""
Test function for KeyRingLib.get_password() / set_password()
Test function for Keyring.get_password() / set_password()
"""
keyringlib = KeyRingLib()
kr = Keyring()
password = 'mypassword'

# Test that the password does not exist
act_password = keyringlib.get_password(keyring_filepath)
act_password = kr.get_password(keyring_filepath)
assert act_password is None

# Test that setting a password succeeds
keyringlib.set_password(keyring_filepath, password)
kr.set_password(keyring_filepath, password)

# Test that getting a password succeeds and is as expected
act_password = keyringlib.get_password(keyring_filepath)
act_password = kr.get_password(keyring_filepath)
assert act_password == password


def test_keyringlib_available():
def test_keyring_available():
"""
Test function for KeyRingLib.is_available()
Test function for Keyring.is_available()
"""
keyringlib = KeyRingLib()
kr = Keyring()

# Code to be tested
is_avail = keyringlib.is_available()
is_avail = kr.is_available()

assert isinstance(is_avail, bool)

check_avail = True
try:
# Code to be tested
keyringlib.check_available()
kr.check_available()
except Exception as exc: # pylint: disable=broad-except
assert isinstance(exc, KeyRingNotAvailable)
assert isinstance(exc, KeyringNotAvailable)
check_avail = False

assert check_avail == is_avail

0 comments on commit 8d6bbee

Please sign in to comment.