Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1"""
2@file
3@brief Helpers around credentials.
4"""
5from pyquickhelper.loghelper import get_password as _getp
6from pyquickhelper.loghelper import set_password as _setp
9def set_password(k1, k2, value):
10 """
11 Stores a password with `keyring
12 <http://pythonhosted.org/keyring/>`_.
14 @param k1 first key
15 @param k2 second key
16 @param value value
18 .. faqref::
19 :title: How to avoid entering the credentials everytime?
21 Using clear credentials in a program file or in a notebook
22 is dangerous. You should do that. However, it is annoying
23 to type his password every time it is required.
24 The solution is to store it with `keyring
25 <http://pythonhosted.org/keyring/>`_.
26 You need to execute only once:
28 ::
30 from pyquickhelper.loghelper import set_password
31 set_password("k1", "k2", "value")
33 And you can retrieve the password anytime
34 not necessarily in the same program:
36 ::
38 from pyquickhelper.loghelper import set_password
39 set_password("k1", "k2", "value")
40 """
41 if not isinstance(k1, str):
42 raise TypeError("k1 must be a string.")
43 if not isinstance(k2, str):
44 raise TypeError("k1 must be a string.")
45 if not isinstance(value, str):
46 raise TypeError("value must be a string.")
47 if len(value.strip()) == 0:
48 raise ValueError("value is empty")
49 _setp(k1, k2, value)
52def get_password(k1, k2):
53 """
54 Retrieves a password with `keyring
55 <http://pythonhosted.org/keyring/>`_.
57 @param k1 first key
58 @param k2 second key
59 @return value
60 """
61 if not isinstance(k1, str):
62 raise TypeError("k1 must be a string.")
63 if not isinstance(k2, str):
64 raise TypeError("k1 must be a string.")
66 value = _getp(k1, k2)
67 if value is None:
68 raise ValueError(
69 "No secret value stored for k1={0} k2={1}".format(k1, k2))
70 return value