7 class MetaMemoize(type):
9 Allows a class to cache off instances for reuse
12 def __call__(cls, connection, *args):
13 obj, newlyCreated = cls.__new__(cls, connection, *args)
15 obj.__init__(connection, connection.get_handle_id(), *args)
16 logging.info("New Handle %r" % obj)
20 class TheOneRingHandle(telepathy.server.Handle):
22 Instances are memoized
25 __metaclass__ = MetaMemoize
27 _instances = weakref.WeakValueDictionary()
29 def __new__(cls, connection, *args):
30 key = cls, connection.username, args
31 if key in cls._instances.keys():
32 return cls._instances[key], False
34 instance = object.__new__(cls, connection, *args)
35 cls._instances[key] = instance # TRICKY: instances is a weakdict
38 def __init__(self, connection, id, handleType, name):
39 telepathy.server.Handle.__init__(self, id, handleType, name)
40 self._conn = weakref.proxy(connection)
43 return "<%s id=%u name='%s'>" % (
44 type(self).__name__, self.id, self.name
47 id = property(telepathy.server.Handle.get_id)
48 type = property(telepathy.server.Handle.get_type)
49 name = property(telepathy.server.Handle.get_name)
52 class SelfHandle(TheOneRingHandle):
56 def __init__(self, connection, id):
57 handleType = telepathy.HANDLE_TYPE_CONTACT
58 handleName = connection.username
59 TheOneRingHandle.__init__(self, connection, id, handleType, handleName)
61 self.profile = connection.username
64 def field_join(fields):
66 >>> field_join("1", "First Name")
69 return "#".join(fields)
72 def field_split(fields):
74 >>> field_split('1#First Name')
77 return fields.split("#")
80 class ContactHandle(TheOneRingHandle):
82 def __init__(self, connection, id, contactId, contactAccount):
83 handleType = telepathy.HANDLE_TYPE_CONTACT
84 handleName = field_join(contactId, contactAccount)
85 TheOneRingHandle.__init__(self, connection, id, handleType, handleName)
87 self.account = contactAccount
92 return self._conn.gvoice_client.get_contact_details(self._id)
95 class ListHandle(TheOneRingHandle):
97 def __init__(self, connection, id, listName):
98 handleType = telepathy.HANDLE_TYPE_LIST
100 TheOneRingHandle.__init__(self, connection, id, handleType, handleName)
103 class GroupHandle(TheOneRingHandle):
105 def __init__(self, connection, id, groupName):
106 handleType = telepathy.HANDLE_TYPE_GROUP
107 handleName = groupName
108 TheOneRingHandle.__init__(self, connection, id, handleType, handleName)
111 _HANDLE_TYPE_MAPPING = {
113 'contact': ContactHandle,
115 'group': GroupHandle,
119 def create_handle(connection, type, *args):
120 handle = _HANDLE_TYPE_MAPPING[type](connection, *args)
121 connection._handles[handle.get_type(), handle.get_id()] = handle