2015-07-21 15:55:02 +00:00
|
|
|
# Copyright 2015 Canonical, Ltd.
|
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU Affero General Public License as
|
|
|
|
# published by the Free Software Foundation, either version 3 of the
|
|
|
|
# License, or (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU Affero General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU Affero General Public License
|
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
import logging
|
|
|
|
import urwid
|
2015-08-23 22:48:52 +00:00
|
|
|
from tornado.ioloop import IOLoop
|
2015-08-31 15:55:46 +00:00
|
|
|
from tornado.util import import_object
|
2016-06-30 18:17:01 +00:00
|
|
|
from subiquitycore.signals import Signal
|
|
|
|
from subiquitycore.palette import STYLES, STYLES_MONO
|
|
|
|
from subiquitycore.prober import Prober, ProberException
|
2015-07-21 15:55:02 +00:00
|
|
|
|
2016-06-30 18:17:01 +00:00
|
|
|
log = logging.getLogger('subiquitycore.core')
|
2015-07-21 15:55:02 +00:00
|
|
|
|
|
|
|
|
|
|
|
class CoreControllerError(Exception):
|
|
|
|
""" Basecontroller exception """
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
class Controller:
|
|
|
|
def __init__(self, ui, opts):
|
2015-10-23 15:03:04 +00:00
|
|
|
try:
|
|
|
|
prober = Prober(opts)
|
|
|
|
except ProberException as e:
|
|
|
|
err = "Prober init failed: {}".format(e)
|
|
|
|
log.exception(err)
|
|
|
|
raise CoreControllerError(err)
|
|
|
|
|
2015-08-31 15:55:46 +00:00
|
|
|
self.common = {
|
|
|
|
"ui": ui,
|
|
|
|
"opts": opts,
|
|
|
|
"signal": Signal(),
|
2015-10-23 15:03:04 +00:00
|
|
|
"prober": prober,
|
2015-08-31 15:55:46 +00:00
|
|
|
"loop": None
|
|
|
|
}
|
2015-08-18 16:29:56 +00:00
|
|
|
self.controllers = {
|
2015-08-31 15:55:46 +00:00
|
|
|
"Welcome": None,
|
|
|
|
"Installpath": None,
|
|
|
|
"Network": None,
|
|
|
|
"Filesystem": None,
|
|
|
|
"Identity": None,
|
|
|
|
"InstallProgress": None,
|
2016-06-22 19:19:54 +00:00
|
|
|
"Login": None,
|
2015-08-18 16:29:56 +00:00
|
|
|
}
|
2016-06-22 19:19:54 +00:00
|
|
|
self.common['controllers'] = self.controllers
|
2015-07-21 20:34:46 +00:00
|
|
|
|
2015-08-18 16:29:56 +00:00
|
|
|
def _connect_base_signals(self):
|
2015-07-21 20:34:46 +00:00
|
|
|
""" Connect signals used in the core controller
|
|
|
|
"""
|
|
|
|
signals = []
|
|
|
|
|
2015-07-23 02:31:21 +00:00
|
|
|
# Add quit signal
|
|
|
|
signals.append(('quit', self.exit))
|
2015-08-24 03:55:45 +00:00
|
|
|
signals.append(('refresh', self.redraw_screen))
|
2015-08-31 15:55:46 +00:00
|
|
|
self.common['signal'].connect_signals(signals)
|
2015-08-18 16:29:56 +00:00
|
|
|
|
|
|
|
# Registers signals from each controller
|
|
|
|
for controller, controller_class in self.controllers.items():
|
|
|
|
controller_class.register_signals()
|
2015-08-31 15:55:46 +00:00
|
|
|
log.debug(self.common['signal'])
|
2015-07-21 20:34:46 +00:00
|
|
|
|
2015-07-21 15:55:02 +00:00
|
|
|
# EventLoop -------------------------------------------------------------------
|
|
|
|
def redraw_screen(self):
|
|
|
|
if hasattr(self, 'loop'):
|
|
|
|
try:
|
2015-08-31 15:55:46 +00:00
|
|
|
self.common['loop'].draw_screen()
|
2015-07-21 15:55:02 +00:00
|
|
|
except AssertionError as e:
|
|
|
|
log.critical("Redraw screen error: {}".format(e))
|
|
|
|
|
|
|
|
def set_alarm_in(self, interval, cb):
|
2015-08-31 15:55:46 +00:00
|
|
|
self.common['loop'].set_alarm_in(interval, cb)
|
2015-07-21 15:55:02 +00:00
|
|
|
return
|
|
|
|
|
|
|
|
def update(self, *args, **kwds):
|
|
|
|
""" Update loop """
|
|
|
|
pass
|
|
|
|
|
|
|
|
def exit(self):
|
|
|
|
raise urwid.ExitMainLoop()
|
|
|
|
|
|
|
|
def header_hotkeys(self, key):
|
2015-10-21 18:03:15 +00:00
|
|
|
return False
|
2015-07-21 15:55:02 +00:00
|
|
|
|
|
|
|
def run(self):
|
|
|
|
if not hasattr(self, 'loop'):
|
|
|
|
palette = STYLES
|
|
|
|
additional_opts = {
|
|
|
|
'screen': urwid.raw_display.Screen(),
|
|
|
|
'unhandled_input': self.header_hotkeys,
|
|
|
|
'handle_mouse': False
|
|
|
|
}
|
2015-08-31 15:55:46 +00:00
|
|
|
if self.common['opts'].run_on_serial:
|
2015-07-21 15:55:02 +00:00
|
|
|
palette = STYLES_MONO
|
|
|
|
else:
|
|
|
|
additional_opts['screen'].set_terminal_properties(colors=256)
|
|
|
|
additional_opts['screen'].reset_default_terminal_palette()
|
|
|
|
|
2015-08-23 22:48:52 +00:00
|
|
|
evl = urwid.TornadoEventLoop(IOLoop())
|
2015-08-31 15:55:46 +00:00
|
|
|
self.common['loop'] = urwid.MainLoop(
|
|
|
|
self.common['ui'], palette, event_loop=evl, **additional_opts)
|
|
|
|
log.debug("Running event loop: {}".format(
|
|
|
|
self.common['loop'].event_loop))
|
2015-07-21 15:55:02 +00:00
|
|
|
|
|
|
|
try:
|
|
|
|
self.set_alarm_in(0.05, self.welcome)
|
2015-08-31 15:55:46 +00:00
|
|
|
for k in self.controllers.keys():
|
|
|
|
log.debug("Importing controller: {}".format(k))
|
|
|
|
klass = import_object(
|
2016-06-30 18:17:01 +00:00
|
|
|
"subiquitycore.controllers.{}Controller".format(
|
2015-08-31 15:55:46 +00:00
|
|
|
k))
|
|
|
|
self.controllers[k] = klass(self.common)
|
|
|
|
|
|
|
|
self._connect_base_signals()
|
|
|
|
self.common['loop'].run()
|
2015-07-21 15:55:02 +00:00
|
|
|
except:
|
|
|
|
log.exception("Exception in controller.run():")
|
|
|
|
raise
|
|
|
|
|
2015-08-18 16:29:56 +00:00
|
|
|
# Welcome Mode ------------------------------------------------------------
|
|
|
|
#
|
|
|
|
# Starts the initial UI view.
|
2015-07-21 15:55:02 +00:00
|
|
|
def welcome(self, *args, **kwargs):
|
2015-10-29 20:58:51 +00:00
|
|
|
self.controllers['Welcome'].signal.emit_signal('menu:welcome:main')
|