2
from xml.dom import minidom
6
from baseaccountmanager import BaseAccountManager, CannotSaveError
8
class NotTextNodeError: pass
10
class XMLAccounts(BaseAccountManager):
13
self.file = os.path.expanduser("~/.config/cgmail/accounts.xml")
14
if not os.path.exists(self.file):
15
print "No xml config file found! Creating a default one..."
17
os.makedirs(os.path.expanduser("~/.config/cgmail"))
20
self.xmldoc = self.__create_default()
22
os.chmod(self.file, 0600)
26
self.xmldoc = minidom.parse(self.file)
28
self.xmldoc = self.__create_default()
31
self.__dic = self.__node_to_dic(self.xmldoc)
34
print "removing", self.file
38
print "Warnig: can't remove", self.file
40
def get_accounts_dicts(self):
41
if self.__dic["accounts"] == '':
44
ret = self.__dic["accounts"]["account"]
45
# we want to return always a list of dicts
47
if type(ret) == types.DictType:
54
print "Error in XMLAccounts.py at get_accounts_dict"
56
def remove_account(self, id):
58
remove the account identified by id
60
#print "removing account", id
62
nodes = self.xmldoc.firstChild
63
for account in nodes.childNodes:
64
if account.nodeType != account.ELEMENT_NODE:
66
for param in account.childNodes:
67
if param.nodeName == "id":
68
value = self.__get_text_from_node(param)
70
parent = param.parentNode # account
71
acc = parent.parentNode # accounts
72
acc.removeChild(parent)
78
self.__dic = self.__node_to_dic(self.xmldoc)
82
def add_account(self, dic, force_id = None):
84
Add an account to configuration. dic is a dict in form
85
of key, values for accounts parameters. This method
86
will add automagically an unique identifier to the new
93
dic["id"] = self.__get_next_id()
95
#print "adding account", dic
97
ac = self.xmldoc.createElement("account")
99
for key, value in dic.iteritems():
100
node = self.xmldoc.createElement(key)
101
nodevalue = self.xmldoc.createTextNode(value)
102
node.appendChild(nodevalue)
105
self.xmldoc.firstChild.appendChild(ac)
109
self.__dic = self.__node_to_dic(self.xmldoc)
113
def __get_next_id(self):
115
get an unique id for accounts
118
if self.__dic["accounts"] == '':
119
# there are no accounts
122
accounts = self.__dic["accounts"].values()[0]
123
if type(accounts) == types.DictType:
124
# there is only one account
125
return str(int(accounts["id"]) + 1)
127
# more than one account
129
for account in accounts:
130
if int(account["id"]) > max:
131
max = int(account["id"])
139
xfile = open(self.file, "w")
140
#self.xmldoc.writexml(xfile, '', '\t', '\n')
141
self.xmldoc.writexml(xfile, '', '', '')
144
raise CannotSaveError()
146
def __create_default(self):
148
Create the dafault config file if there isn't one
150
domImp = xml.dom.getDOMImplementation()
151
doc = domImp.createDocument("", "accounts", None)
154
def __get_text_from_node(self, node):
156
scans through all children of node and gathers the
157
text. if node has non-text child-nodes, then
158
NotTextNodeError is raised.
161
for n in node.childNodes:
162
if n.nodeType == n.TEXT_NODE:
165
raise NotTextNodeError
166
return t.strip("\n\t")
169
def __node_to_dic(self, node):
171
nodeToDic() scans through the children of node and makes a
172
dictionary from the content.
175
multlist = {} # holds temporary lists where there are multiple children
177
for n in node.childNodes:
178
if n.nodeType != n.ELEMENT_NODE:
181
# find out if there are multiple records
182
if len(node.getElementsByTagName(n.nodeName)) > 1:
184
# and set up the list to hold the values
185
if not multlist.has_key(n.nodeName):
186
multlist[n.nodeName] = []
190
text = self.__get_text_from_node(n)
191
except NotTextNodeError:
194
multlist[n.nodeName].append(self.__node_to_dic(n))
195
dic.update({n.nodeName:multlist[n.nodeName]})
199
dic.update({n.nodeName:self.__node_to_dic(n)})
204
multlist[n.nodeName].append(text)
205
dic.update({n.nodeName:multlist[n.nodeName]})
207
dic.update({n.nodeName:text})
211
if __name__ == "__main__":
213
x.add_account({"username": "redgun", "password": "test", "server": "pop.libero.it"})