From: Ed Page Date: Wed, 6 Jan 2010 04:05:44 +0000 (-0600) Subject: Adding a debug prompt. X-Git-Url: http://git.maemo.org/git/?p=theonering;a=commitdiff_plain;h=4856262e2c13dcb2d23de4774010800bd084bc7d Adding a debug prompt. To help, trying to make the numbers passed around a bit more consistent --- diff --git a/src/aliasing.py b/src/aliasing.py index a05e4fe..c22f6bb 100644 --- a/src/aliasing.py +++ b/src/aliasing.py @@ -3,6 +3,7 @@ import logging import telepathy import gtk_toolbox +import util.misc as util_misc import handle @@ -35,7 +36,7 @@ def make_pretty(phonenumber): if phonenumber is None or phonenumber is "": return "" - phonenumber = handle.strip_number(phonenumber) + phonenumber = util_misc.strip_number(phonenumber) if len(phonenumber) < 3: return phonenumber @@ -126,7 +127,7 @@ class AliasingMixin(telepathy.server.ConnectionInterfaceAliasing): return # Update callback - uglyNumber = handle.strip_number(userHandleAndAlias[1]) + uglyNumber = util_misc.strip_number(userHandleAndAlias[1]) self.session.backend.set_callback_number(uglyNumber) # Inform of change diff --git a/src/channel/__init__.py b/src/channel/__init__.py index aeb2796..7625b70 100644 --- a/src/channel/__init__.py +++ b/src/channel/__init__.py @@ -3,3 +3,4 @@ import contact_list import text import call +import debug_prompt diff --git a/src/channel/debug_prompt.py b/src/channel/debug_prompt.py new file mode 100644 index 0000000..3352c2a --- /dev/null +++ b/src/channel/debug_prompt.py @@ -0,0 +1,164 @@ +import cmd +import StringIO +import time +import datetime +import logging + +import telepathy + +import gtk_toolbox + + +_moduleLogger = logging.getLogger("channel.text") + + +class DebugPromptChannel(telepathy.server.ChannelTypeText, cmd.Cmd): + """ + Look into implementing ChannelInterfaceMessages for rich text formatting + """ + + def __init__(self, connection, manager, props, contactHandle): + self._manager = manager + self._props = props + + cmd.Cmd.__init__(self, "Debug Prompt") + self.use_rawinput = False + try: + # HACK Older python-telepathy way + telepathy.server.ChannelTypeText.__init__(self, connection, contactHandle) + except TypeError: + # HACK Newer python-telepathy way + telepathy.server.ChannelTypeText.__init__(self, connection, manager, props) + self._nextRecievedId = 0 + self._lastMessageTimestamp = datetime.datetime(1, 1, 1) + + self._otherHandle = contactHandle + + @gtk_toolbox.log_exception(_moduleLogger) + def Send(self, messageType, text): + if messageType != telepathy.CHANNEL_TEXT_MESSAGE_TYPE_NORMAL: + raise telepathy.errors.NotImplemented("Unhandled message type: %r" % messageType) + + self.Sent(int(time.time()), messageType, text) + + oldStdin, oldStdout = self.stdin, self.stdout + try: + self.stdin = currentStdin = StringIO.StringIO() + self.stdout = currentStdout = StringIO.StringIO() + self.onecmd(text) + finally: + self.stdin, self.stdout = oldStdin, oldStdout + + self._report_new_message(currentStdout.getvalue().strip()) + + @gtk_toolbox.log_exception(_moduleLogger) + def Close(self): + self.close() + + def close(self): + telepathy.server.ChannelTypeText.Close(self) + if self._manager.channel_exists(self._props): + # HACK Older python-telepathy requires doing this manually + self._manager.remove_channel(self) + self.remove_from_connection() + + def _report_new_message(self, message): + currentReceivedId = self._nextRecievedId + + timestamp = int(time.time()) + type = telepathy.CHANNEL_TEXT_MESSAGE_TYPE_NORMAL + + self.Received(currentReceivedId, timestamp, self._otherHandle, type, 0, message) + + self._nextRecievedId += 1 + + def do_reset_state_machine(self, args): + if args: + self._report_new_message("No arguments supported") + return + + try: + self._conn.session.stateMachine.reset_timers() + except Exception, e: + self._report_new_message(str(e)) + + def do_get_state(self, args): + if args: + self._report_new_message("No arguments supported") + return + + try: + state = self._conn.session.stateMachine.get_state() + self._report_new_message(str(state)) + except Exception, e: + self._report_new_message(str(e)) + + def do_is_authed(self, args): + if args: + self._report_new_message("No arguments supported") + return + + try: + isAuthed = self._conn.session.backend.is_authed() + self._report_new_message(str(isAuthed)) + except Exception, e: + self._report_new_message(str(e)) + + def do_is_dnd(self, args): + if args: + self._report_new_message("No arguments supported") + return + + try: + isDnd = self._conn.session.backend.is_dnd() + self._report_new_message(str(isDnd)) + except Exception, e: + self._report_new_message(str(e)) + + def do_get_account_number(self, args): + if args: + self._report_new_message("No arguments supported") + return + + try: + number = self._conn.session.backend.get_account_number() + self._report_new_message(number) + except Exception, e: + self._report_new_message(str(e)) + + def do_get_callback_numbers(self, args): + if args: + self._report_new_message("No arguments supported") + return + + try: + numbers = self._conn.session.backend.get_callback_numbers() + numbersDisplay = "\n".join( + "%s: %s" % (name, number) + for (number, name) in numbers.iteritems() + ) + self._report_new_message(numbersDisplay) + except Exception, e: + self._report_new_message(str(e)) + + def do_get_callback_number(self, args): + if args: + self._report_new_message("No arguments supported") + return + + try: + number = self._conn.session.backend.get_callback_number() + self._report_new_message(number) + except Exception, e: + self._report_new_message(str(e)) + + def do_call(self, args): + if len(args) != 1: + self._report_new_message("No arguments supported") + return + + try: + number = args[0] + self._conn.session.backend.call(number) + except Exception, e: + self._report_new_message(str(e)) diff --git a/src/channel/text.py b/src/channel/text.py index d2c5313..0fbf67d 100644 --- a/src/channel/text.py +++ b/src/channel/text.py @@ -46,7 +46,7 @@ class TextChannel(telepathy.server.ChannelTypeText): try: mergedConversations = self._conn.session.conversations.get_conversation(self._contactKey) except KeyError: - pass + _moduleLogger.info("Nothing in the conversation yet for %r" % (self._contactKey, )) else: self._report_conversation(mergedConversations) diff --git a/src/channel_manager.py b/src/channel_manager.py index 4f289dd..29549de 100644 --- a/src/channel_manager.py +++ b/src/channel_manager.py @@ -4,6 +4,7 @@ import dbus import telepathy import channel +import util.misc as util_misc _moduleLogger = logging.getLogger("channel_manager") @@ -131,22 +132,28 @@ class ChannelManager(TelepathyChannelManager): ) def _get_list_channel(self, props): - _, surpress_handler, handle = self._get_type_requested_handle(props) + _, surpress_handler, h = self._get_type_requested_handle(props) _moduleLogger.debug('New contact list channel') - chan = channel.contact_list.create_contact_list_channel(self._conn, self, props, handle) + chan = channel.contact_list.create_contact_list_channel(self._conn, self, props, h) return chan def _get_text_channel(self, props): - _, surpress_handler, handle = self._get_type_requested_handle(props) + _, surpress_handler, h = self._get_type_requested_handle(props) - _moduleLogger.debug('New text channel') - chan = channel.text.TextChannel(self._conn, self, props, handle) + accountNumber = util_misc.strip_number(self._conn.session.backend.get_account_number()) + if h.phoneNumber == accountNumber: + _moduleLogger.debug('New Debug channel') + chan = channel.debug_prompt.DebugPromptChannel(self._conn, self, props, h) + else: + _moduleLogger.debug('New text channel') + _moduleLogger.info('%r %r' % (accountNumber, h.phoneNumber)) + chan = channel.text.TextChannel(self._conn, self, props, h) return chan def _get_media_channel(self, props): - _, surpress_handler, handle = self._get_type_requested_handle(props) + _, surpress_handler, h = self._get_type_requested_handle(props) _moduleLogger.debug('New media channel') - chan = channel.call.CallChannel(self._conn, self, props, handle) + chan = channel.call.CallChannel(self._conn, self, props, h) return chan diff --git a/src/gvoice/addressbook.py b/src/gvoice/addressbook.py index f9f713b..5a88891 100644 --- a/src/gvoice/addressbook.py +++ b/src/gvoice/addressbook.py @@ -4,6 +4,7 @@ import logging import util.coroutines as coroutines +import util.misc as util_misc _moduleLogger = logging.getLogger("gvoice.addressbook") @@ -52,6 +53,13 @@ class Addressbook(object): self._populate_contact_details(contactId) return self._get_contact_details(contactId) + def find_contacts_with_number(self, queryNumber): + strippedQueryNumber = util_misc.strip_number(queryNumber) + for contactId, (contactName, contactDetails) in self.get_contacts(): + for phoneType, number in contactDetails: + if number == strippedQueryNumber: + yield contactId + def _populate_contacts(self): if self._contacts: return @@ -59,7 +67,10 @@ class Addressbook(object): for contactId, contactDetails in contacts: contactName = contactDetails["name"] contactNumbers = [ - (numberDetails.get("phoneType", "Mobile"), numberDetails["phoneNumber"]) + ( + numberDetails.get("phoneType", "Mobile"), + util_misc.strip_number(numberDetails["phoneNumber"]), + ) for numberDetails in contactDetails["numbers"] ] self._contacts[contactId] = (contactName, contactNumbers) diff --git a/src/gvoice/conversations.py b/src/gvoice/conversations.py index 7435da8..36aca67 100644 --- a/src/gvoice/conversations.py +++ b/src/gvoice/conversations.py @@ -4,6 +4,7 @@ import logging import util.coroutines as coroutines +import util.misc as util_misc _moduleLogger = logging.getLogger("gvoice.conversations") @@ -27,7 +28,7 @@ class Conversations(object): conversations = list(self._backend.get_conversations()) conversations.sort() for conversation in conversations: - key = conversation.contactId, conversation.number + key = conversation.contactId, util_misc.strip_number(conversation.number) try: mergedConversations = self._conversations[key] except KeyError: diff --git a/src/handle.py b/src/handle.py index 33d5660..9c34ca5 100644 --- a/src/handle.py +++ b/src/handle.py @@ -3,6 +3,8 @@ import weakref import telepathy +import util.misc as util_misc + _moduleLogger = logging.getLogger("handle") @@ -36,19 +38,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 +46,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,12 +58,12 @@ 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 @property diff --git a/src/util/misc.py b/src/util/misc.py index 8210a66..47924fa 100644 --- a/src/util/misc.py +++ b/src/util/misc.py @@ -640,3 +640,16 @@ def lexical_scope(*args): for key in (x for x in f_locals.keys() if x not in saved): del f_locals[key] del frame + + +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