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):
32 def get_birthday_date(self):
33 return self._safe_get('bday')
35 def get_contact(self):
36 return self._safe_get('contact')
39 return self._safe_get('fn')
44 def set_source(self, source):
47 def get_nickname(self):
48 return self._safe_get("nickname")
51 try: return self._multi_attributes['url']
54 def get_photo_url(self):
55 return self._safe_get('photo-url')
57 def has_birthday_date(self):
58 return self._has('bday')
61 for a in self._attributes:
63 for a in self._multi_attributes:
67 def set_name(self, name):
70 def set_nickname(self, nickname):
71 self._set('nickname', nickname)
73 def set_birthday_date(self, date):
74 self._set('bday', date)
76 def set_contact(self, contact):
77 self._set('contact', contact)
79 def set_photo_url(self, url):
80 self._set('photo-url', url)
82 def update_from_friend(self, other_friend, overwrite=False):
84 Overwrites any attributes in this friend, with attributes from other_friend
87 self._attributes.update(other_friend._attributes)
89 for key in other_friend._multi_attributes.keys():
90 for value in other_friend._multi_attributes[key]:
93 def update_contact(self, contact, overwrite=False):
94 """Updates the contact with information from this object,
95 without overwriting unless overwrite is set to True.
96 Returns flag indicating if anything *was* changed."""
98 def set_birthday(arg):
99 # Hackily assumes Facebook format
100 date_str = arg.split('/')
102 return contact.set_birthday(int(date_str[1]),
107 if overwrite or contact.get_photo() is None: updated += self._if_defined('photo-url', contact.set_photo)
108 if overwrite or contact.get_nickname() is None: updated += self._if_defined('nickname', contact.set_nickname)
109 if overwrite or contact.get_birthday() is None: updated += self._if_defined('bday', set_birthday)
110 if self._multi_attributes.has_key('url'):
111 for url in self._multi_attributes['url']:
112 updated += contact.add_url(url)
117 # private helpers -----------------------
119 def _add(self, key, value):
120 if value is not None:
121 if not self._multi_attributes.has_key(key):
122 self._multi_attributes[key] = []
123 # print "%s ADD %s to %s" % (self, key, value)
124 self._multi_attributes[key].append(value)
126 def _contains(self, key, value):
127 if self._attributes.has_key(key):
128 return value == self._attributes[key]
129 if self._multi_attributes.has_key(key):
130 return value in self._multi_attributes[key]
133 def _if_defined(self, key, callback):
134 return self._attributes.has_key(key) and callback(self._attributes[key]) or False
137 return self._attributes.has_key(key) or self._multi_attributes.has_key(key)
139 def _safe_get(self, key):
140 try: return self._attributes[key]
143 def _set(self, key, value):
144 if value is not None:
145 # print "%s SET %s to %s" % (self, key, value)
146 self._attributes[key] = value