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']
29 def get_nickname(self):
30 return self._attributes["nickname"]
33 try: return self._multi_attributes['url']
36 def get_photo_url(self):
37 try: return self._attributes['photo-url']
40 def add_url(self, url):
44 for a in self._attributes:
46 for a in self._multi_attributes:
50 def has_birthday_date(self):
51 return self._has('bday')
53 def set_name(self, name):
56 def set_nickname(self, nickname):
57 self._set('nickname', nickname)
59 def set_birthday_date(self, date):
60 self._set('bday', date)
62 def set_contact(self, contact):
63 self._set('contact', contact)
65 def set_photo_url(self, url):
66 self._set('photo-url', url)
68 def update(self, other_friend):
70 Overwrites any attributes in this friend, with attributes from other_friend
73 self._attributes.update(other_friend._attributes)
75 for key in other_friend._multi_attributes.keys():
76 for value in other_friend._multi_attributes[key]:
79 def update_contact(self, contact, overwrite=False):
81 Updates the contact with information from this object,
82 without overwriting unless overwrite is set to True.
85 # FIXME: currently we overwrite everything
86 self._if_defined('photo-url', contact.set_photo)
87 self._if_defined('nickname', contact.set_nickname)
88 if self._multi_attributes.has_key('url'):
89 for url in self._multi_attributes['url']:
94 #print "FIXME - birthday date needs to be parsed/fixed %s before calling contact.set_birthday" % arg
95 self._if_defined('bday', fixme)
97 # private helpers -----------------------
99 def _if_defined(self, key, callback):
100 if self._attributes.has_key(key):
101 callback(self._attributes[key])
103 def _set(self, key, value):
104 if value is not None:
105 # print "%s SET %s to %s" % (self, key, value)
106 self._attributes[key] = value
108 def _add(self, key, value):
109 if value is not None:
110 if not self._multi_attributes.has_key(key):
111 self._multi_attributes[key] = []
112 # print "%s ADD %s to %s" % (self, key, value)
113 self._multi_attributes[key].append(value)
116 return self._attributes.has_key(key) or self._multi_attributes.has_key(key)
118 def _contains(self, key, value):
119 if self._attributes.has_key(key):
120 return value == self._attributes[key]
121 if self._multi_attributes.has_key(key):
122 return value in self._multi_attributes[key]
125 def __getitem__(self, key):
126 return self._attributes[key]