"""
The main module for HomePiServer. Initializes SocketIO, ServiceManager,
NavigationChannel, View Manager.
"""


import signal
from threading import Thread

import eventlet
from flask import Flask
from flask_socketio import SocketIO

from .controllers import CONTROLLERS
from .core.logger import configure_logging
from .core.websocket_manager import WebSocketManager
from .core.servicemanager import ServiceManager
from .services import SERVICES


class HomePiServer(object):
    """
    Encapsulates the entire server.
    """
    def __init__(self, config):
        params = {
            "template_folder": "../templates",
            "static_folder": "../static"
        }
        self.flask_app = Flask(__name__, **params)
        self.flask_app.config.from_object(config)
        self.register_blueprints(self.flask_app, CONTROLLERS)

        self.app = SocketIO(self.flask_app)

        self.socket_manager = WebSocketManager(self.app)
        self.service_manager = ServiceManager(SERVICES, self.socket_manager)

        configure_logging(self.flask_app.logger)

        self.start_services()

    def start_services(self):
        """Starts self.service_manager.start() on a new thread."""
        self.service_thread = Thread(target=self.service_manager.start).start()

    @staticmethod
    def register_blueprints(app, params):
        """
        Registers all the blueprints in controllers list.
        Args:
            app: Flask app to register the blueprint with.
            controllers: List like: [(prefix, blueprint), ...]
        """
        for prefix, controller in params:
            app.register_blueprint(controller, url_prefix=prefix)

    def shutdown(self):
        pass


def setup_signals(app):
    """ Listen for SIGTERM and SIGINIT and calls app.shutdown()"""
    def make_new_handler(prev_handler_func):
        def new_handler(var1, var2):
            app.shutdown()
            if prev_handler_func:
                prev_handler_func(var1, var2)
        return new_handler

    for sig in (signal.SIGTERM, signal.SIGINT):
        prev_handler = signal.getsignal(sig)
        signal.signal(sig, make_new_handler(prev_handler))


def create_app(config=None):
    """ Returns a new instance of HomePiServer."""
    if config is None:
        import app.config
        config = app.config
    app = HomePiServer(config)
    setup_signals(app)
    return app.flask_app, app.app

@@ -107,17 +107,16 @@
 r.%0A%22%22%22%0A%0A
-%0A
 import s
@@ -125,347 +125,105 @@
 nal%0A
-from threading import Thread%0A%0Aimport eventlet%0Afrom flask import Flask%0Afrom flask_socketio import SocketIO%0A%0Afrom .controllers import CONTROLLERS%0Afrom .core.logger import configure_logging%0Afrom .core.websocket_manager import WebSocketManager%0Afrom .core.servicemanager import ServiceManager%0Afrom .services import SERVICES%0A%0A%0Aclass HomePiServer
+%0Afrom .core.registry import Registry%0Afrom .core.servicemanager import ServiceManager%0A%0A%0Aclass Hulk
 (obj
@@ -305,16 +305,8 @@
 self
-, config
 ):%0A 
@@ -316,539 +316,94 @@
     
-params = %7B%0A            %22template_folder%22: %22../templates%22,%0A            %22static_folder%22: %22../static%22%0A        %7D%0A        self.flask_app = Flask(__name__, **params)%0A        self.flask_app.config.from_object(config)%0A        self.register_blueprints(self.flask_app, CONTROLLERS)%0A%0A        self.app = SocketIO(self.flask_app)%0A%0A        self.socket_manager = WebSocketManager(self.app)%0A        self.service_manager = ServiceManager(SERVICES, self.socket_manager)%0A%0A        configure_logging(self.flask_app.logger)%0A%0A        self.start_services(
+self.registry = Registry()%0A        self.service_manager = ServiceManager(self.registry
 )%0A%0A 
@@ -418,17 +418,8 @@
 tart
-_services
 (sel
@@ -514,491 +514,82 @@
 ice_
-thread = Thread(target=self.service_manager.start).start()%0A%0A    @staticmethod%0A    def register_blueprints(app, params):%0A        %22%22%22%0A        Registers all the blueprints in controllers list.%0A        Args:%0A            app: Flask app to register the blueprint with.%0A            controllers: List like: %5B(prefix, blueprint), ...%5D%0A        %22%22%22%0A        for prefix, controller in params:%0A            app.register_blueprint(controller, url_prefix=prefix)%0A%0A    def shutdown(self):%0A        pass
+manager.run()%0A%0A    def shutdown(self):%0A        self.service_manager.stop()
 %0A%0A%0Ad
@@ -1068,19 +1068,8 @@
 app(
-config=None
 ):%0A 
@@ -1126,110 +1126,19 @@
     
-if config is None:%0A        import app.config%0A        config = app.config%0A    app = HomePiServer(config
+app = Hulk(
 )%0A  
@@ -1176,24 +1176,5 @@
  app
-.flask_app, app.app
 %0A

Move flask and socketio to services. Remove configure_logging. Rename HomePiServer -> Hulk. Code cleanup.