Adding quit, update_now. Fixing reset_state_machine
[theonering] / src / channel / debug_prompt.py
index 7c8df1c..fca8a9b 100644 (file)
@@ -11,11 +11,12 @@ import telepathy
 
 import constants
 import tp
-import gtk_toolbox
+import util.misc as misc_utils
+import util.go_utils as gobject_utils
 import gvoice
 
 
-_moduleLogger = logging.getLogger("channel.debug_prompt")
+_moduleLogger = logging.getLogger(__name__)
 
 
 class DebugPromptChannel(tp.ChannelTypeText, cmd.Cmd):
@@ -31,8 +32,9 @@ class DebugPromptChannel(tp.ChannelTypeText, cmd.Cmd):
                self.__lastMessageTimestamp = datetime.datetime(1, 1, 1)
 
                self.__otherHandle = contactHandle
+               self._conn.add_logger(self)
 
-       @gtk_toolbox.log_exception(_moduleLogger)
+       @misc_utils.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)
@@ -51,7 +53,7 @@ class DebugPromptChannel(tp.ChannelTypeText, cmd.Cmd):
                if stdoutData:
                        self._report_new_message(stdoutData)
 
-       @gtk_toolbox.log_exception(_moduleLogger)
+       @misc_utils.log_exception(_moduleLogger)
        def Close(self):
                self.close()
 
@@ -59,6 +61,11 @@ class DebugPromptChannel(tp.ChannelTypeText, cmd.Cmd):
                _moduleLogger.debug("Closing debug")
                tp.ChannelTypeText.Close(self)
                self.remove_from_connection()
+               self._conn.remove_logger(self)
+
+       def log_message(self, component, message):
+               formattedMessage = "LOG: %s\n%s" % (component, message)
+               self._report_new_message(formattedMessage)
 
        def _report_new_message(self, message):
                currentReceivedId = self.__nextRecievedId
@@ -70,6 +77,19 @@ class DebugPromptChannel(tp.ChannelTypeText, cmd.Cmd):
 
                self.__nextRecievedId += 1
 
+       def do_quit(self, args):
+               if args:
+                       self._report_new_message("No arguments supported")
+                       return
+
+               try:
+                       self._conn.manager.quit_now()
+               except Exception, e:
+                       self._report_new_message(str(e))
+
+       def help_quit(self):
+               self._report_new_message("Shutdown the connection manager immediately, forcing it to be relaunched on reconnect")
+
        def do_reset_state_machine(self, args):
                try:
                        args = args.strip().lower()
@@ -77,13 +97,13 @@ class DebugPromptChannel(tp.ChannelTypeText, cmd.Cmd):
                                args  = "all"
                        if args == "all":
                                for machine in self._conn.session.stateMachine._machines:
-                                       machine.reset_timers()
+                                       machine.reset_timers(True)
                        elif args == "contacts":
-                               self._conn.session.addressbookStateMachine.reset_timers()
+                               self._conn.session.addressbookStateMachine.reset_timers(True)
                        elif args == "voicemail":
-                               self._conn.session.voicemailsStateMachine.reset_timers()
+                               self._conn.session.voicemailsStateMachine.reset_timers(True)
                        elif args == "texts":
-                               self._conn.session.textsStateMachine.reset_timers()
+                               self._conn.session.textsStateMachine.reset_timers(True)
                        else:
                                self._report_new_message('Unknown machine "%s"' % (args, ))
                except Exception, e:
