4
Created by Kang Zhang on 2009-07-09
12
def set_keyring(keyring):
13
"""Set current keyring backend.
15
global _keyring_backend
16
if isinstance(keyring, backend.KeyringBackend):
17
_keyring_backend = keyring
18
else: raise TypeError("The keyring must be a subclass of KeyringBackend")
21
"""Get current keyring backend.
23
return _keyring_backend
25
def get_password(service_name, username):
26
"""Get password from the specified service
28
return _keyring_backend.get_password(service_name, username)
30
def set_password(service_name, username, password):
31
"""Set password for the user in the spcified service
33
return _keyring_backend.set_password(service_name, username, password)
36
"""first try to load the keyring in the config file, if it has not
37
been decleared, assign a defult keyring according to the platform.
39
#select a backend according to the config file
40
keyring_impl = load_config()
42
# if the user dose not specify a keyring, we apply a default one
43
if keyring_impl is None:
45
keyrings = backend.get_all_keyring()
46
# rank according the supported
47
keyrings.sort(lambda x, y: y.supported() - x.supported())
48
# get the most recommend one
49
keyring_impl = keyrings[0]
51
set_keyring(keyring_impl)
54
"""load a keyring using the config file, the config file can be
55
in the current working directory, or in the user's home directory.
59
# search from current working directory and the home folder
60
keyring_cfg_list = [os.path.join(os.getcwd(), "keyringrc.cfg"),
61
os.path.join(os.path.expanduser("~"), "keyringrc.cfg")]
62
# initial the keyring_cfg with the fist detected config file
64
for path in keyring_cfg_list:
66
if os.path.exists(path):
69
if os.path.exists(keyring_cfg):
70
config = ConfigParser.RawConfigParser()
71
config.read(keyring_cfg)
72
# load the keyring-path option
74
keyring_path = config.get("backend", "keyring-path").strip()
75
except ConfigParser.NoOptionError:
77
# load the keyring class name, and load it
79
keyring_name = config.get("backend", "default-keyring").strip()
81
def load_module(name, path):
82
"""Load the specified module from the disk.
84
path_list = name.split('.')
85
module_info = imp.find_module(path_list[0], path)
86
module_file, pathname, description = module_info
87
module = imp.load_module(path_list[0], module_file, \
88
pathname, description)
92
if len(path_list) > 1:
93
# for the class name containing dots
94
sub_name = '.'.join(path_list[1:])
98
sub_path = path + module.__path__
99
except AttributeError:
102
return load_module(sub_name, sub_path)
106
# avoid import the imported modules
107
module = sys.modules[keyring_name[:keyring_name.rfind('.')]]
109
module = load_module( keyring_name, sys.path+[keyring_path])
111
keyring_class = keyring_name.split('.')[-1].strip()
112
exec "keyring_temp = module." + keyring_class + "() " in locals()
114
keyring_impl = keyring_temp
115
except (ConfigParser.NoOptionError, ImportError):
116
logger.warning("Keyring Config file does not write correctly.\n" + \
117
"Config file: %s" % keyring_cfg)
121
# init the _keyring_backend