Imitiating buttfly in being explicitly typed
[theonering] / src / handle.py
1 import logging
2 import weakref
3
4 import telepathy
5
6 import tp
7 import util.misc as misc_utils
8
9
10 _moduleLogger = logging.getLogger(__name__)
11
12
13 class TheOneRingHandle(tp.Handle):
14         """
15         Instances are memoized
16         """
17
18         def __init__(self, connection, id, handleType, name):
19                 tp.Handle.__init__(self, id, handleType, name)
20                 self._conn = weakref.proxy(connection)
21
22         def __repr__(self):
23                 return "<%s id=%u name='%s'>" % (
24                         type(self).__name__, self.id, self.name
25                 )
26
27         id = property(tp.Handle.get_id)
28         type = property(tp.Handle.get_type)
29         name = property(tp.Handle.get_name)
30
31
32 class ConnectionHandle(TheOneRingHandle):
33
34         def __init__(self, connection, id):
35                 handleType = telepathy.HANDLE_TYPE_CONTACT
36                 handleName = connection.username
37                 TheOneRingHandle.__init__(self, connection, id, handleType, handleName)
38
39                 self.profile = connection.username
40
41
42 class ContactHandle(TheOneRingHandle):
43
44         def __init__(self, connection, id, phoneNumber):
45                 self._phoneNumber = misc_utils.normalize_number(phoneNumber)
46
47                 handleType = telepathy.HANDLE_TYPE_CONTACT
48                 handleName = self._phoneNumber
49                 TheOneRingHandle.__init__(self, connection, id, handleType, handleName)
50
51         @property
52         def phoneNumber(self):
53                 return self._phoneNumber
54
55
56 class ListHandle(TheOneRingHandle):
57
58         def __init__(self, connection, id, listName):
59                 handleType = telepathy.HANDLE_TYPE_LIST
60                 handleName = listName
61                 TheOneRingHandle.__init__(self, connection, id, handleType, handleName)
62
63
64 _HANDLE_TYPE_MAPPING = {
65         'connection': ConnectionHandle,
66         'contact': ContactHandle,
67         'list': ListHandle,
68 }
69
70
71 def create_handle_factory():
72
73         cache = weakref.WeakValueDictionary()
74
75         def _create_handle(connection, type, *args):
76                 Handle = _HANDLE_TYPE_MAPPING[type]
77                 key = Handle, connection.username, args
78                 try:
79                         handle = cache[key]
80                         isNewHandle = False
81                 except KeyError:
82                         # The misnamed get_handle_id requests a new handle id
83                         handle = Handle(connection, connection.get_handle_id(), *args)
84                         cache[key] = handle
85                         isNewHandle = True
86                 connection._handles[handle.get_type(), handle.get_id()] = handle
87                 if isNewHandle:
88                         handleStatus = "Is New!" if isNewHandle else "From Cache"
89                         _moduleLogger.debug("Created Handle: %r (%s)" % (handle, handleStatus))
90                 return handle
91
92         return _create_handle
93
94
95 create_handle = create_handle_factory()