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):
30 if not isinstance(url, basestring):
32 raise Exception('Not valid to add non-string URLs')
35 def add_phone(self, phone):
36 self._add('phone', phone)
38 def get_birthday_date(self):
39 return self._safe_get('bday')
41 def get_contact(self):
42 return self._safe_get('contact')
45 return self._safe_get('fn')
50 def set_source(self, source):
53 def get_nickname(self):
54 return self._safe_get("nickname")
57 try: return self._multi_attributes['url']
61 try: return self._multi_attributes['phone']
64 def get_photo_url(self):
65 return self._safe_get('photo-url')
67 def has_birthday_date(self):
68 return self._has('bday')
71 for a in self._attributes:
73 for a in self._multi_attributes:
77 def set_name(self, name):
80 def set_nickname(self, nickname):
81 self._set('nickname', nickname)
83 def set_birthday_date(self, date):
84 self._set('bday', date)
86 def set_contact(self, contact):
87 self._set('contact', contact)
89 def set_photo_url(self, url):
90 self._set('photo-url', url)
92 def update_from_friend(self, other_friend, overwrite=False):
94 Overwrites any attributes in this friend, with attributes from other_friend
97 self._attributes.update(other_friend._attributes)
99 for key in other_friend._multi_attributes.keys():
100 for value in other_friend._multi_attributes[key]:
101 self._add(key, value)
103 def update_contact(self, contact, overwrite=False):
104 """Updates the contact with information from this object,
105 without overwriting unless overwrite is set to True.
106 Returns flag indicating if anything *was* changed."""
108 def set_birthday(arg):
109 # Hackily assumes Facebook format (mm/d[/y])
110 date_str = arg.split('/')
112 return contact.set_birthday(int(date_str[1]),
117 if overwrite or contact.get_photo() is None: updated += self._if_defined('photo-url', contact.set_photo)
118 if overwrite or contact.get_nickname() is None: updated += self._if_defined('nickname', contact.set_nickname)
119 if overwrite or contact.get_birthday() is None: updated += self._if_defined('bday', set_birthday)
120 if self._multi_attributes.has_key('url'):
121 for url in self._multi_attributes['url']:
122 updated += contact.add_url(url)
124 if self._multi_attributes.has_key('phone'):
125 for phone in self._multi_attributes['phone']:
126 updated += contact.add_phone(phone)
131 # private helpers -----------------------
133 def _add(self, key, value):
134 if value is not None:
135 if not self._multi_attributes.has_key(key):
136 self._multi_attributes[key] = []
137 # print "%s ADD %s to %s" % (self, key, value)
138 self._multi_attributes[key].append(value)
140 def _contains(self, key, value):
141 if self._attributes.has_key(key):
142 return value == self._attributes[key]
143 if self._multi_attributes.has_key(key):
144 return value in self._multi_attributes[key]
147 def _if_defined(self, key, callback):
148 return self._attributes.has_key(key) and callback(self._attributes[key]) or False
151 return self._attributes.has_key(key) or self._multi_attributes.has_key(key)
153 def _safe_get(self, key):
154 try: return self._attributes[key]
157 def _set(self, key, value):
158 if value is not None:
159 # print "%s SET %s to %s" % (self, key, value)
160 self._attributes[key] = value