@@ -98,6 +118,34 @@ class DebugPromptChannel(tp.ChannelTypeText, cmd.Cmd):
 "reset_state_machine texts"
 """)
 
+       def do_update_now(self, args):
+               try:
+                       args = args.strip().lower()
+                       if not args:
+                               args  = "all"
+                       if args == "all":
+                               for machine in self._conn.session.stateMachine._machines:
+                                       machine.update_now()
+                       elif args == "contacts":
+                               self._conn.session.addressbookStateMachine.update_now()
+                       elif args == "voicemail":
+                               self._conn.session.voicemailsStateMachine.update_now()
+                       elif args == "texts":
+                               self._conn.session.textsStateMachine.update_now()
+                       else:
+                               self._report_new_message('Unknown machine "%s"' % (args, ))
+               except Exception, e:
+                       self._report_new_message(str(e))
+
+       def help_update_now(self):
+               self._report_new_message("""Updates all the state machines now skipping there initialization time.
+"update_now" - for all
+"update_now all"
+"update_now contacts"
+"update_now voicemail"
+"update_now texts"
+""")
+
        def do_get_state(self, args):
                if args:
                        self._report_new_message("No arguments supported")
@@ -112,30 +160,84 @@ class DebugPromptChannel(tp.ChannelTypeText, cmd.Cmd):
        def help_get_state(self):
                self._report_new_message("Print the current state the refreshing state machine is in")
 
+       def do_get_polling(self, args):
+               if args:
+                       self._report_new_message("No arguments supported")
+                       return
+               self._report_new_message("\n".join((
+                       "Contacts:", repr(self._conn.session.addressbookStateMachine)
+               )))
+               self._report_new_message("\n".join((
+                       "Voicemail:", repr(self._conn.session.voicemailsStateMachine)
+               )))
+               self._report_new_message("\n".join((
+                       "Texts:", repr(self._conn.session.textsStateMachine)
+               )))
+
+       def help_get_polling(self):
+               self._report_new_message("Prints the frequency each of the state machines updates")
+
+       def do_get_state_status(self, args):
+               if args:
+                       self._report_new_message("No arguments supported")
+                       return
+               self._report_new_message("\n".join((
+                       "Contacts:", str(self._conn.session.addressbookStateMachine)
+               )))
+               self._report_new_message("\n".join((
+                       "Voicemail:", str(self._conn.session.voicemailsStateMachine)
+               )))
+               self._report_new_message("\n".join((
+                       "Texts:", str(self._conn.session.textsStateMachine)
+               )))
+
+       def help_get_state_status(self):
+               self._report_new_message("Prints the current setting for the state machines")
+
        def do_is_authed(self, args):
+               le = gobject_utils.AsyncLinearExecution(self._conn.session.pool, self._is_authed)
+               le.start(args)
+
+       @misc_utils.log_exception(_moduleLogger)
+       def _is_authed(self, args):
                if args:
                        self._report_new_message("No arguments supported")
                        return
 
                try:
-                       isAuthed = self._conn.session.backend.is_authed()
+                       isAuthed = yield (
+                               self._conn.session.backend.is_authed,
+                               (),
+                               {}
+                       )
                        self._report_new_message(str(isAuthed))
                except Exception, e:
                        self._report_new_message(str(e))
+                       return
 
        def help_is_authed(self):
                self._report_new_message("Print whether logged in to Google Voice")
 
        def do_is_dnd(self, args):
+               le = gobject_utils.AsyncLinearExecution(self._conn.session.pool, self._is_dnd)
+               le.start(args)
+
+       @misc_utils.log_exception(_moduleLogger)
+       def _is_dnd(self, args):
                if args:
                        self._report_new_message("No arguments supported")
                        return
 
                try:
-                       isDnd = self._conn.session.backend.is_dnd()
+                       isDnd = yield (
+                               self._conn.session.backend.is_dnd,
+                               (),
+                               {}
+                       )
                        self._report_new_message(str(isDnd))
                except Exception, e:
                        self._report_new_message(str(e))
+                       return
 
        def help_is_dnd(self):
                self._report_new_message("Print whether Do-Not-Disturb mode enabled on the Google Voice account")
@@ -201,13 +303,22 @@ class DebugPromptChannel(tp.ChannelTypeText, cmd.Cmd):
                self._report_new_message("Print the callback number currently enabled")
 
        def do_call(self, args):
+               le = gobject_utils.AsyncLinearExecution(self._conn.session.pool, self._call)
+               le.start(args)
+
+       @misc_utils.log_exception(_moduleLogger)
+       def _call(self, args):
                if not args:
                        self._report_new_message("Must specify the phone number and only the phone nunber")
                        return
 
                try:
                        number = args
-                       self._conn.session.backend.call(number)
+                       yield (
+                               self._conn.session.backend.call,
+                               (number, ),
+                               {}
+                       )
                except Exception, e:
                        self._report_new_message(str(e))
 
@@ -215,6 +326,11 @@ class DebugPromptChannel(tp.ChannelTypeText, cmd.Cmd):
                self._report_new_message("\n".join(["call NUMBER", "Initiate a callback, Google forwarding the call to the callback number"]))
 
        def do_send_sms(self, args):
+               le = gobject_utils.AsyncLinearExecution(self._conn.session.pool, self._send_sms)
+               le.start(args)
+
+       @misc_utils.log_exception(_moduleLogger)
+       def _send_sms(self, args):
                args = args.split(" ")
                if 1 < len(args):
                        self._report_new_message("Must specify the phone number and then message")
@@ -223,7 +339,11 @@ class DebugPromptChannel(tp.ChannelTypeText, cmd.Cmd):
                try:
                        number = args[0]
                        message = " ".join(args[1:])
-                       self._conn.session.backend.send_sms(number, message)
+                       yield (
+                               self._conn.session.backend.send_sms,
+                               ([number], message),
+                               {},
+                       )
                except Exception, e:
                        self._report_new_message(str(e))
 
@@ -239,30 +359,13 @@ class DebugPromptChannel(tp.ChannelTypeText, cmd.Cmd):
        def help_version(self):
                self._report_new_message("Prints the version (hint: %s-%s)" % (constants.__version__, constants.__build__))
 
-       def do_get_polling(self, args):
-               if args:
-                       self._report_new_message("No arguments supported")
-                       return
-               self._report_new_message("\n".join((
-                       "Contacts:", repr(self._conn.session.addressbookStateMachine)
-               )))
-               self._report_new_message("\n".join((
-                       "Voicemail:", repr(self._conn.session.voicemailsStateMachine)
-               )))
-               self._report_new_message("\n".join((
-                       "Texts:", repr(self._conn.session.textsStateMachine)
-               )))
-
-       def help_get_polling(self):
-               self._report_new_message("Prints the frequency each of the state machines updates")
-
        def do_grab_log(self, args):
                if args:
                        self._report_new_message("No arguments supported")
                        return
 
                try:
-                       publishProps = self._conn._generate_props(telepathy.CHANNEL_TYPE_FILE_TRANSFER, self.__otherHandle, False)
+                       publishProps = self._conn.generate_props(telepathy.CHANNEL_TYPE_FILE_TRANSFER, self.__otherHandle, False)
                        self._conn._channel_manager.channel_for_props(publishProps, signal=True)
                except Exception, e:
                        self._report_new_message(str(e))