Git Product home page Git Product logo

python-keyring-lib's Introduction

Installing and Using Python Keyring Lib

The Python keyring lib provides a easy way to access the system keyring service from python. It can be used in any application that needs safe password storage.

The keyring library is licensed under both the MIT license and the PSF license.

These primary keyring services are supported by the Python keyring lib:

  • Mac OS X Keychain
  • Linux Secret Service
  • Windows Credential Vault

Other keyring implementations are provided as well. For more detail, browse the source.

Run easy_install or pip:

$ easy_install keyring
$ pip install keyring

Download the source tarball from https://pypi.python.org/pypi/keyring, uncompress it, and then run "setup.py install".

The basic usage of keyring is pretty simple: just call keyring.set_password and keyring.get_password:

>>> import keyring
>>> keyring.set_password("system", "username", "password")
>>> keyring.get_password("system", "username")
'password'

The python keyring lib contains implementations for several backends. The library will automatically choose the keyring that is most suitable for your current environment. You can also specify the keyring you like to be used in the config file or by calling the set_keyring() function.

This section describes how to change your option in the config file.

The configuration of the lib is stored in a file named "keyringrc.cfg". This file must be found in a platform-specific location. To determine where the config file is stored, run the following:

python -c "import keyring.util.platform_; print(keyring.util.platform_.config_root())"

Some keyrings also store the keyring data in the file system. To determine where the data files are stored, run this command:

python -c "import keyring.util.platform_; print(keyring.util.platform_.data_root())"

To specify a keyring backend, set the default-keyring option to the full path of the class for that backend, such as keyring.backends.OS_X.Keyring.

If keyring-path is indicated, keyring will add that path to the Python module search path before loading the backend.

For example, this config might be used to load the SimpleKeyring from the demo directory in the project checkout:

[backend]
default-keyring=simplekeyring.SimpleKeyring
keyring-path=/home/kang/pyworkspace/python-keyring-lib/demo/

The interface for the backend is defined by keyring.backend.KeyringBackend. Every backend should derive from that base class and define a priority attribute and three functions: get_password(), set_password(), and delete_password().

See the backend module for more detail on the interface of this class.

Keyring additionally allows programmatic configuration of the backend calling the api set_keyring(). The indicated backend will subsequently be used to store and retrieve passwords.

Here's an example demonstrating how to invoke set_keyring:

# define a new keyring class which extends the KeyringBackend
import keyring.backend

class TestKeyring(keyring.backend.KeyringBackend):
    """A test keyring which always outputs same password
    """
    priority = 1

    def set_password(self, servicename, username, password):
        pass

    def get_password(self, servicename, username):
        return "password from TestKeyring"

    def delete_password(self, servicename, username, password):
        pass

# set the keyring for keyring lib
keyring.set_keyring(TestKeyring())

# invoke the keyring lib
try:
    keyring.set_password("demo-service", "tarek", "passexample")
    print("password stored sucessfully")
except keyring.errors.PasswordSetError:
    print("failed to store password")
print("password", keyring.get_password("demo-service", "tarek"))

The keyring lib has a few functions:

  • get_keyring(): Return the currently-loaded keyring implementation.
  • get_password(service, username): Returns the password stored in the active keyring. If the password does not exist, it will return None.
  • set_password(service, username, password): Store the password in the keyring.
  • delete_password(service, username): Delete the password stored in keyring. If the password does not exist, it will raise an exception.

Python keyring lib is an open community project and highly welcomes new contributors.

Tests are continuously run using Travis-CI.

BuildStatus

To run the tests yourself, you'll want keyring installed to some environment in which it can be tested. Three recommended techniques are described below.

Keyring is instrumented with pytest runner. Thus, you may invoke the tests from any supported Python (with distribute installed) using this command:

python setup.py ptr

pytest runner will download any unmet dependencies and run the tests using pytest.

This technique is the one used by the Travis-CI script.

Pytest and Nose are two popular test runners that will discover tests and run them. Unittest (unittest2 under Python 2.6) also has a mode to discover tests.

First, however, these test runners typically need a test environment in which to run. It is recommended that you install keyring to a virtual environment to avoid interfering with your system environment. For more information, see the venv documentation or the virtualenv homepage.

After you've created (or designated) your environment, install keyring into the environment by running:

python setup.py develop

Then, invoke your favorite test runner, e.g.:

py.test

or:

nosetests

Keyring supplies a buildout.cfg for use with buildout. If you have buildout installed, tests can be invoked as so:

1. bin/buildout  # prepare the buildout.
2. bin/test  # execute the test runner.

For more information about the options that the script provides do execute:

python bin/test --help

The project was based on Tarek Ziade's idea in this post. Kang Zhang initially carried it out as a Google Summer of Code project, and Tarek mentored Kang on this project.

See CONTRIBUTORS.txt for a complete list of contributors.

python-keyring-lib's People

Contributors

jaraco avatar kangzhang avatar multani avatar mitya57 avatar benji-york avatar tarekziade avatar rl-0x0 avatar maciex avatar mekk avatar sborho avatar cournape avatar jonnyjd avatar mata-p avatar hefee avatar mandel-macaque avatar fraca7 avatar dholth avatar bretth avatar dims avatar jelmer avatar mdeslaur avatar rhafer avatar sebastinas avatar stefwalter avatar takluyver avatar kstark avatar swdyh avatar

Watchers

Philip Neustrom avatar James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.