2
2
"""Simple command line interface to get/set password from a keyring"""
5
from optparse import OptionParser
12
def input_password(prompt):
13
"""Ask for a password to the user.
15
This mostly exists to ease the testing process.
18
return getpass.getpass(prompt)
21
def output_password(password):
22
"""Output the password to the user.
24
This mostly exists to ease the testing process.
12
class CommandLineTool(object):
14
self.parser = OptionParser(usage="%prog [get|set] SERVICE USERNAME")
15
self.parser.add_option("-p", "--keyring-path",
16
dest="keyring_path", default=None,
17
help="Path to the keyring backend")
18
self.parser.add_option("-b", "--keyring-backend",
19
dest="keyring_backend", default=None,
20
help="Name of the keyring backend")
23
opts, args = self.parser.parse_args(argv)
26
kind, service, username = args
29
# Be nice with the user if he just tries to launch the tool
30
self.parser.print_help()
33
self.parser.error("Wrong number of arguments")
35
if opts.keyring_backend is not None:
37
backend = keyring.core.load_keyring(opts.keyring_path,
39
keyring.set_keyring(backend)
41
# Tons of things can go wrong here:
42
# ImportError when using "fjkljfljkl"
43
# AttributeError when using "os.path.bar"
44
# TypeError when using "__builtins__.str"
45
# So, we play on the safe side, and catch everything.
46
self.parser.error("Unable to load specified keyring: %s" % e)
50
password = keyring.get_password(service, username)
54
self.output_password(password)
58
password = self.input_password("Password for '%s' in '%s': " %
60
keyring.set_password(service, username, password)
64
self.parser.error("You can only 'get' or 'set' a password.")
67
def input_password(self, prompt):
68
"""Ask for a password to the user.
70
This mostly exists to ease the testing process.
73
return getpass.getpass(prompt)
76
def output_password(self, password):
77
"""Output the password to the user.
79
This mostly exists to ease the testing process.
30
85
def main(argv=None):
31
86
"""Main command line interface."""
33
parser = optparse.OptionParser(usage="%prog [get|set] SERVICE USERNAME")
34
parser.add_option("-p", "--keyring-path", dest="keyring_path", default=None,
35
help="Path to the keyring backend")
36
parser.add_option("-b", "--keyring-backend", dest="keyring_backend", default=None,
37
help="Name of the keyring backend")
40
90
argv = sys.argv[1:]
42
opts, args = parser.parse_args(argv)
45
kind, service, username = args
48
# Be nice with the user if he just tries to launch the tool
52
parser.error("Wrong number of arguments")
54
if opts.keyring_backend is not None:
56
backend = keyring.core.load_keyring(opts.keyring_path, opts.keyring_backend)
57
keyring.set_keyring(backend)
59
# Tons of things can go wrong here:
60
# ImportError when using "fjkljfljkl"
61
# AttributeError when using "os.path.bar"
62
# TypeError when using "__builtins__.str"
63
# So, we play on the safe side, and catch everything.
64
parser.error("Unable to load specified keyring: %s" % e)
68
password = keyring.get_password(service, username)
72
output_password(password)
76
password = input_password("Password for '%s' in '%s': " %
78
keyring.set_password(service, username, password)
82
parser.error("You can only 'get' or 'set' a password.")
92
cli = CommandLineTool()
85
96
if __name__ == '__main__':