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, props=None):
9 """ source is source service, such as LinkedIn """
10 self._attributes = {};
11 if name: self._set('fn', name)
12 self._multi_attributes = {}
15 self._set(key, props[key])
17 def __getitem__(self, key):
18 return self._safe_get(key)
20 def __unicode__(self):
21 return self.__repr__()
24 return "Friend %s" % self.get_name()
26 # public accessors -----------------
28 def add_url(self, url):
31 def get_birthday_date(self):
32 return self._safe_get('bday')
34 def get_contact(self):
35 return self._safe_get('contact')
38 return self._safe_get('fn')
43 def get_nickname(self):
44 return self._safe_get("nickname")
47 try: return self._multi_attributes['url']
50 def get_photo_url(self):
51 return self._safe_get('photo-url')
53 def has_birthday_date(self):
54 return self._has('bday')
57 for a in self._attributes:
59 for a in self._multi_attributes:
63 def set_name(self, name):
66 def set_nickname(self, nickname):
67 self._set('nickname', nickname)
69 def set_birthday_date(self, date):
70 self._set('bday', date)
72 def set_contact(self, contact):
73 self._set('contact', contact)
75 def set_photo_url(self, url):
76 self._set('photo-url', url)
78 def update_friend(self, other_friend, overwrite=False):
80 Overwrites any attributes in this friend, with attributes from other_friend
83 self._attributes.update(other_friend._attributes)
85 for key in other_friend._multi_attributes.keys():
86 for value in other_friend._multi_attributes[key]:
89 def update_contact(self, contact, overwrite=False):
91 Updates the contact with information from this object,
92 without overwriting unless overwrite is set to True.
95 # FIXME: currently we overwrite everything
96 self._if_defined('photo-url', contact.set_photo)
97 self._if_defined('nickname', contact.set_nickname)
98 if self._multi_attributes.has_key('url'):
99 for url in self._multi_attributes['url']:
104 #print "FIXME - birthday date needs to be parsed/fixed %s before calling contact.set_birthday" % arg
105 self._if_defined('bday', fixme)
107 # private helpers -----------------------
109 def _add(self, key, value):
110 if value is not None:
111 if not self._multi_attributes.has_key(key):
112 self._multi_attributes[key] = []
113 # print "%s ADD %s to %s" % (self, key, value)
114 self._multi_attributes[key].append(value)
116 def _contains(self, key, value):
117 if self._attributes.has_key(key):
118 return value == self._attributes[key]
119 if self._multi_attributes.has_key(key):
120 return value in self._multi_attributes[key]
123 def _if_defined(self, key, callback):
124 if self._attributes.has_key(key):
125 callback(self._attributes[key])
128 return self._attributes.has_key(key) or self._multi_attributes.has_key(key)
130 def _safe_get(self, key):
131 try: return self._attributes[key]
134 def _set(self, key, value):
135 if value is not None:
136 # print "%s SET %s to %s" % (self, key, value)
137 self._attributes[key] = value