/
account.py
122 lines (104 loc) · 3.16 KB
/
account.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#!/usr/bin/env python
import gnomekeyring as gk
import glib
class Account():
NAME = "login"
ITEM = "gedit-pastebin-plugin"
TYPE = gk.ITEM_GENERIC_SECRET
def __init__(self):
if not gk.is_available():
raise AccountError("Gnome Keyring Unavailable")
else:
errors = []
errors.append(gk.DeniedError)
errors.append(gk.NoKeyringDaemonError)
errors.append(gk.AlreadyUnlockedError)
errors.append(gk.NoSuchKeyringError)
errors.append(gk.BadArgumentsError)
errors.append(gk.IOError)
errors.append(gk.CancelledError)
errors.append(gk.AlreadyExistsError)
errors.append(gk.NoMatchError)
self.GK_EXCEPT = tuple(errors)
def delete_details(self):
""" Delete account details """
try:
item = self.__get_item()
if item is not None:
gk.item_delete_sync(self.NAME, item)
return True
return False
except self.GK_EXCEPT as e:
raise AccountError(repr(e))
def exists(self):
""" Whether account details are stored """
try:
if self.__get_item() is not None:
return True
return False
except self.GK_EXCEPT as e:
raise AccountError(repr(e))
def get_details(self):
""" Get the account details """
try:
item = self.__get_item()
if item is not None:
username = self.__get_usr(item)
password = self.__get_pwd(item)
return (username, password)
else:
raise AccountError("Keyring item doesn't exist")
except self.GK_EXCEPT as e:
raise AccountError(repr(e))
def set_details(self, usr, pwd):
""" Set the account details with a username and password """
try:
item = self.__get_item()
if item is not None:
self.__set_usr(usr, item)
self.__set_pwd(pwd, item)
else:
attrs = {"username" : str(usr)}
gk.item_create_sync(self.NAME, self.TYPE,
self.ITEM, attrs, pwd, True)
except self.GK_EXCEPT as e:
raise AccountError(repr(e))
def __get_usr(self, item):
""" Get the username from the account details """
attrs = gk.item_get_attributes_sync(self.NAME, item)
if "username" in attrs:
return attrs.get("username")
else:
raise AccountError("Username could not be retrieved")
def __set_usr(self, usr, item):
""" Set the username of the account details """
attrs = { "username" : str(usr)}
gk.item_set_attributes_sync(self.NAME, item, attrs)
def __get_pwd(self, item):
""" Get the password from the account details """
info = gk.item_get_info_sync(self.NAME, item)
return info.get_secret()
def __set_pwd(self, pwd, item):
""" Set the password of the account details """
info = gk.item_get_info_sync(self.NAME, item)
info.set_secret(pwd)
gk.item_set_info_sync(self.NAME, item, info)
def __get_item(self):
""" Get the keyring item """
keyrings = gk.list_keyring_names_sync()
if self.NAME in keyrings:
items = gk.list_item_ids_sync(self.NAME)
for item in items:
info = gk.item_get_info_sync(self.NAME, item)
if self.ITEM == info.get_display_name():
return item
return None
class AccountError(Exception):
def __init__(self, msg):
self.msg = msg
def __str__(self):
return "AccountError Details: " + self.msg
def get_type(self):
return "AccountError Details"
def get_msg(self):
return self.msg