From 8713a26f7f2307e5393f9c5d48ce0507f30def14 Mon Sep 17 00:00:00 2001
From: Stavros Sachtouris <saxtouri@admin.grnet.gr>
Date: Thu, 15 Oct 2015 14:12:30 +0300
Subject: [PATCH] Manage GUI launching in cli.py, handle CLI ctrl-C

When starting GUI from CLI, the user may stop it with a keyboard
interrupt. We treat this as equivalent to quitting the application
through the GUI, therefore the interrupt is caught and handled
accordingly.

The implementation required to move some launching code from
"gui.py" to "cli.py", which enabled the reuse of some CLI
methods (_start and _shutdown).
---
 agkyra/cli.py | 21 ++++++++++++++++++++-
 agkyra/gui.py | 21 ---------------------
 2 files changed, 20 insertions(+), 22 deletions(-)

diff --git a/agkyra/cli.py b/agkyra/cli.py
index 593a1d6..bf1dbe2 100644
--- a/agkyra/cli.py
+++ b/agkyra/cli.py
@@ -449,4 +449,23 @@ class AgkyraCLI(cmd.Cmd):
         """
         if self.must_help('gui'):
             return
-        gui.run(callback=self.callback, debug=self.args.debug)
+        session = self.helper.load_active_session()
+        if not session:
+            self._start('')
+            session = self.helper.wait_session_to_load()
+        if session:
+            LOGGER.info('Start new GUI')
+            new_gui = gui.GUI(session, debug=self.args.debug)
+            try:
+                new_gui.start()
+            except KeyboardInterrupt:
+                LOGGER.info('GUI interrupted by user')
+                sys.stderr.write('GUI interrupted by user, exiting\n')
+                sys.stderr.flush()
+                new_gui.clean_exit()
+            LOGGER.info('GUI is shutdown')
+            if self.client:
+                self._shutdown('')
+        else:
+            sys.stderr.write('Session failed to load\n')
+            sys.stderr.flush()
diff --git a/agkyra/gui.py b/agkyra/gui.py
index c2d5eaf..982041a 100644
--- a/agkyra/gui.py
+++ b/agkyra/gui.py
@@ -101,24 +101,3 @@ class GUI(WebSocketBaseClient):
         finally:
             self.clean_exit()
         LOG.debug('GUI finished, close GUI wrapper connection')
-
-
-def run(callback, debug):
-    """Prepare SessionHelper and GUI and run them in the proper order"""
-    launch_server(callback, debug)
-    LOG.info('Client blocks until session is ready')
-    session = SessionHelper().wait_session_to_load()
-    assert session, 'UI server failed to load...'
-    LOG.info('Server session is ready, setup the GUI session')
-    gui = GUI(session, debug=debug)
-
-    try:
-        LOG.info('Start GUI')
-        gui.start()
-    except KeyboardInterrupt:
-        LOG.info('Shutdown GUI')
-        gui.clean_exit()
-
-if __name__ == '__main__':
-    logging.basicConfig(filename='agkyra.log', level=logging.DEBUG)
-    run(os.path.abspath('gui/app'))
-- 
GitLab