Skip to content

Commit

Permalink
#45 - Prepare to remove BanksController, EffectController, Pedalboard…
Browse files Browse the repository at this point in the history
…Controller, ParamController
  • Loading branch information
SrMouraSilva committed May 27, 2017
1 parent 24dfa19 commit 248e612
Show file tree
Hide file tree
Showing 16 changed files with 401 additions and 133 deletions.
28 changes: 15 additions & 13 deletions application/application.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,14 @@
from shutil import copytree
from unittest.mock import MagicMock

from application.controller.banks_controller import BanksController
from application.component.components_observer import ComponentsObserver
from application.component.current_pedalboard_observer import CurrentPedalboardObserver
from application.controller.component_data_controller import ComponentDataController
from application.controller.current_controller import CurrentController
from application.controller.device_controller import DeviceController
from application.controller.effect_controller import EffectController
from application.controller.notification_controller import NotificationController
from application.controller.param_controller import ParamController
from application.controller.pedalboard_controller import PedalboardController
from application.controller.plugins_controller import PluginsController

from pluginsmanager.mod_host.mod_host import ModHost
from pluginsmanager.observer.autosaver.autosaver import Autosaver
from pluginsmanager.observer.mod_host.mod_host import ModHost

logging.basicConfig(format='[%(asctime)s] %(levelname)s - %(message)s', stream=sys.stdout, level=logging.DEBUG)

