7 _moduleLogger = logging.getLogger("handle")
10 class TheOneRingHandle(telepathy.server.Handle):
12 Instances are memoized
15 def __init__(self, connection, id, handleType, name):
16 telepathy.server.Handle.__init__(self, id, handleType, name)
17 self._conn = weakref.proxy(connection)
20 return "<%s id=%u name='%s'>" % (
21 type(self).__name__, self.id, self.name
24 id = property(telepathy.server.Handle.get_id)
25 type = property(telepathy.server.Handle.get_type)
26 name = property(telepathy.server.Handle.get_name)
29 class ConnectionHandle(TheOneRingHandle):
31 def __init__(self, connection, id):
32 handleType = telepathy.HANDLE_TYPE_CONTACT
33 handleName = connection.username
34 TheOneRingHandle.__init__(self, connection, id, handleType, handleName)
36 self.profile = connection.username
39 def strip_number(prettynumber):
41 function to take a phone number and strip out all non-numeric
44 >>> strip_number("+012-(345)-678-90")
48 uglynumber = re.sub('\D', '', prettynumber)
52 class ContactHandle(TheOneRingHandle):
54 def __init__(self, connection, id, contactId, phoneNumber):
55 handleType = telepathy.HANDLE_TYPE_CONTACT
56 handleName = self.to_handle_name(contactId, phoneNumber)
57 TheOneRingHandle.__init__(self, connection, id, handleType, handleName)
59 self._contactId = contactId
60 self._phoneNumber = phoneNumber
63 def from_handle_name(handleName):
64 parts = handleName.split("#", 1)
66 contactId, contactNumber = parts[0:2]
68 contactId, contactNumber = "", handleName
70 raise RuntimeError("Invalid handle: %s" % handleName)
72 contactNumber = strip_number(contactNumber)
73 return contactId, contactNumber
76 def to_handle_name(contactId, contactNumber):
77 handleName = "#".join((contactId, strip_number(contactNumber)))
82 return self._contactId
85 def phoneNumber(self):
86 return self._phoneNumber
89 def contactDetails(self):
90 return self._conn.addressbook.get_contact_details(self._id)
93 class ListHandle(TheOneRingHandle):
95 def __init__(self, connection, id, listName):
96 handleType = telepathy.HANDLE_TYPE_LIST
98 TheOneRingHandle.__init__(self, connection, id, handleType, handleName)
101 _HANDLE_TYPE_MAPPING = {
102 'connection': ConnectionHandle,
103 'contact': ContactHandle,
108 def create_handle_factory():
110 cache = weakref.WeakValueDictionary()
112 def create_handle(connection, type, *args):
113 Handle = _HANDLE_TYPE_MAPPING[type]
114 key = Handle, connection.username, args
119 # The misnamed get_handle_id requests a new handle id
120 handle = Handle(connection, connection.get_handle_id(), *args)
123 connection._handles[handle.get_type(), handle.get_id()] = handle
124 handleStatus = "Is New!" if isNewHandle else "From Cache"
125 _moduleLogger.info("Created Handle: %r (%s)" % (handle, handleStatus))
131 create_handle = create_handle_factory()