12
12
# See the License for the specific language governing permissions and
13
13
# limitations under the License.
14
14
15
- import time
15
+ import atexit
16
+ import logging
16
17
import os
17
- from shutil import copytree
18
+ import sys
18
19
from pathlib import Path
20
+ from shutil import copytree
21
+ from unittest .mock import MagicMock
19
22
20
- from application .controller . banks_controller import BanksController
21
- from application .controller . current_controller import CurrentController
23
+ from application .component . components_observer import ComponentsObserver
24
+ from application .component . current_pedalboard_observer import CurrentPedalboardObserver
22
25
from application .controller .component_data_controller import ComponentDataController
26
+ from application .controller .current_controller import CurrentController
23
27
from application .controller .device_controller import DeviceController
24
- from application .controller .effect_controller import EffectController
25
- from application .controller .notification_controller import NotificationController
26
- from application .controller .param_controller import ParamController
27
- from application .controller .pedalboard_controller import PedalboardController
28
28
from application .controller .plugins_controller import PluginsController
29
+ from pluginsmanager .observer .autosaver .autosaver import Autosaver
30
+ from pluginsmanager .observer .mod_host .mod_host import ModHost
29
31
30
- from pluginsmanager .mod_host .mod_host import ModHost
31
-
32
- from unittest .mock import MagicMock
32
+ logging .basicConfig (format = '[%(asctime)s] %(levelname)s - %(message)s' , stream = sys .stdout , level = logging .DEBUG )
33
33
34
34
35
35
class Application (object ):
@@ -43,16 +43,16 @@ class Application(object):
43
43
for control::
44
44
45
45
>>> from application.application import Application
46
- >>> from application.controller.CurrentController import CurrentController
46
+ >>> from application.controller.current_controller import CurrentController
47
47
48
48
>>> application = Application()
49
49
>>> current_controller = application.controller(CurrentController)
50
50
51
- >>> print(current_controller.current_pedalboard )
51
+ >>> print(current_controller.pedalboard )
52
52
<Pedalboard object as Shows with 2 effects at 0x7fa3bcb49be0>
53
53
54
54
>>> current_controller.to_next_pedalboard()
55
- >>> current_controller.current_pedalboard
55
+ >>> current_controller.pedalboard
56
56
<Pedalboard object as Shows 2 with 1 effects at 0x7fa3bbcdecf8>
57
57
58
58
For more details see the Controllers extended classes.
@@ -67,12 +67,25 @@ class Application(object):
67
67
def __init__ (self , path_data = "data/" , address = "localhost" , test = False ):
68
68
self .mod_host = self ._initialize (address , test )
69
69
70
+ # Data
71
+ path_data = Path (path_data )
70
72
self .path_data = self ._initialize_data (path_data )
73
+ self .autosaver = Autosaver (str (path_data / Path ('banks' )))
74
+ self .manager = self .autosaver .load (DeviceController .sys_effect )
75
+
76
+ # Controllers
71
77
self .components = []
72
78
self .controllers = self ._load_controllers ()
73
79
74
80
self ._configure_controllers (self .controllers )
75
81
82
+ # Observers
83
+ self .components_observer = ComponentsObserver (self .manager )
84
+ current_pedalboard_observer = CurrentPedalboardObserver (self .controller (CurrentController ))
85
+
86
+ self .manager .register (self .components_observer )
87
+ self .manager .register (current_pedalboard_observer )
88
+
76
89
def _initialize (self , address , test = False ):
77
90
mod_host = ModHost (address )
78
91
if test :
@@ -83,34 +96,25 @@ def _initialize(self, address, test=False):
83
96
return mod_host
84
97
85
98
def _initialize_data (self , path ):
86
- if not os .path .exists (path ):
99
+ str_path = str (path )
100
+ if not os .path .exists (str_path ):
87
101
default_path_data = os .path .dirname (os .path .abspath (__file__ )) / Path ('data' )
88
102
89
103
ignore_files = lambda d , files : [f for f in files if (Path (d ) / Path (f )).is_file () and f .endswith ('.py' )]
90
- copytree (str (default_path_data ), str (os .path .abspath (path )), ignore = ignore_files )
104
+ copytree (str (default_path_data ), str (os .path .abspath (str_path )), ignore = ignore_files )
91
105
92
106
self .log ('Data - Create initial data' )
93
107
94
- self .log ('Data - Loads' , os .path .abspath (path ))
108
+ self .log ('Data - Loads {} ' , os .path .abspath (str_path ))
95
109
return path
96
110
97
- def _teste (self , d , files ):
98
- for f in files :
99
- print (f )
100
- return
101
-
102
111
def _load_controllers (self ):
103
112
controllers = {}
104
113
105
114
list_controllers = [
106
- BanksController ,
107
115
ComponentDataController ,
108
116
CurrentController ,
109
117
DeviceController ,
110
- EffectController ,
111
- NotificationController ,
112
- ParamController ,
113
- PedalboardController ,
114
118
PluginsController ,
115
119
]
116
120
@@ -122,28 +126,69 @@ def _load_controllers(self):
122
126
def _configure_controllers (self , controllers ):
123
127
for controller in list (controllers .values ()):
124
128
controller .configure ()
125
- self .log ('Load controller -' , controller .__class__ .__name__ )
129
+ self .log ('Load controller - {} ' , controller .__class__ .__name__ )
126
130
127
131
def register (self , component ):
128
132
"""
129
- Register a :class:`Component` extended class into Application.
133
+ Register a :class:`. Component` extended class into Application.
130
134
The components will be loaded when application is loaded (after `start` method is called).
131
135
132
136
:param Component component: A module to be loaded when the Application is loaded
133
137
"""
134
138
self .components .append (component )
135
139
140
+ def register_observer (self , observer ):
141
+ """
142
+ Register a :class:`.ApplicationObserver` specialization into Application.
143
+ The observer will receive calls when changes occurs in system, like
144
+ banks creation, current pedalboard changes.
145
+
146
+ :param ApplicationObserver observer: The observer who will receive the changes notifications
147
+ """
148
+ self .components_observer .register (observer )
149
+
150
+ def unregister_observer (self , observer ):
151
+ """
152
+ Unregister an observer in :class:`.Application`.
153
+ The observer not will be more notified of the changes requested in the application API.
154
+
155
+ :param ApplicationObserver observer: The observer who will not receive further changes notification
156
+ """
157
+ self .components_observer .unregister (observer )
158
+
136
159
def start (self ):
137
160
"""
138
- Start this API , initializing the components.
161
+ Start the application , initializing your components.
139
162
"""
140
- current_pedalboard = self .controller (CurrentController ).current_pedalboard
141
- self .log ('Load current pedalboard -' , '"' + current_pedalboard .name + '"' )
163
+ current_pedalboard = self .controller (CurrentController ).pedalboard
164
+ if current_pedalboard is None :
165
+ self .log ('Not exists any current pedalboard.' )
166
+ self .log ('Use CurrentController to set the current pedalboard' )
167
+ else :
168
+ self .log ('Load current pedalboard - "{}"' , current_pedalboard .name )
169
+
142
170
self .mod_host .pedalboard = current_pedalboard
143
171
144
172
for component in self .components :
145
173
component .init ()
146
- self .log ('Load component -' , component .__class__ .__name__ )
174
+ self .log ('Load component - {}' , component .__class__ .__name__ )
175
+
176
+ self .log ('Components loaded' )
177
+ atexit .register (self .stop )
178
+
179
+ def stop (self ):
180
+ """
181
+ Stop the application, closing your components.
182
+ """
183
+ for component in self .components :
184
+ component .close ()
185
+ self .log ('Stopping component - {}' , component .__class__ .__name__ )
186
+
187
+ for controller in self .controllers .values ():
188
+ controller .close ()
189
+ self .log ('Stopping controller - {}' , controller .__class__ .__name__ )
190
+
191
+ atexit .unregister (self .stop )
147
192
148
193
def controller (self , controller ):
149
194
"""
@@ -163,5 +208,5 @@ def dao(self, dao):
163
208
"""
164
209
return dao (self .path_data )
165
210
166
- def log (self , * args , ** kwargs ):
167
- print ( '[' + time . strftime ( '%Y-%m-%d %H:%M:%S' ) + ']' , * args , ** kwargs )
211
+ def log (self , message , * args , ** kwargs ):
212
+ logging . info ( message . format ( * args , ** kwargs ) )
0 commit comments