Expand Down Expand Up @@ -69,16 +65,27 @@ class Application(object):
"""

def __init__(self, path_data="data/", address="localhost", test=False):
path_data = Path(path_data)
self.mod_host = self._initialize(address, test)

# Data
path_data = Path(path_data)
self.path_data = self._initialize_data(path_data)
self.autosaver = Autosaver(str(path_data / Path('banks')))
self.manager = self.autosaver.load(DeviceController.sys_effect)

# Controllers
self.components = []
self.controllers = self._load_controllers()

self._configure_controllers(self.controllers)

# Observers
self._components_observer = ComponentsObserver(self.manager)
current_pedalboard_observer = CurrentPedalboardObserver(self.controller(CurrentController))

self.manager.register(self._components_observer)
self.manager.register(current_pedalboard_observer)

def _initialize(self, address, test=False):
mod_host = ModHost(address)
if test:
Expand All @@ -105,14 +112,9 @@ def _load_controllers(self):
controllers = {}

list_controllers = [
BanksController,
ComponentDataController,
CurrentController,
DeviceController,
EffectController,
NotificationController,
ParamController,
PedalboardController,
PluginsController,
]

Expand Down
16 changes: 2 additions & 14 deletions application/component/application_observer.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
# limitations under the License.

from abc import ABCMeta, abstractmethod
from pluginsmanager.model.updates_observer import UpdatesObserver
from pluginsmanager.observer.updates_observer import UpdatesObserver


class ApplicationObserver(UpdatesObserver, metaclass=ABCMeta):
Expand All @@ -40,23 +40,11 @@ class ApplicationObserver(UpdatesObserver, metaclass=ABCMeta):
def __init__(self):
super(ApplicationObserver, self).__init__()

@property
@abstractmethod
def token(self):
"""
Observer token identifier.
:return: string for token identifier or None if is not necessary identify the observer
(it will receive all notification)
"""
pass

@abstractmethod
def on_current_pedalboard_changed(self, pedalboard, token=None):
def on_current_pedalboard_changed(self, pedalboard, **kwargs):
"""
Called when the current pedalboard is changed
:param Pedalboard pedalboard: New current pedalboard
:param string token: Request token identifier
"""
pass
65 changes: 65 additions & 0 deletions application/component/components_observer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
# Copyright 2017 SrMouraSilva
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from pluginsmanager.observer.updates_observer import UpdatesObserver


class ComponentsObserver(UpdatesObserver):

def __init__(self, manager):
super(ComponentsObserver, self).__init__()
self.observers = []
self._manager = manager

def register(self, observer):
self.observers.append(observer)
observer.manager = self.manager

def unregister(self, observer):
observer.manager = None
self.observers.remove(observer)

@property
def scope(self):
return self._manager.observer_manager.scope

def on_bank_updated(self, bank, update_type, index, origin, **kwargs):
for observer in self.observers:
if observer != self.scope:
observer.on_bank_updated(bank, update_type, index=index, origin=origin, **kwargs)

def on_pedalboard_updated(self, pedalboard, update_type, index, origin, **kwargs):
for observer in self.observers:
if observer != self.scope:
observer.on_pedalboard_updated(pedalboard, update_type, index=index, origin=origin, **kwargs)

def on_effect_updated(self, effect, update_type, index, origin, **kwargs):
for observer in self.observers:
if observer != self.scope:
observer.on_effect_updated(effect, update_type, index=index, origin=origin, **kwargs)

def on_effect_status_toggled(self, effect, **kwargs):
for observer in self.observers:
if observer != self.scope:
observer.on_effect_status_toggled(effect, **kwargs)

def on_param_value_changed(self, param, **kwargs):
for observer in self.observers:
if observer != self.scope:
observer.on_param_value_changed(param, **kwargs)

def on_connection_updated(self, connection, update_type, pedalboard, **kwargs):
for observer in self.observers:
if observer != self.scope:
observer.on_connection_updated(connection, update_type, pedalboard=pedalboard, **kwargs)
66 changes: 66 additions & 0 deletions application/component/current_pedalboard_observer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
# Copyright 2017 SrMouraSilva
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from application.component.application_observer import ApplicationObserver
from pluginsmanager.observer.update_type import UpdateType


class CurrentPedalboardObserver(ApplicationObserver):
"""
This viewer allows change the current pedalboard
if it is updated or removed or if your bank is updated or removed.
"""

def __init__(self, current_controller):
"""
:param CurrentController current_controller:
"""
super(CurrentPedalboardObserver, self).__init__()
self._current_controller = current_controller

def on_bank_updated(self, bank, update_type, index, origin, **kwargs):
if update_type == UpdateType.UPDATED:
old_bank = kwargs['old']
if old_bank == self._current_controller.bank:
self._current_controller.set_bank(bank, try_preserve_index=True)

elif update_type == UpdateType.DELETED:
if bank == self._current_controller.bank:
next_bank_index = self._current_controller.next_bank_index(index-1)
new_current_bank = origin.banks[next_bank_index]

self._current_controller.set_bank(new_current_bank)

def on_pedalboard_updated(self, pedalboard, update_type, index, origin, **kwargs):
if update_type == UpdateType.UPDATED:
old_pedalboard = kwargs['old']

if pedalboard == self._current_controller.pedalboard \
or old_pedalboard == self._current_controller.pedalboard:
self._current_controller.set_pedalboard(pedalboard, notify=False, force=True)

def on_effect_status_toggled(self, effect, **kwargs):
pass

def on_connection_updated(self, connection, update_type, pedalboard, **kwargs):
pass

def on_param_value_changed(self, param, **kwargs):
pass

def on_effect_updated(self, effect, update_type, index, origin, **kwargs):
pass

def on_current_pedalboard_changed(self, pedalboard, token=None):
pass
2 changes: 1 addition & 1 deletion application/controller/banks_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
from application.controller.notification_controller import NotificationController

from pluginsmanager.model.bank import Bank
from pluginsmanager.model.update_type import UpdateType
from pluginsmanager.observer.update_type import UpdateType


class BankError(Exception):
Expand Down
18 changes: 16 additions & 2 deletions application/controller/current_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ def next_bank_index(self, current_index):

return next_index

def set_bank(self, bank, token=None, notify=True):
def set_bank(self, bank, token=None, notify=True, try_preserve_index=False):
"""
Set the current :class:`Bank` for the bank
only if the ``bank != current_bank``
Expand All @@ -236,6 +236,12 @@ def set_bank(self, bank, token=None, notify=True):
:param string token: Request token identifier
:param bool notify: If false, not notify change for :class:`UpdatesObserver`
instances registered in :class:`Application`
:param bool try_preserve_index: Tries to preserve the index of the current pedalboard
when changing the bank. That is, if the current pedalboard is the fifth,
when updating the bank, it will attempt to place the fifth pedalboard
of the new bank as the current one. If it does not get
(``len(bank.pedalboards) < 6``) the current pedalboard will be the
first pedalboard.
"""
if bank not in self._manager:
raise CurrentPedalboardError('Bank {} has not added in banks manager'.format(bank))
Expand All @@ -244,6 +250,14 @@ def set_bank(self, bank, token=None, notify=True):
return

if bank.pedalboards:
self.set_pedalboard(bank.pedalboards[0], token, notify)
pedalboard = self._equivalent_pedalboard(bank) if try_preserve_index else bank.pedalboards[0]
self.set_pedalboard(pedalboard, token, notify)
else:
self.set_pedalboard(None, token, notify)

def _equivalent_pedalboard(self, other_bank):
index = self.pedalboard.index
try:
return other_bank.pedalboards[index]
except IndexError:
return other_bank.pedalboards[0]
5 changes: 1 addition & 4 deletions application/controller/device_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,7 @@ def __init__(self, application):
super(DeviceController, self).__init__(application)

def configure(self):
from application.controller.banks_controller import BanksController
banks_controller = self.app.controller(BanksController)

banks_controller.manager.register(self.mod_host)
self.app.manager.register(self.mod_host)

def close(self):
self.mod_host.close()
Expand Down
2 changes: 1 addition & 1 deletion application/controller/effect_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
from application.controller.controller import Controller
from application.controller.notification_controller import NotificationController

from pluginsmanager.model.update_type import UpdateType
from pluginsmanager.observer.update_type import UpdateType


class EffectError(Exception):
Expand Down

0 comments on commit 248e612

Please sign in to comment.