Attempting to fix a bug in code I stole from python-telepathy in which they deviated...
[theonering] / src / handle.py
index 55f36fb..d063a77 100644 (file)
@@ -3,6 +3,8 @@ import weakref
 
 import telepathy
 
+import util.misc as util_misc
+
 
 _moduleLogger = logging.getLogger("handle")
 
@@ -21,6 +23,9 @@ class TheOneRingHandle(telepathy.server.Handle):
                        type(self).__name__, self.id, self.name
                )
 
+       def is_same(self, handleType, handleName):
+               return self.get_name() == handleName and self.get_type() == handleType
+
        id = property(telepathy.server.Handle.get_id)
        type = property(telepathy.server.Handle.get_type)
        name = property(telepathy.server.Handle.get_name)
@@ -36,19 +41,6 @@ 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):
@@ -57,7 +49,7 @@ class ContactHandle(TheOneRingHandle):
                TheOneRingHandle.__init__(self, connection, id, handleType, handleName)
 
                self._contactId = contactId
-               self._phoneNumber = phoneNumber
+               self._phoneNumber = util_misc.strip_number(phoneNumber)
 
        @staticmethod
        def from_handle_name(handleName):
@@ -69,14 +61,28 @@ class ContactHandle(TheOneRingHandle):
                else:
                        raise RuntimeError("Invalid handle: %s" % handleName)
 
-               contactNumber = strip_number(contactNumber)
+               contactNumber = util_misc.strip_number(contactNumber)
                return contactId, contactNumber
 
        @staticmethod
        def to_handle_name(contactId, contactNumber):
-               handleName = "#".join((contactId, strip_number(contactNumber)))
+               handleName = "#".join((contactId, util_misc.strip_number(contactNumber)))
                return handleName
 
+       @classmethod
+       def normalize_handle_name(cls, name):
+               if "#" in name:
+                       # Already a properly formatted name, run through the ringer just in case
+                       return cls.to_handle_name(*cls.from_handle_name(name))
+                       return name
+               else:
+                       return cls.to_handle_name("", name)
+
+       def is_same(self, handleType, handleName):
+               handleName = self.normalize_handle_name(handleName)
+               _moduleLogger.info("%r == %r %r?" % (self, handleType, handleName))
+               return self.get_name() == handleName and self.get_type() == handleType
+
        @property
        def contactID(self):
                return self._contactId
@@ -121,8 +127,9 @@ def create_handle_factory():
                        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))
+               if False:
+                       handleStatus = "Is New!" if isNewHandle else "From Cache"
+                       _moduleLogger.info("Created Handle: %r (%s)" % (handle, handleStatus))
                return handle
 
        return create_handle