Missed some locations
[theonering] / src / handle.py
1 import logging
2 import weakref
3
4 import telepathy
5
6
7 _moduleLogger = logging.getLogger("handle")
8
9
10 class MetaMemoize(type):
11         """
12         Allows a class to cache off instances for reuse
13         """
14
15         def __call__(cls, connection, *args):
16                 obj, newlyCreated = cls.__new__(cls, connection, *args)
17                 if newlyCreated:
18                         obj.__init__(connection, connection.get_handle_id(), *args)
19                         _moduleLogger.info("New Handle %r" % obj)
20                 return obj
21
22
23 class TheOneRingHandle(telepathy.server.Handle):
24         """
25         Instances are memoized
26         """
27
28         __metaclass__ = MetaMemoize
29
30         _instances = weakref.WeakValueDictionary()
31
32         def __new__(cls, connection, *args):
33                 key = cls, connection.username, args
34                 if key in cls._instances.keys():
35                         return cls._instances[key], False
36                 else:
37                         instance = object.__new__(cls, connection, *args)
38                         cls._instances[key] = instance # TRICKY: instances is a weakdict
39                         return instance, True
40
41         def __init__(self, connection, id, handleType, name):
42                 telepathy.server.Handle.__init__(self, id, handleType, name)
43                 self._conn = weakref.proxy(connection)
44
45         def __repr__(self):
46                 return "<%s id=%u name='%s'>" % (
47                         type(self).__name__, self.id, self.name
48                 )
49
50         id = property(telepathy.server.Handle.get_id)
51         type = property(telepathy.server.Handle.get_type)
52         name = property(telepathy.server.Handle.get_name)
53
54
55 class ConnectionHandle(TheOneRingHandle):
56
57         instance = None
58
59         def __init__(self, connection, id):
60                 handleType = telepathy.HANDLE_TYPE_CONTACT
61                 handleName = connection.username
62                 TheOneRingHandle.__init__(self, connection, id, handleType, handleName)
63
64                 self.profile = connection.username
65
66
67 def field_join(fields):
68         """
69         >>> field_join("1", "First Name")
70         '1#First Name'
71         """
72         return "#".join(fields)
73
74
75 def field_split(fields):
76         """
77         >>> field_split('1#First Name')
78         ['1', 'First Name']
79         """
80         return fields.split("#")
81
82
83 class ContactHandle(TheOneRingHandle):
84
85         def __init__(self, connection, id, contactId, contactAccount):
86                 handleType = telepathy.HANDLE_TYPE_CONTACT
87                 handleName = field_join(contactId, contactAccount)
88                 TheOneRingHandle.__init__(self, connection, id, handleType, handleName)
89
90                 self.account = contactAccount
91                 self._id = contactId
92
93         @property
94         def contact(self):
95                 return self._conn.gvoice_client.get_contact_details(self._id)
96
97
98 class ListHandle(TheOneRingHandle):
99
100         def __init__(self, connection, id, listName):
101                 handleType = telepathy.HANDLE_TYPE_LIST
102                 handleName = listName
103                 TheOneRingHandle.__init__(self, connection, id, handleType, handleName)
104
105
106 class GroupHandle(TheOneRingHandle):
107
108         def __init__(self, connection, id, groupName):
109                 handleType = telepathy.HANDLE_TYPE_GROUP
110                 handleName = groupName
111                 TheOneRingHandle.__init__(self, connection, id, handleType, handleName)
112
113
114 _HANDLE_TYPE_MAPPING = {
115         'connection': ConnectionHandle,
116         'contact': ContactHandle,
117         'list': ListHandle,
118         'group': GroupHandle,
119 }
120
121
122 def create_handle(connection, type, *args):
123         handle = _HANDLE_TYPE_MAPPING[type](connection, *args)
124         connection._handles[handle.get_type(), handle.get_id()] = handle
125         return handle