2015-08-18 16:29:56 +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/>.
|
|
|
|
|
|
|
|
|
2016-11-01 23:44:04 +00:00
|
|
|
from abc import ABC, abstractmethod
|
2015-08-18 16:29:56 +00:00
|
|
|
import logging
|
|
|
|
|
2016-06-30 18:17:01 +00:00
|
|
|
log = logging.getLogger("subiquitycore.controller")
|
2015-08-18 16:29:56 +00:00
|
|
|
|
2016-11-22 03:09:51 +00:00
|
|
|
def view(func):
|
|
|
|
n = func.__name__
|
|
|
|
def f(self, *args, **kw):
|
|
|
|
m = getattr(self, n)
|
|
|
|
self.view_stack.append((m, args, kw))
|
|
|
|
return func(self, *args, **kw)
|
|
|
|
return f
|
|
|
|
|
2015-08-18 16:29:56 +00:00
|
|
|
|
2016-11-01 23:44:04 +00:00
|
|
|
class BaseController(ABC):
|
2016-07-26 02:16:46 +00:00
|
|
|
"""Base class for controllers."""
|
2015-08-18 16:29:56 +00:00
|
|
|
|
2016-10-10 23:48:28 +00:00
|
|
|
signals = []
|
|
|
|
|
2015-08-31 15:55:46 +00:00
|
|
|
def __init__(self, common):
|
|
|
|
self.ui = common['ui']
|
|
|
|
self.signal = common['signal']
|
|
|
|
self.opts = common['opts']
|
|
|
|
self.loop = common['loop']
|
|
|
|
self.prober = common['prober']
|
2016-06-22 19:19:54 +00:00
|
|
|
self.controllers = common['controllers']
|
2017-01-13 01:42:31 +00:00
|
|
|
self.pool = common['pool']
|
2017-01-17 01:26:07 +00:00
|
|
|
self._callfromthread = common['_callfromthread']
|
2016-11-22 03:09:51 +00:00
|
|
|
self.view_stack = []
|
2015-08-18 16:29:56 +00:00
|
|
|
|
|
|
|
def register_signals(self):
|
2016-07-26 02:16:46 +00:00
|
|
|
"""Defines signals associated with controller from model."""
|
2016-09-29 01:52:11 +00:00
|
|
|
signals = []
|
|
|
|
for sig, cb in self.signals:
|
|
|
|
signals.append((sig, getattr(self, cb)))
|
|
|
|
self.signal.connect_signals(signals)
|
2016-09-27 02:33:54 +00:00
|
|
|
|
2016-11-22 03:09:51 +00:00
|
|
|
def prev_view(self):
|
|
|
|
self.view_stack.pop()
|
|
|
|
meth, args, kw = self.view_stack.pop()
|
|
|
|
meth(*args, **kw)
|
|
|
|
|
2017-01-17 01:26:07 +00:00
|
|
|
def call_from_thread(self, func, *args, **kw):
|
|
|
|
return self._callfromthread.call_from_thread(func, *args, **kw)
|
|
|
|
|
2016-11-01 23:44:04 +00:00
|
|
|
@abstractmethod
|
|
|
|
def cancel(self):
|
|
|
|
pass
|
|
|
|
|
|
|
|
@abstractmethod
|
2016-09-27 02:33:54 +00:00
|
|
|
def default(self):
|
2016-11-01 23:44:04 +00:00
|
|
|
pass
|