There's already a pototype demo in our demo folder(demo/keyring_demo.py). Here's some snippets from the demo.
KEYRINGRC = ".keyringrc"
def load_keyring_by_config():
"""
This function shows how to enable a keyring using config file
"""
# create the config file
f = open(KEYRINGRC,'w')
f.writelines(["[backend]\n",
# the path for the user created keyring
"keyring-path= %s\n" % str(os.path.abspath(__file__))[:-16],
# the name of the keyring class
"default-keyring=simplekeyring.SimpleKeyring\n" ])
f.close()
# import the keyring lib, the lib will automaticlly load the
# config file and load the user defined module
import keyring
# invoke the keyring to store and fetch the password
if keyring.setpass("demo-service","tarek","passexample") == 0:
print "password stored sucessful"
print "password", keyring.getpass("demo-service","tarek")
os.remove(KEYRINGRC)
def set_keyring_in_runtime():
"""
This function shows how to create a keyring manully and use it in runtime
"""
# define a new keyring class which extends the KeyringBackend
import keyring.backend
class TestKeyring(keyring.backend.KeyringBackend):
def setpass(self,servicename,username,password): return 0
def getpass(self,servicename,username): return "password from TestKeyring"
# set the keyring for keyring lib
import keyring
keyring.set_keyring(TestKeyring())
# invoke the keyring lib
if keyring.setpass("demo-service","tarek","passexample") == 0:
print "password stored successful"
print "password", keyring.getpass("demo-service","tarek")
That two funtions illustrate the process of enabling a user created keyring by the config file and set the keyring in the runtime. The keyring class that load from disk is stored in demo/simplekeyring.py. Here's the definition of the keyring class.
class SimpleKeyring(KeyringBackend):
"""Simple Keyring is a keyring which can store only one
password in memory.
"""
def __init__(self):
self.password = ''
def getpass(self,servicename,username):
return self.password
def setpass(self,servicename,username,password):
print "calling SimpleKeyring.setpass()"
self.password = password
return 0
For more details, please refer to our repository
0 comments:
Post a Comment