17
20
global _keyring_backend
18
21
if isinstance(keyring, backend.KeyringBackend):
19
22
_keyring_backend = keyring
20
else: raise TypeError("The keyring must be a subclass of KeyringBackend")
24
raise TypeError("The keyring must be a subclass of KeyringBackend")
23
27
"""Get current keyring backend.
30
34
return _keyring_backend.get_password(service_name, username)
32
36
def set_password(service_name, username, password):
33
"""Set password for the user in the spcified service
37
"""Set password for the user in the specified service
35
39
_keyring_backend.set_password(service_name, username, password)
37
41
def init_backend():
38
"""first try to load the keyring in the config file, if it has not
39
been decleared, assign a defult keyring according to the platform.
42
"""Load a keyring from a config file or for the default platform.
44
First try to load the keyring in the config file, if it has not
45
been declared, assign a default keyring according to the platform.
41
#select a backend according to the config file
47
# select a backend according to the config file
42
48
keyring = load_config()
44
# if the user dose not specify a keyring, we apply a default one
50
# if the user doesn't specify a keyring, we apply a default one
45
51
if keyring is None:
47
53
keyrings = backend.get_all_keyring()
48
# rank according the supported
49
keyrings.sort(lambda x, y: y.supported() - x.supported())
50
# get the most recommend one
54
# rank according to the supported result
55
keyrings.sort(key = lambda x: -x.supported())
56
# get the most recommended one
51
57
keyring = keyrings[0]
53
59
set_keyring(keyring)
56
62
def load_keyring(keyring_path, keyring_name):
57
63
"""Load the specified keyring name from the specified path
59
`keyring_path` can be None and it will not interfer with the loading
65
`keyring_path` can be None and it will not interfere with the loading
92
98
module = load_module(keyring_name, sys.path+[keyring_path])
94
100
keyring_class = keyring_name.split('.')[-1].strip()
95
exec "keyring_temp = module." + keyring_class + "() " in locals()
101
keyring_temp = getattr(module, keyring_class)()
97
103
return keyring_temp
100
106
def load_config():
101
"""load a keyring using the config file, the config file can be
102
in the current working directory, or in the user's home directory.
107
"""Load a keyring using the config file.
109
The config file can be in the current working directory, or in the user's
106
114
# search from current working directory and the home folder
107
115
keyring_cfg_list = [os.path.join(os.getcwd(), "keyringrc.cfg"),
108
116
os.path.join(os.path.expanduser("~"), "keyringrc.cfg")]
109
# initial the keyring_cfg with the fist detected config file
118
# initialize the keyring_config with the first detected config file
110
119
keyring_cfg = None
111
120
for path in keyring_cfg_list:
112
121
keyring_cfg = path
122
131
keyring_path = config.get("backend", "keyring-path").strip()
124
133
keyring_path = None
125
except ConfigParser.NoOptionError:
134
except config_parser.NoOptionError:
126
135
keyring_path = None
128
# load the keyring class name, and load it
137
# load the keyring class name, and then load this keyring
130
139
if config.has_section("backend"):
131
140
keyring_name = config.get("backend", "default-keyring").strip()
133
raise ConfigParser.NoOptionError('backend', 'default-keyring')
142
raise config_parser.NoOptionError('backend', 'default-keyring')
135
144
keyring = load_keyring(keyring_path, keyring_name)
136
except (ConfigParser.NoOptionError, ImportError):
137
logger.warning("Keyring Config file does not write correctly.\n" + \
145
except (config_parser.NoOptionError, ImportError):
146
logger.warning("Keyring config file contains incorrect values.\n" +
138
147
"Config file: %s" % keyring_cfg)
142
151
# init the _keyring_backend