forked from vintitres/gnome-shell-google-calendar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
keyring.py
66 lines (48 loc) · 1.7 KB
/
keyring.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
"""
Keyring storage of Google Calendar login credentials
"""
import gnomekeyring as gk
_keyring = 'login'
_display_name = 'GNOME Shell Google Calendar Account'
_attrs = {'application': 'gnome-shell-google-calendar'}
_type = gk.ITEM_GENERIC_SECRET
_item_id = None
class KeyringError(Exception):
pass
def get_item_id():
global _item_id
if _item_id:
return _item_id
try:
results = gk.find_items_sync(gk.ITEM_GENERIC_SECRET, _attrs)
_item_id = results[0].item_id
except gk.NoMatchError:
# Check if ''_keyring'' has been created.
if _keyring not in gk.list_keyring_names_sync():
# if it is missing then create a new keyring called ''_keyring''
gk.create_sync(_keyring, _keyring)
# Create item
_item_id = gk.item_create_sync(_keyring, _type,
_display_name, _attrs, '', True)
return _item_id
def get_credentials():
item_id = get_item_id()
attrs = gk.item_get_attributes_sync(_keyring, item_id)
if not 'email' in attrs:
raise KeyringError('Login credentials not found')
info = gk.item_get_info_sync(_keyring, item_id)
return attrs['email'], info.get_secret()
def set_credentials(email, password):
item_id = get_item_id()
info = gk.ItemInfo()
info.set_display_name(_display_name)
info.set_type(_type)
info.set_secret(password)
gk.item_set_info_sync(_keyring, item_id, info)
attrs = _attrs.copy()
attrs['email'] = email
gk.item_set_attributes_sync(_keyring, item_id, attrs)
if __name__ == '__main__':
print 'item_id', get_item_id()
#set_credentials('[email protected]', 'secret')
print 'credentials', get_credentials()