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):
9 """ source is source service, such as LinkedIn """
10 self._attributes = {};
11 if name: self._set('fn', name)
12 self._multi_attributes = {}
15 def __getitem__(self, key):
16 return self._safe_get(key)
18 def __unicode__(self):
19 return self.__repr__()
22 return "Friend %s" % self.get_name()
24 # public accessors -----------------
26 def add_url(self, url):
29 def get_birthday_date(self):
30 return self._safe_get('bday')
32 def get_contact(self):
33 return self._safe_get('contact')
36 return self._safe_get('fn')
41 def get_nickname(self):
42 return self._safe_get("nickname")
45 try: return self._multi_attributes['url']
48 def get_photo_url(self):
49 return self._safe_get('photo-url')
51 def has_birthday_date(self):
52 return self._has('bday')
55 for a in self._attributes:
57 for a in self._multi_attributes:
61 def set_name(self, name):
64 def set_nickname(self, nickname):
65 self._set('nickname', nickname)
67 def set_birthday_date(self, date):
68 self._set('bday', date)
70 def set_contact(self, contact):
71 self._set('contact', contact)
73 def set_photo_url(self, url):
74 self._set('photo-url', url)
76 def update_from_friend(self, other_friend, overwrite=False):
78 Overwrites any attributes in this friend, with attributes from other_friend
81 self._attributes.update(other_friend._attributes)
83 for key in other_friend._multi_attributes.keys():
84 for value in other_friend._multi_attributes[key]:
87 def update_contact(self, contact, overwrite=False):
89 Updates the contact with information from this object,
90 without overwriting unless overwrite is set to True.
93 # FIXME: currently we overwrite everything
94 self._if_defined('photo-url', contact.set_photo)
95 self._if_defined('nickname', contact.set_nickname)
96 if self._multi_attributes.has_key('url'):
97 for url in self._multi_attributes['url']:
102 #print "FIXME - birthday date needs to be parsed/fixed %s before calling contact.set_birthday" % arg
103 self._if_defined('bday', fixme)
105 # private helpers -----------------------
107 def _add(self, key, value):
108 if value is not None:
109 if not self._multi_attributes.has_key(key):
110 self._multi_attributes[key] = []
111 # print "%s ADD %s to %s" % (self, key, value)
112 self._multi_attributes[key].append(value)
114 def _contains(self, key, value):
115 if self._attributes.has_key(key):
116 return value == self._attributes[key]
117 if self._multi_attributes.has_key(key):
118 return value in self._multi_attributes[key]
121 def _if_defined(self, key, callback):
122 if self._attributes.has_key(key):
123 callback(self._attributes[key])
126 return self._attributes.has_key(key) or self._multi_attributes.has_key(key)
128 def _safe_get(self, key):
129 try: return self._attributes[key]
132 def _set(self, key, value):
133 if value is not None:
134 # print "%s SET %s to %s" % (self, key, value)
135 self._attributes[key] = value