Add v0.0.1 of Hermes from source tarball
[hermes] / package / src / contacts.py
1 import os
2 import os.path
3 import urllib
4 import Image
5 import ImageOps
6
7 class ContactStore:
8   """Provide an API for changing contact data. Abstracts limitations
9      in the evolution-python bindings.
10
11      Copyright (c) Andrew Flegg <andrew@bleb.org> 2009.
12      Released under the Artistic Licence."""
13
14
15   # -----------------------------------------------------------------------
16   def __init__(self, book):
17     """Create a new contact store for modifying contacts in the given
18        EBook."""
19
20     self.temp_file = os.tmpnam()
21     self.book = book
22
23  
24   # -----------------------------------------------------------------------
25   def close(self):
26     """Close the store and tidy-up any resources."""
27
28     if (os.path.isfile(self.temp_file)):
29       os.unlink(self.temp_file)
30
31
32   # -----------------------------------------------------------------------
33   def set_photo(self, contact, url):
34     """Set the given contact's photo to the picture found at the URL. If the
35        photo is wider than it is tall, it will be cropped with a bias towards
36        the top of the photo."""
37
38     urllib.urlretrieve(url, self.temp_file)
39     im = Image.open(self.temp_file)
40     (w, h) = im.size
41     if (h > w):
42       print "Shrinking photo for %s as it's %d x %d" % (contact.get_name(), w, h)
43       im = ImageOps.fit(im, (w, w), Image.NEAREST, 0, (0, 0.1))
44       im.save(self.temp_file, "JPEG")
45       
46     print "Updating photo for %s" % (contact.get_name())
47     os.spawnl(os.P_WAIT, '/opt/hermes/bin/contact-update', 'contact-update', contact.get_name(), '--photo', 'image/jpeg', self.temp_file)