Move the API around a bit

This commit is contained in:
Raoul Snyman 2016-06-15 00:03:08 +02:00
parent 4a76b3d26d
commit 30ee384a9b
13 changed files with 137 additions and 98 deletions

View File

@ -20,38 +20,9 @@
# Temple Place, Suite 330, Boston, MA 02111-1307 USA #
###############################################################################
from .errors import NotFound, ServerError
from .httprouter import WSGIApplication
from openlp.core.api.http.endpoint import Endpoint
from openlp.core.api.http import register_endpoint
from openlp.core.api.tab import ApiTab
from openlp.core.api.controller import ApiController
application = WSGIApplication('api')
def _route_from_url(url_prefix, url):
"""
Create a route from the URL
"""
url_prefix = '/{prefix}/'.format(prefix=url_prefix.strip('/'))
if not url:
url = url_prefix[:-1]
else:
url = url_prefix + url
url = url.replace('//', '/')
return url
def register_endpoint(end_point):
"""
Register an endpoint with the app
"""
for url, view_func, method, secure in end_point.routes:
route = _route_from_url(end_point.url_prefix, url)
application.add_route(route, view_func, method, secure)
from .endpoint import Endpoint
from .apitab import ApiTab
from .poll import OpenLPPoll
from .wsserver import OpenLPWSServer
from .httpserver import OpenLPHttpServer
from .apicontroller import ApiController
__all__ = ['OpenLPPoll', 'RemoteController', 'OpenLPHttpServer', 'application']
__all__ = ['Endpoint', 'ApiController', 'ApiTab', 'register_endpoint']

View File

@ -21,13 +21,10 @@
###############################################################################
import logging
from openlp.core.api import OpenLPWSServer, OpenLPPoll, OpenLPHttpServer
from openlp.core.api.http.server import HttpServer
from openlp.core.api.websockets import WebSocketServer
from openlp.core.common import OpenLPMixin, Registry, RegistryMixin, RegistryProperties
# These are here to load the endpoints
from openlp.core.api.coreendpoints import stage_endpoint
from openlp.core.api.controllerendpoints import controller_endpoint
log = logging.getLogger(__name__)
@ -48,7 +45,8 @@ class ApiController(RegistryMixin, OpenLPMixin, RegistryProperties):
"""
Register the poll return service and start the servers.
"""
self.poll = OpenLPPoll()
Registry().register('OpenLPPoll', self.poll)
self.wsserver = OpenLPWSServer()
self.httpserver = OpenLPHttpServer()
self.poller = Poller()
Registry().register('Poller', self.poller)
self.ws_server = WebSocketServer()
self.http_server = HttpServer()

View File

@ -1,34 +0,0 @@
"""
openlp/core/api/endpoint.py: Endpoint stuff
"""
class Endpoint(object):
"""
This is an endpoint for the API
"""
def __init__(self, url_prefix):
"""
Create an endpoint with a URL prefix
"""
print("init")
self.url_prefix = url_prefix
self.routes = []
def add_url_route(self, url, view_func, method, secure):
"""
Add a url route to the list of routes
"""
self.routes.append((url, view_func, method, secure))
def route(self, rule, method='GET', secure=False):
"""
Set up a URL route
"""
def decorator(func):
"""
Make this a decorator
"""
self.add_url_route(rule, func, method, secure)
return func
return decorator

View File

@ -0,0 +1,48 @@
# -*- coding: utf-8 -*-
# vim: autoindent shiftwidth=4 expandtab textwidth=120 tabstop=4 softtabstop=4
###############################################################################
# OpenLP - Open Source Lyrics Projection #
# --------------------------------------------------------------------------- #
# Copyright (c) 2008-2016 OpenLP Developers #
# --------------------------------------------------------------------------- #
# This program is free software; you can redistribute it and/or modify it #
# under the terms of the GNU General Public License as published by the Free #
# Software Foundation; version 2 of the License. #
# #
# 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 General Public License for #
# more details. #
# #
# You should have received a copy of the GNU General Public License along #
# with this program; if not, write to the Free Software Foundation, Inc., 59 #
# Temple Place, Suite 330, Boston, MA 02111-1307 USA #
###############################################################################
from openlp.core.api.http.wsgiapp import WSGIApplication
application = WSGIApplication('api')
def _route_from_url(url_prefix, url):
"""
Create a route from the URL
"""
url_prefix = '/{prefix}/'.format(prefix=url_prefix.strip('/'))
if not url:
url = url_prefix[:-1]
else:
url = url_prefix + url
url = url.replace('//', '/')
return url
def register_endpoint(end_point):
"""
Register an endpoint with the app
"""
for url, view_func, method, secure in end_point.routes:
route = _route_from_url(end_point.url_prefix, url)
application.add_route(route, view_func, method, secure)

View File

