Imitiating buttfly in being explicitly typed
[theonering] / src / handle.py
index 33d5660..0db35fe 100644 (file)
@@ -3,17 +3,20 @@ import weakref
 
 import telepathy
 
+import tp
+import util.misc as misc_utils
 
-_moduleLogger = logging.getLogger("handle")
 
+_moduleLogger = logging.getLogger(__name__)
 
-class TheOneRingHandle(telepathy.server.Handle):
+
+class TheOneRingHandle(tp.Handle):
        """
        Instances are memoized
        """
 
        def __init__(self, connection, id, handleType, name):
-               telepathy.server.Handle.__init__(self, id, handleType, name)
+               tp.Handle.__init__(self, id, handleType, name)
                self._conn = weakref.proxy(connection)
 
        def __repr__(self):
@@ -21,9 +24,9 @@ class TheOneRingHandle(telepathy.server.Handle):
                        type(self).__name__, self.id, self.name
                )
 
-       id = property(telepathy.server.Handle.get_id)
-       type = property(telepathy.server.Handle.get_type)
-       name = property(telepathy.server.Handle.get_name)
+       id = property(tp.Handle.get_id)
+       type = property(tp.Handle.get_type)
+       name = property(tp.Handle.get_name)
 
 
 class ConnectionHandle(TheOneRingHandle):
@@ -36,59 +39,19 @@ class ConnectionHandle(TheOneRingHandle):
                self.profile = connection.username
 
 
-def strip_number(prettynumber):
-       """
-       function to take a phone number and strip out all non-numeric
-       characters
-
-       >>> strip_number("+012-(345)-678-90")
-       '01234567890'
-       """
-       import re
-       uglynumber = re.sub('\D', '', prettynumber)
-       return uglynumber
-
-
 class ContactHandle(TheOneRingHandle):
 
-       def __init__(self, connection, id, contactId, phoneNumber):
+       def __init__(self, connection, id, phoneNumber):
+               self._phoneNumber = misc_utils.normalize_number(phoneNumber)
+
                handleType = telepathy.HANDLE_TYPE_CONTACT
-               handleName = self.to_handle_name(contactId, phoneNumber)
+               handleName = self._phoneNumber
                TheOneRingHandle.__init__(self, connection, id, handleType, handleName)
 
-               self._contactId = contactId
-               self._phoneNumber = phoneNumber
-
-       @staticmethod
-       def from_handle_name(handleName):
-               parts = handleName.split("#", 1)
-               if len(parts) == 2:
-                       contactId, contactNumber = parts[0:2]
-               elif len(parts) == 1:
-                       contactId, contactNumber = "", handleName
-               else:
-                       raise RuntimeError("Invalid handle: %s" % handleName)
-
-               contactNumber = strip_number(contactNumber)
-               return contactId, contactNumber
-
-       @staticmethod
-       def to_handle_name(contactId, contactNumber):
-               handleName = "#".join((contactId, strip_number(contactNumber)))
-               return handleName
-
-       @property
-       def contactID(self):
-               return self._contactId
-
        @property
        def phoneNumber(self):
                return self._phoneNumber
 
-       @property
-       def contactDetails(self):
-               return self._conn.addressbook.get_contact_details(self._id)
-
 
 class ListHandle(TheOneRingHandle):
 
@@ -109,7 +72,7 @@ def create_handle_factory():
 
        cache = weakref.WeakValueDictionary()
 
-       def create_handle(connection, type, *args):
+       def _create_handle(connection, type, *args):
                Handle = _HANDLE_TYPE_MAPPING[type]
                key = Handle, connection.username, args
                try:
@@ -121,12 +84,12 @@ def create_handle_factory():
                        cache[key] = handle
                        isNewHandle = True
                connection._handles[handle.get_type(), handle.get_id()] = handle
-               if False:
+               if isNewHandle:
                        handleStatus = "Is New!" if isNewHandle else "From Cache"
-                       _moduleLogger.info("Created Handle: %r (%s)" % (handle, handleStatus))
+                       _moduleLogger.debug("Created Handle: %r (%s)" % (handle, handleStatus))
                return handle
 
-       return create_handle
+       return _create_handle
 
 
 create_handle = create_handle_factory()