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._attributes['fn']
21 # public accessors -----------------
23 def get_contact(self):
24 return self._attributes['contact']
27 return self._attributes['fn']
32 def get_nickname(self):
33 return self._attributes["nickname"]
36 try: return self._multi_attributes['url']
39 def get_photo_url(self):
40 try: return self._attributes['photo-url']
43 def add_url(self, url):
47 for a in self._attributes:
49 for a in self._multi_attributes:
53 def has_birthday_date(self):
54 return self._has('bday')
56 def set_name(self, name):
59 def set_nickname(self, nickname):
60 self._set('nickname', nickname)
62 def set_birthday_date(self, date):
63 self._set('bday', date)
65 def set_contact(self, contact):
66 self._set('contact', contact)
68 def set_photo_url(self, url):
69 self._set('photo-url', url)
71 def update(self, other_friend):
73 Overwrites any attributes in this friend, with attributes from other_friend
76 self._attributes.update(other_friend._attributes)
78 for key in other_friend._multi_attributes.keys():
79 for value in other_friend._multi_attributes[key]:
82 def update_contact(self, contact, overwrite=False):
84 Updates the contact with information from this object,
85 without overwriting unless overwrite is set to True.
88 # FIXME: currently we overwrite everything
89 self._if_defined('photo-url', contact.set_photo)
90 self._if_defined('nickname', contact.set_nickname)
91 if self._multi_attributes.has_key('url'):
92 for url in self._multi_attributes['url']:
97 #print "FIXME - birthday date needs to be parsed/fixed %s before calling contact.set_birthday" % arg
98 self._if_defined('bday', fixme)
100 # private helpers -----------------------
102 def _if_defined(self, key, callback):
103 if self._attributes.has_key(key):
104 callback(self._attributes[key])
106 def _set(self, key, value):
107 if value is not None:
108 # print "%s SET %s to %s" % (self, key, value)
109 self._attributes[key] = value
111 def _add(self, key, value):
112 if value is not None:
113 if not self._multi_attributes.has_key(key):
114 self._multi_attributes[key] = []
115 # print "%s ADD %s to %s" % (self, key, value)
116 self._multi_attributes[key].append(value)
119 return self._attributes.has_key(key) or self._multi_attributes.has_key(key)
121 def _contains(self, key, value):
122 if self._attributes.has_key(key):
123 return value == self._attributes[key]
124 if self._multi_attributes.has_key(key):
125 return value in self._multi_attributes[key]
128 def __getitem__(self, key):
129 return self._attributes[key]