X-Git-Url: http://git.maemo.org/git/?a=blobdiff_plain;f=src%2Fhandle.py;h=ff405bfe23c65c78c0f6168206302f59e56712f1;hb=2818cfdf68997350d8aaf40c6abf990a91996460;hp=022a09353c8ff421349d033d189a9852f39f5371;hpb=ab4da214c520d763bbbddeb828c8e7c838374fe3;p=theonering diff --git a/src/handle.py b/src/handle.py index 022a093..ff405bf 100644 --- a/src/handle.py +++ b/src/handle.py @@ -4,17 +4,7 @@ import weakref import telepathy -class MetaMemoize(type): - """ - Allows a class to cache off instances for reuse - """ - - def __call__(cls, connection, *args): - obj, newlyCreated = cls.__new__(cls, connection, *args) - if newlyCreated: - obj.__init__(connection, connection.get_handle_id(), *args) - logging.info("New Handle %r" % obj) - return obj +_moduleLogger = logging.getLogger("handle") class TheOneRingHandle(telepathy.server.Handle): @@ -22,19 +12,6 @@ class TheOneRingHandle(telepathy.server.Handle): Instances are memoized """ - __metaclass__ = MetaMemoize - - _instances = weakref.WeakValueDictionary() - - def __new__(cls, connection, *args): - key = cls, connection.username, args - if key in cls._instances.keys(): - return cls._instances[key], False - else: - instance = object.__new__(cls, connection, *args) - cls._instances[key] = instance # TRICKY: instances is a weakdict - return instance, True - def __init__(self, connection, id, handleType, name): telepathy.server.Handle.__init__(self, id, handleType, name) self._conn = weakref.proxy(connection) @@ -49,9 +26,7 @@ class TheOneRingHandle(telepathy.server.Handle): name = property(telepathy.server.Handle.get_name) -class SelfHandle(TheOneRingHandle): - - instance = None +class ConnectionHandle(TheOneRingHandle): def __init__(self, connection, id): handleType = telepathy.HANDLE_TYPE_CONTACT @@ -61,35 +36,48 @@ class SelfHandle(TheOneRingHandle): self.profile = connection.username -def field_join(fields): - """ - >>> field_join("1", "First Name") - '1#First Name' +def strip_number(prettynumber): """ - return "#".join(fields) - + function to take a phone number and strip out all non-numeric + characters -def field_split(fields): + >>> strip_number("+012-(345)-678-90") + '01234567890' """ - >>> field_split('1#First Name') - ['1', 'First Name'] - """ - return fields.split("#") + import re + uglynumber = re.sub('\D', '', prettynumber) + return uglynumber class ContactHandle(TheOneRingHandle): - def __init__(self, connection, id, contactId, contactAccount): + def __init__(self, connection, id, contactId, phoneNumber): handleType = telepathy.HANDLE_TYPE_CONTACT - handleName = field_join(contactId, contactAccount) + handleName = self.to_handle_name(contactId, phoneNumber) TheOneRingHandle.__init__(self, connection, id, handleType, handleName) - self.account = contactAccount - self._id = contactId + self._contactId = contactId + self._phoneNumber = phoneNumber + + @staticmethod + def from_handle_name(handleName): + parts = handleName.split("#") + assert len(parts) == 2 + contactId, contactNumber = parts[0:2] + return contactId, contactNumber + + @staticmethod + def to_handle_name(contactId, contactNumber): + handleName = "#".join((contactId, strip_number(contactNumber))) + return handleName @property - def contact(self): - return self._conn.gvoice_client.get_contact_details(self._id) + def contactID(self): + return self._contactId + + @property + def contactDetails(self): + return self._conn.addressbook.get_contact_details(self._id) class ListHandle(TheOneRingHandle): @@ -100,23 +88,34 @@ class ListHandle(TheOneRingHandle): TheOneRingHandle.__init__(self, connection, id, handleType, handleName) -class GroupHandle(TheOneRingHandle): - - def __init__(self, connection, id, groupName): - handleType = telepathy.HANDLE_TYPE_GROUP - handleName = groupName - TheOneRingHandle.__init__(self, connection, id, handleType, handleName) - - _HANDLE_TYPE_MAPPING = { - 'self': SelfHandle, + 'connection': ConnectionHandle, 'contact': ContactHandle, 'list': ListHandle, - 'group': GroupHandle, } -def create_handle(connection, type, *args): - handle = _HANDLE_TYPE_MAPPING[type](connection, *args) - connection._handles[handle.get_type(), handle.get_id()] = handle - return handle +def create_handle_factory(): + + cache = weakref.WeakValueDictionary() + + def create_handle(connection, type, *args): + Handle = _HANDLE_TYPE_MAPPING[type] + key = Handle, connection.username, args + try: + handle = cache[key] + isNewHandle = False + except KeyError: + # The misnamed get_handle_id requests a new handle id + handle = Handle(connection, connection.get_handle_id(), *args) + cache[key] = handle + isNewHandle = True + connection._handles[handle.get_type(), handle.get_id()] = handle + handleStatus = "Is New!" if isNewHandle else "From Cache" + _moduleLogger.info("Created Handle: %r (%s)" % (handle, handleStatus)) + return handle + + return create_handle + + +create_handle = create_handle_factory()