8 """Provide an API for changing contact data. Abstracts limitations
9 in the evolution-python bindings.
11 Copyright (c) Andrew Flegg <andrew@bleb.org> 2009.
12 Released under the Artistic Licence."""
15 # -----------------------------------------------------------------------
16 def __init__(self, book):
17 """Create a new contact store for modifying contacts in the given
20 self.temp_file = os.tmpnam()
24 # -----------------------------------------------------------------------
26 """Close the store and tidy-up any resources."""
28 if (os.path.isfile(self.temp_file)):
29 os.unlink(self.temp_file)
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."""
38 urllib.urlretrieve(url, self.temp_file)
39 im = Image.open(self.temp_file)
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")
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)