Exemplo n.º 1
0
 def test_smart_search(self):
     """Test smart searching."""
     with TemporaryDirectory() as directory:
         touch(os.path.join(directory, 'abcdef.gpg'))
         touch(os.path.join(directory, 'aabbccddeeff.gpg'))
         touch(os.path.join(directory, 'Google.gpg'))
         program = PasswordStore(directory=directory)
         # Test a substring match that avoids fuzzy matching.
         matches = program.smart_search('abc')
         assert len(matches) == 1
         assert matches[0].name == 'abcdef'
         # Test a fuzzy match to confirm that the fall back works.
         matches = program.smart_search('gg')
         assert len(matches) == 1
         assert matches[0].name == 'Google'
Exemplo n.º 2
0
def get_secret_from_store(name, directory=None):
    """
    Use :mod:`qpass` to get a secret from ``~/.password-store``.

    :param name: The name of a password or a search pattern that matches a
                 single entry in the password store (a string).
    :param directory: The directory to use (a string, defaults to
                      ``~/.password-store``).
    :returns: The secret (a string).
    :raises: :exc:`exceptions.ValueError` when the given `name` doesn't match
             any entries or matches multiple entries in the password store.
    """
    kw = dict(directory=directory) if directory else {}
    store = PasswordStore(**kw)
    matches = store.smart_search(name)
    if len(matches) != 1:
        msg = "Expected exactly one match in password database! (input: %s)"
        raise ValueError(format(msg, name))
    return matches[0].password