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 get_nickname(self):
45 return self._safe_get("nickname")
48 try: return self._multi_attributes['url']
51 def get_photo_url(self):
52 return self._safe_get('photo-url')
54 def has_birthday_date(self):
55 return self._has('bday')
58 for a in self._attributes:
60 for a in self._multi_attributes:
64 def set_name(self, name):
67 def set_nickname(self, nickname):
68 self._set('nickname', nickname)
70 def set_birthday_date(self, date):
71 self._set('bday', date)
73 def set_contact(self, contact):
74 self._set('contact', contact)
76 def set_photo_url(self, url):
77 self._set('photo-url', url)
79 def update_from_friend(self, other_friend, overwrite=False):
81 Overwrites any attributes in this friend, with attributes from other_friend
84 self._attributes.update(other_friend._attributes)
86 for key in other_friend._multi_attributes.keys():
87 for value in other_friend._multi_attributes[key]:
90 def update_contact(self, contact, overwrite=False):
92 Updates the contact with information from this object,
93 without overwriting unless overwrite is set to True.
96 # FIXME: currently we overwrite everything
97 self._if_defined('photo-url', contact.set_photo)
98 self._if_defined('nickname', contact.set_nickname)
99 if self._multi_attributes.has_key('url'):
100 for url in self._multi_attributes['url']:
105 #print "FIXME - birthday date needs to be parsed/fixed %s before calling contact.set_birthday" % arg
106 self._if_defined('bday', fixme)
108 # private helpers -----------------------
110 def _add(self, key, value):
111 if value is not None:
112 if not self._multi_attributes.has_key(key):
113 self._multi_attributes[key] = []
114 # print "%s ADD %s to %s" % (self, key, value)
115 self._multi_attributes[key].append(value)
117 def _contains(self, key, value):
118 if self._attributes.has_key(key):
119 return value == self._attributes[key]
120 if self._multi_attributes.has_key(key):
121 return value in self._multi_attributes[key]
124 def _if_defined(self, key, callback):
125 if self._attributes.has_key(key):
126 callback(self._attributes[key])
129 return self._attributes.has_key(key) or self._multi_attributes.has_key(key)
131 def _safe_get(self, key):
132 try: return self._attributes[key]
135 def _set(self, key, value):
136 if value is not None:
137 # print "%s SET %s to %s" % (self, key, value)
138 self._attributes[key] = value