|
From: Stas Z <sta...@gm...> - 2005-06-03 15:32:36
|
I was hacking gmailagent to add a 'save contacts when something goes
wrong' function.
It occurs to me that libgmail2 does very little to save the contacts
in case or errors.
I just paste the stuff I come up with for gmailagent.
(It can be found in Gmail.py)
Stas
def save_contacts(contacts):
"""Save the contact in case of an error. This is an attempt to prevent
data loss in case of an error when interacting with Gmail contacts."""
try:
f =3D open(os.path.expanduser('~/.GmailContacts.pickle'),'w')
pickle.dump(contacts,f)
## XXX TODO: some way to load the pickled object and restore it
except Exception,info:
text =3D "Attempt to pickle the contacts %s" % str(info)
logging.critical(text)
f.close()
try:
f =3Dopen(os.path.expanduser('~/.GmailContacts.txt'),'w')
text =3D "#An error occured in GmailAgent and these contacts are
saved\n"+\
"#to this file in an attempt to save them\n"+\
"#A contact is saved as follows:\n"+\
"#GmailID Name email notes\n"
f.write(text)
for con in contacts:
name,email,notes,id =3D
con.getName(),con.getEmail(),con.getNotes(),con.getId()
f.write("%s %s %s %s\n" % (con.getId(),\
con.getName(),\
con.getEmail(),\
con.getNotes()))
f.close()
except Exception,info:
logging.critical(str(info))
return str(info)
=20
--=20
"Everything that is really great and inspiring is created by the individual
who can labor in freedom."
-Albert Einstein
|