@ -0,0 +1,55 @@
# -*- coding: utf-8 -*-
# vim: autoindent shiftwidth=4 expandtab textwidth=120 tabstop=4 softtabstop=4
###############################################################################
# OpenLP - Open Source Lyrics Projection #
# --------------------------------------------------------------------------- #
# Copyright (c) 2008-2016 OpenLP Developers #
# --------------------------------------------------------------------------- #
# This program is free software; you can redistribute it and/or modify it #
# under the terms of the GNU General Public License as published by the Free #
# Software Foundation; version 2 of the License. #
# #
# 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 General Public License for #
# more details. #
# #
# You should have received a copy of the GNU General Public License along #
# with this program; if not, write to the Free Software Foundation, Inc., 59 #
# Temple Place, Suite 330, Boston, MA 02111-1307 USA #
###############################################################################
"""
openlp/core/api/endpoint.py: Endpoint stuff
"""
class Endpoint(object):
"""
This is an endpoint for the HTTP API
"""
def __init__(self, url_prefix):
"""
Create an endpoint with a URL prefix
"""
print("init")
self.url_prefix = url_prefix
self.routes = []
def add_url_route(self, url, view_func, method, secure):
"""
Add a url route to the list of routes
"""
self.routes.append((url, view_func, method, secure))
def route(self, rule, method='GET', secure=False):
"""
Set up a URL route
"""
def decorator(func):
"""
Make this a decorator
"""
self.add_url_route(rule, func, method, secure)
return func
return decorator

View File

@ -30,13 +30,13 @@ import logging
from PyQt5 import QtCore
from waitress import serve
from openlp.core.api import application
from openlp.core.api.http import application
from openlp.core.common import RegistryProperties, OpenLPMixin
log = logging.getLogger(__name__)
class HttpThread(QtCore.QObject):
class HttpWorker(QtCore.QObject):
"""
A special Qt thread class to allow the HTTP server to run at the same time as the UI.
"""
@ -46,7 +46,7 @@ class HttpThread(QtCore.QObject):
:param server: The http server class.
"""
super().__init__()
super(HttpWorker).__init__()
def start(self):
"""
@ -58,7 +58,7 @@ class HttpThread(QtCore.QObject):
pass
class OpenLPHttpServer(RegistryProperties, OpenLPMixin):
class HttpServer(RegistryProperties, OpenLPMixin):
"""
Wrapper round a server instance
"""
@ -66,9 +66,9 @@ class OpenLPHttpServer(RegistryProperties, OpenLPMixin):
"""
Initialise the http server, and start the http server
"""
super(OpenLPHttpServer, self).__init__()
super(HttpServer, self).__init__()
self.thread = QtCore.QThread()
self.worker = HttpThread()
self.worker = HttpWorker()
self.worker.moveToThread(self.thread)
self.thread.started.connect(self.worker.start)
self.thread.start()

View File

@ -28,7 +28,7 @@ import re
from webob import Request, Response
from .errors import HttpError, NotFound, ServerError
from openlp.core.api.http.errors import HttpError, NotFound, ServerError
log = logging.getLogger(__name__)

View File

@ -37,7 +37,7 @@ from openlp.core.common import Settings, RegistryProperties, OpenLPMixin, Regist
log = logging.getLogger(__name__)
class WSThread(QtCore.QObject):
class WebSocketWorker(QtCore.QObject):
"""
A special Qt thread class to allow the WebSockets server to run at the same time as the UI.
"""
@ -47,7 +47,7 @@ class WSThread(QtCore.QObject):
:param server: The http server class.
"""
super().__init__()
super(WebSocketWorker).__init__()
self.ws_server = server
def start(self):
@ -60,7 +60,7 @@ class WSThread(QtCore.QObject):
self.ws_server.stop = True
class OpenLPWSServer(RegistryProperties, OpenLPMixin):
class WebSocketServer(RegistryProperties, OpenLPMixin):
"""
Wrapper round a server instance
"""
@ -68,10 +68,10 @@ class OpenLPWSServer(RegistryProperties, OpenLPMixin):
"""
Initialise the http server, and start the WebSockets server
"""
super(OpenLPWSServer, self).__init__()
super(WebSocketServer, self).__init__()
self.settings_section = 'remotes'
self.thread = QtCore.QThread()
self.worker = WSThread(self)
self.worker = WebSocketWorker(self)
self.worker.moveToThread(self.thread)
self.thread.started.connect(self.worker.start)
self.thread.start()
@ -125,17 +125,18 @@ class OpenLPWSServer(RegistryProperties, OpenLPMixin):
log.debug("web socket handler registered with client")
previous_poll = None
previous_main_poll = None
openlppoll = Registry().get('OpenLPPoll')
poller = Registry().get('Poller')
# TODO: FIXME: These URLs need to be named better
if path == '/poll':
while True:
current_poll = openlppoll.poll()
current_poll = poller.poll()
if current_poll != previous_poll:
yield from request.send(current_poll)
previous_poll = current_poll
yield from asyncio.sleep(0.2)
elif path == '/main_poll':
while True:
main_poll = openlppoll.main_poll()
main_poll = poller.main_poll()
if main_poll != previous_main_poll:
yield from request.send(main_poll)
previous_main_poll = main_poll

View File

@ -528,15 +528,15 @@ class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow, RegistryProperties):
self.copy_data = False
Settings().set_up_default_values()
self.about_form = AboutForm(self)
MediaController()
ApiController()
SettingsForm(self)
self.media_controller = MediaController()
self.api_controller = ApiController()
self.settings_form = SettingsForm(self)
self.formatting_tag_form = FormattingTagForm(self)
self.shortcut_form = ShortcutListForm(self)
# Set up the path with plugins
PluginManager(self)
ImageManager()
Renderer()
self.plugin_manager = PluginManager(self)
self.image_manager = ImageManager()
self.renderer = Renderer()
# Set up the interface
self.setupUi(self)
# Define the media Dock Manager