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 __unicode__(self):
16 return self.__repr__()
19 return "Friend %s" % self.get_name()
21 # public accessors -----------------
23 def get_contact(self):
24 return self._safe_get('contact')
27 return self._safe_get('fn')
32 def get_nickname(self):
33 return self._safe_get("nickname")
36 try: return self._multi_attributes['url']
39 def get_photo_url(self):
40 return self._safe_get('photo-url')
42 def add_url(self, url):
46 for a in self._attributes:
48 for a in self._multi_attributes:
52 def has_birthday_date(self):
53 return self._has('bday')
55 def set_name(self, name):
58 def set_nickname(self, nickname):
59 self._set('nickname', nickname)
61 def set_birthday_date(self, date):
62 self._set('bday', date)
64 def set_contact(self, contact):
65 self._set('contact', contact)
67 def set_photo_url(self, url):
68 self._set('photo-url', url)
70 def update(self, other_friend):
72 Overwrites any attributes in this friend, with attributes from other_friend
75 self._attributes.update(other_friend._attributes)
77 for key in other_friend._multi_attributes.keys():
78 for value in other_friend._multi_attributes[key]:
81 def update_contact(self, contact, overwrite=False):
83 Updates the contact with information from this object,
84 without overwriting unless overwrite is set to True.
87 # FIXME: currently we overwrite everything
88 self._if_defined('photo-url', contact.set_photo)
89 self._if_defined('nickname', contact.set_nickname)
90 if self._multi_attributes.has_key('url'):
91 for url in self._multi_attributes['url']:
96 #print "FIXME - birthday date needs to be parsed/fixed %s before calling contact.set_birthday" % arg
97 self._if_defined('bday', fixme)
99 # private helpers -----------------------
101 def _if_defined(self, key, callback):
102 if self._attributes.has_key(key):
103 callback(self._attributes[key])
105 def _set(self, key, value):
106 if value is not None:
107 # print "%s SET %s to %s" % (self, key, value)
108 self._attributes[key] = value
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)
118 return self._attributes.has_key(key) or self._multi_attributes.has_key(key)
120 def _contains(self, key, value):
121 if self._attributes.has_key(key):
122 return value == self._attributes[key]
123 if self._multi_attributes.has_key(key):
124 return value in self._multi_attributes[key]
127 def __getitem__(self, key):
128 return self._safe_get(key)
130 def _safe_get(self, key):
131 try: return self._attributes[key]