import telepathy
import tp
-import util.misc as util_misc
+import util.misc as misc_utils
-_moduleLogger = logging.getLogger("handle")
+_moduleLogger = logging.getLogger(__name__)
class TheOneRingHandle(tp.Handle):
class ContactHandle(TheOneRingHandle):
- _DELIMETER = "|"
+ def __init__(self, connection, id, phoneNumber):
+ self._phoneNumber = misc_utils.normalize_number(phoneNumber)
- def __init__(self, connection, id, contactId, 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 = util_misc.normalize_number(phoneNumber)
-
- @classmethod
- def from_handle_name(cls, handleName):
- """
- >>> ContactHandle.from_handle_name("+1 555 123-1234")
- ('', '+15551231234')
- >>> ContactHandle.from_handle_name("+15551231234")
- ('', '+15551231234')
- >>> ContactHandle.from_handle_name("123456|+15551231234")
- ('123456', '+15551231234')
- """
- parts = handleName.split(cls._DELIMETER, 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 = util_misc.normalize_number(contactNumber)
- return contactId, contactNumber
-
- @classmethod
- def to_handle_name(cls, contactId, contactNumber):
- """
- >>> ContactHandle.to_handle_name('', "+1 555 123-1234")
- '+15551231234'
- >>> ContactHandle.to_handle_name('', "+15551231234")
- '+15551231234'
- >>> ContactHandle.to_handle_name('123456', "+15551231234")
- '123456|+15551231234'
- """
- contactNumber = util_misc.normalize_number(contactNumber)
- if contactId:
- handleName = cls._DELIMETER.join((contactId, contactNumber))
- else:
- handleName = 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):
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:
_moduleLogger.debug("Created Handle: %r (%s)" % (handle, handleStatus))
return handle
- return create_handle
+ return _create_handle
create_handle = create_handle_factory()