2015-07-16 21:58:32 +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/>.
|
|
|
|
|
2015-07-21 20:34:46 +00:00
|
|
|
|
|
|
|
""" Registers all known signal emitters
|
|
|
|
"""
|
|
|
|
import logging
|
2019-09-09 02:52:36 +00:00
|
|
|
import types
|
|
|
|
|
|
|
|
import urwid
|
2015-07-21 20:34:46 +00:00
|
|
|
|
|
|
|
log = logging.getLogger('subiquity.signals')
|
|
|
|
|
|
|
|
|
|
|
|
class SignalException(Exception):
|
|
|
|
"Problem with a signal"
|
2015-07-16 21:58:32 +00:00
|
|
|
|
|
|
|
|
2015-07-21 15:55:02 +00:00
|
|
|
class Signal:
|
2015-07-21 20:34:46 +00:00
|
|
|
known_signals = []
|
|
|
|
|
|
|
|
def register_signals(self, signals):
|
|
|
|
if type(signals) is list:
|
|
|
|
self.known_signals.extend(signals)
|
2015-07-22 02:51:06 +00:00
|
|
|
else:
|
|
|
|
self.known_signals.append(signals)
|
2019-03-13 20:55:03 +00:00
|
|
|
urwid.register_signal(Signal, self.known_signals)
|
2015-07-21 20:34:46 +00:00
|
|
|
|
|
|
|
def emit_signal(self, name, *args, **kwargs):
|
|
|
|
urwid.emit_signal(self, name, *args, **kwargs)
|
|
|
|
|
2019-09-09 02:52:36 +00:00
|
|
|
def connect_signal(self, name, cb):
|
|
|
|
if isinstance(cb, types.MethodType):
|
|
|
|
scb = "{}.{}".format(
|
|
|
|
cb.__self__.__class__.__name__, cb.__func__.__name__)
|
|
|
|
else:
|
|
|
|
scb = str(cb)
|
|
|
|
log.debug("connect_signal: %s -> %s", name, scb)
|
|
|
|
urwid.connect_signal(self, name, cb)
|
2015-07-21 20:34:46 +00:00
|
|
|
|
|
|
|
def connect_signals(self, signal_callback):
|
|
|
|
""" Connects a batch of signals
|
|
|
|
|
2019-03-07 01:13:06 +00:00
|
|
|
:param list signal_callback: List of tuples eg. ('signame', self.cb)
|
2015-07-21 20:34:46 +00:00
|
|
|
"""
|
|
|
|
if not type(signal_callback) is list:
|
|
|
|
raise SignalException(
|
|
|
|
"Passed something other than a required list.")
|
|
|
|
for sig, cb in signal_callback:
|
2019-03-13 20:55:03 +00:00
|
|
|
if sig not in self.known_signals:
|
|
|
|
self.register_signals(sig)
|
2015-07-21 20:34:46 +00:00
|
|
|
self.connect_signal(sig, cb)
|