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 add_phone(self, phone):
33 self._add('phone', phone)
35 def get_birthday_date(self):
36 return self._safe_get('bday')
38 def get_contact(self):
39 return self._safe_get('contact')
42 return self._safe_get('fn')
47 def set_source(self, source):
50 def get_nickname(self):
51 return self._safe_get("nickname")
54 try: return self._multi_attributes['url']
58 try: return self._multi_attributes['phone']
61 def get_photo_url(self):
62 return self._safe_get('photo-url')
64 def has_birthday_date(self):
65 return self._has('bday')
68 for a in self._attributes:
70 for a in self._multi_attributes:
74 def set_name(self, name):
77 def set_nickname(self, nickname):
78 self._set('nickname', nickname)
80 def set_birthday_date(self, date):
81 self._set('bday', date)
83 def set_contact(self, contact):
84 self._set('contact', contact)
86 def set_photo_url(self, url):
87 self._set('photo-url', url)
89 def update_from_friend(self, other_friend, overwrite=False):
91 Overwrites any attributes in this friend, with attributes from other_friend
94 self._attributes.update(other_friend._attributes)
96 for key in other_friend._multi_attributes.keys():
97 for value in other_friend._multi_attributes[key]:
100 def update_contact(self, contact, overwrite=False):
101 """Updates the contact with information from this object,
102 without overwriting unless overwrite is set to True.
103 Returns flag indicating if anything *was* changed."""
105 def set_birthday(arg):
106 # Hackily assumes Facebook format
107 date_str = arg.split('/')
109 return contact.set_birthday(int(date_str[1]),
114 if overwrite or contact.get_photo() is None: updated += self._if_defined('photo-url', contact.set_photo)
115 if overwrite or contact.get_nickname() is None: updated += self._if_defined('nickname', contact.set_nickname)
116 if overwrite or contact.get_birthday() is None: updated += self._if_defined('bday', set_birthday)
117 if self._multi_attributes.has_key('url'):
118 for url in self._multi_attributes['url']:
119 updated += contact.add_url(url)
121 if self._multi_attributes.has_key('phone'):
122 for url in self._multi_attributes['phone']:
123 updated += contact.add_phone(url)
128 # private helpers -----------------------
130 def _add(self, key, value):
131 if value is not None:
132 if not self._multi_attributes.has_key(key):
133 self._multi_attributes[key] = []
134 # print "%s ADD %s to %s" % (self, key, value)
135 self._multi_attributes[key].append(value)
137 def _contains(self, key, value):
138 if self._attributes.has_key(key):
139 return value == self._attributes[key]
140 if self._multi_attributes.has_key(key):
141 return value in self._multi_attributes[key]
144 def _if_defined(self, key, callback):
145 return self._attributes.has_key(key) and callback(self._attributes[key]) or False
148 return self._attributes.has_key(key) or self._multi_attributes.has_key(key)
150 def _safe_get(self, key):
151 try: return self._attributes[key]
154 def _set(self, key, value):
155 if value is not None:
156 # print "%s SET %s to %s" % (self, key, value)
157 self._attributes[key] = value