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."""
100 #print "FIXME - birthday date needs to be parsed/fixed %s before calling contact.set_birthday" % arg
103 if overwrite or contact.get_photo() is None: updated += self._if_defined('photo-url', contact.set_photo)
104 if overwrite or contact.get_nickname() is None: updated += self._if_defined('nickname', contact.set_nickname)
105 if overwrite or contact.get_birthday() is None: updated += self._if_defined('bday', fixme)
106 if self._multi_attributes.has_key('url'):
107 for url in self._multi_attributes['url']:
108 updated += contact.add_url(url)
113 # private helpers -----------------------
115 def _add(self, key, value):
116 if value is not None:
117 if not self._multi_attributes.has_key(key):
118 self._multi_attributes[key] = []
119 # print "%s ADD %s to %s" % (self, key, value)
120 self._multi_attributes[key].append(value)
122 def _contains(self, key, value):
123 if self._attributes.has_key(key):
124 return value == self._attributes[key]
125 if self._multi_attributes.has_key(key):
126 return value in self._multi_attributes[key]
129 def _if_defined(self, key, callback):
130 return self._attributes.has_key(key) and callback(self._attributes[key]) or False
133 return self._attributes.has_key(key) or self._multi_attributes.has_key(key)
135 def _safe_get(self, key):
136 try: return self._attributes[key]
139 def _set(self, key, value):
140 if value is not None:
141 # print "%s SET %s to %s" % (self, key, value)
142 self._attributes[key] = value