2 """Encapsulate the data from a remote service.
4 Copyright (c) Fredrik Wendt <fredrik@wendt.se> 2010.
5 Released under the Artistic Licence."""
8 def __init__(self, name, source=None):
9 """ source is source service, such as LinkedIn """
10 self._attributes = { "fn": name }
11 self._multi_attributes = {}
14 def __unicode__(self):
15 return self.__repr__()
18 return "Friend %s" % self._attributes['fn']
20 # public accessors -----------------
23 return self._attributes['fn']
25 def get_nickname(self):
26 return self._attributes["nickname"]
29 try: return self._multi_attributes['url']
32 def get_photo_url(self):
33 try: return self._attributes['photo-url']
36 def add_url(self, url):
40 for a in self._attributes:
42 for a in self._multi_attributes:
46 def has_birthday_date(self):
47 return self._has('bday')
49 def set_name(self, name):
52 def set_nickname(self, nickname):
53 self._set('nickname', nickname)
55 def set_birthday_date(self, date):
56 self._set('bday', date)
58 def set_photo_url(self, url):
59 self._set('photo-url', url)
61 def update(self, other_friend):
63 Overwrites any attributes in this friend, with attributes from other_friend
66 self._attributes.update(other_friend._attributes)
68 for key in other_friend._multi_attributes.keys():
69 for value in other_friend._multi_attributes[key]:
72 def update_contact(self, contact, overwrite=False):
74 Updates the contact with information from this object,
75 without overwriting unless overwrite is set to True.
78 # FIXME: currently we overwrite everything
79 self._if_defined('photo-url', contact.set_photo)
80 self._if_defined('nickname', contact.set_nickname)
81 if self._multi_attributes.has_key('url'):
82 for url in self._multi_attributes['url']:
87 #print "FIXME - birthday date needs to be parsed/fixed %s before calling contact.set_birthday" % arg
88 self._if_defined('bday', fixme)
90 # private helpers -----------------------
92 def _if_defined(self, key, callback):
93 if self._attributes.has_key(key):
94 callback(self._attributes[key])
96 def _set(self, key, value):
98 # print "%s SET %s to %s" % (self, key, value)
99 self._attributes[key] = value
101 def _add(self, key, value):
102 if value is not None:
103 if not self._multi_attributes.has_key(key):
104 self._multi_attributes[key] = []
105 # print "%s ADD %s to %s" % (self, key, value)
106 self._multi_attributes[key].append(value)
109 return self._attributes.has_key(key) or self._multi_attributes.has_key(key)
111 def _contains(self, key, value):
112 if self._attributes.has_key(key):
113 return value == self._attributes[key]
114 if self._multi_attributes.has_key(key):
115 return value in self._multi_attributes[key]