openlp/openlp/core/api/http/endpoint.py

82 lines
3.3 KiB
Python
Raw Normal View History

2016-08-13 05:03:12 +00:00
# -*- coding: utf-8 -*-
# vim: autoindent shiftwidth=4 expandtab textwidth=120 tabstop=4 softtabstop=4
2019-04-13 13:00:22 +00:00
##########################################################################
# OpenLP - Open Source Lyrics Projection #
# ---------------------------------------------------------------------- #
# Copyright (c) 2008-2019 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, 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 General Public License for more details. #
# #
# You should have received a copy of the GNU General Public License #
# along with this program. If not, see <https://www.gnu.org/licenses/>. #
##########################################################################
2016-08-13 05:03:12 +00:00
"""
The Endpoint class, which provides plugins with a way to serve their own portion of the API
"""
from mako.template import Template
2017-10-07 07:05:07 +00:00
from openlp.core.common.applocation import AppLocation
2016-08-13 05:03:12 +00:00
class Endpoint(object):
"""
This is an endpoint for the HTTP API
"""
def __init__(self, url_prefix, template_dir=None, static_dir=None, assets_dir=None):
"""
Create an endpoint with a URL prefix
"""
self.url_prefix = url_prefix
self.static_dir = static_dir
self.template_dir = template_dir
if assets_dir:
self.assets_dir = assets_dir
else:
2016-12-17 06:58:44 +00:00
self.assets_dir = None
2016-08-13 05:03:12 +00:00
self.routes = []
def add_url_route(self, url, view_func, method):
"""
Add a url route to the list of routes
"""
self.routes.append((url, view_func, method))
def route(self, rule, method='GET'):
"""
Set up a URL route
"""
def decorator(func):
"""
Make this a decorator
"""
self.add_url_route(rule, func, method)
return func
return decorator
def render_template(self, filename, **kwargs):
"""
Render a mako template
2017-11-18 11:23:15 +00:00
:param str filename: The file name of the template to render.
:return: The rendered template object.
:rtype: mako.template.Template
2016-08-13 05:03:12 +00:00
"""
2017-11-18 11:23:15 +00:00
root_path = AppLocation.get_section_data_path('remotes')
2016-08-13 05:03:12 +00:00
if not self.template_dir:
raise Exception('No template directory specified')
2017-11-18 11:23:15 +00:00
path = root_path / self.template_dir / filename
2016-08-13 05:03:12 +00:00
if self.static_dir:
kwargs['static_url'] = '/{prefix}/static'.format(prefix=self.url_prefix)
kwargs['static_url'] = kwargs['static_url'].replace('//', '/')
kwargs['assets_url'] = '/assets'
2017-11-18 11:23:15 +00:00
return Template(filename=str(path), input_encoding='utf-8').render(**kwargs)