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=None, source=None, props=None):
9 """ source is source service, such as LinkedIn """
10 self._attributes = {};
11 if name: self._set('fn', name)
12 self._multi_attributes = {}
16 self._set(key, props[key])
18 def __getitem__(self, key):
19 return self._safe_get(key)
21 def __unicode__(self):
22 return self.__repr__()
25 return "Friend %s" % self.get_name()
27 # public accessors -----------------
29 def add_url(self, url):
31 if not isinstance(url, basestring):
33 raise Exception('Not valid to add non-string URLs')
36 def add_phone(self, phone):
38 self._add('phone', phone)
40 def get_birthday_date(self):
41 return self._safe_get('bday')
43 def get_contact(self):
44 return self._safe_get('contact')
47 return self._safe_get('fn')
52 def set_source(self, source):
55 def get_nickname(self):
56 return self._safe_get("nickname")
59 try: return self._multi_attributes['url']
63 try: return self._multi_attributes['phone']
66 def get_photo_url(self):
67 return self._safe_get('photo-url')
69 def has_birthday_date(self):
70 return self._has('bday')
73 for a in self._attributes:
75 for a in self._multi_attributes:
79 def set_name(self, name):
82 def set_nickname(self, nickname):
83 self._set('nickname', nickname)
85 def set_birthday_date(self, date):
86 self._set('bday', date)
88 def set_contact(self, contact):
89 self._set('contact', contact)
91 def set_photo_url(self, url):
92 self._set('photo-url', url)
94 def update_from_friend(self, other_friend, overwrite=False):
96 Overwrites any attributes in this friend, with attributes from other_friend
99 self._attributes.update(other_friend._attributes)
101 for key in other_friend._multi_attributes.keys():
102 for value in other_friend._multi_attributes[key]:
103 self._add(key, value)
105 def update_contact(self, contact, overwrite=False):
106 """Updates the contact with information from this object,
107 without overwriting unless overwrite is set to True.
108 Returns flag indicating if anything *was* changed."""
110 def set_birthday(arg):
111 # Hackily assumes Facebook format (mm/d[/y])
112 date_str = arg.split('/')
114 return contact.set_birthday(int(date_str[1]),
119 if overwrite or contact.get_photo() is None: updated += self._if_defined('photo-url', contact.set_photo)
120 if overwrite or contact.get_nickname() is None: updated += self._if_defined('nickname', contact.set_nickname)
121 if overwrite or contact.get_birthday() is None: updated += self._if_defined('bday', set_birthday)
122 if self._multi_attributes.has_key('url'):
123 for url in self._multi_attributes['url']:
124 updated += contact.add_url(url)
126 if self._multi_attributes.has_key('phone'):
127 for phone in self._multi_attributes['phone']:
128 updated += contact.add_phone(phone)
133 # private helpers -----------------------
135 def _add(self, key, value):
136 if value is not None:
137 if not self._multi_attributes.has_key(key):
138 self._multi_attributes[key] = []
139 # print "%s ADD %s to %s" % (self, key, value)
140 self._multi_attributes[key].append(value)
142 def _contains(self, key, value):
143 if self._attributes.has_key(key):
144 return value == self._attributes[key]
145 if self._multi_attributes.has_key(key):
146 return value in self._multi_attributes[key]
149 def _if_defined(self, key, callback):
150 return self._attributes.has_key(key) and callback(self._attributes[key]) or False
153 return self._attributes.has_key(key) or self._multi_attributes.has_key(key)
155 def _safe_get(self, key):
156 try: return self._attributes[key]
159 def _set(self, key, value):
160 if value is not None:
161 print u"%s SET %s to %s" % (self, key, value)
162 self._attributes[key] = value
164 print u"%s not setting %s to undefined value" % (self, key)